xref: /netbsd-src/usr.sbin/diskpart/diskpart.c (revision 81794312729eae751db5b27e66b92ca6fc48671d)
161f28255Scgd /*
26aeb76c1Smikel  * Copyright (c) 1983, 1988, 1993
36aeb76c1Smikel  *	The Regents of the University of California.  All rights reserved.
461f28255Scgd  *
561f28255Scgd  * Redistribution and use in source and binary forms, with or without
661f28255Scgd  * modification, are permitted provided that the following conditions
761f28255Scgd  * are met:
861f28255Scgd  * 1. Redistributions of source code must retain the above copyright
961f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1061f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1261f28255Scgd  *    documentation and/or other materials provided with the distribution.
13326b2259Sagc  * 3. Neither the name of the University nor the names of its contributors
1461f28255Scgd  *    may be used to endorse or promote products derived from this software
1561f28255Scgd  *    without specific prior written permission.
1661f28255Scgd  *
1761f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1861f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1961f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2061f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2161f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2261f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2361f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2461f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2561f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2661f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2761f28255Scgd  * SUCH DAMAGE.
2861f28255Scgd  */
2961f28255Scgd 
30328c1f1dSlukem #include <sys/cdefs.h>
3161f28255Scgd #ifndef lint
329c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
339c194566Slukem  The Regents of the University of California.  All rights reserved.");
3461f28255Scgd #endif /* not lint */
3561f28255Scgd 
3661f28255Scgd #ifndef lint
37328c1f1dSlukem #if 0
38328c1f1dSlukem static char sccsid[] = "from: @(#)diskpart.c	8.3 (Berkeley) 11/30/94";
39328c1f1dSlukem #else
40*81794312Sandvar __RCSID("$NetBSD: diskpart.c,v 1.21 2021/08/01 18:02:22 andvar Exp $");
41328c1f1dSlukem #endif
4261f28255Scgd #endif /* not lint */
4361f28255Scgd 
4461f28255Scgd /*
4561f28255Scgd  * Program to calculate standard disk partition sizes.
4661f28255Scgd  */
4761f28255Scgd #include <sys/param.h>
4861f28255Scgd #define DKTYPENAMES
4961f28255Scgd #include <sys/disklabel.h>
5061f28255Scgd 
5161f28255Scgd #include <ctype.h>
52257673aaStron #include <disktab.h>
531375baa2Slukem #include <limits.h>
541375baa2Slukem #include <stdio.h>
551375baa2Slukem #include <stdlib.h>
561375baa2Slukem #include <string.h>
571375baa2Slukem #include <unistd.h>
5861f28255Scgd 
5961f28255Scgd #define	for_now			/* show all of `c' partition for disklabel */
6061f28255Scgd #define	NPARTITIONS	8
6161f28255Scgd #define	PART(x)		(x - 'a')
6261f28255Scgd 
6361f28255Scgd /*
6461f28255Scgd  * Default partition sizes, where they exist.
6561f28255Scgd  */
6661f28255Scgd #define	NDEFAULTS	4
675c1014c6Sjoerg static int	defpart[NDEFAULTS][NPARTITIONS] = {
6861f28255Scgd    { 15884, 66880, 0, 15884, 307200, 0, 0, 291346 },	/* ~ 356+ Mbytes */
6961f28255Scgd    { 15884, 33440, 0, 15884, 55936, 0, 0, 291346 },	/* ~ 206-355 Mbytes */
7061f28255Scgd    { 15884, 33440, 0, 15884, 55936, 0, 0, 0 },		/* ~ 61-205 Mbytes */
7161f28255Scgd    { 15884, 10032, 0, 15884, 0, 0, 0, 0 },		/* ~ 20-60 Mbytes */
7261f28255Scgd };
7361f28255Scgd 
7461f28255Scgd /*
7561f28255Scgd  * Each array defines a layout for a disk;
7661f28255Scgd  * that is, the collection of partitions totally
7761f28255Scgd  * covers the physical space on a disk.
7861f28255Scgd  */
7961f28255Scgd #define	NLAYOUTS	3
805c1014c6Sjoerg static char	layouts[NLAYOUTS][NPARTITIONS] = {
8161f28255Scgd    { 'a', 'b', 'h', 'g' },
8261f28255Scgd    { 'a', 'b', 'h', 'd', 'e', 'f' },
8361f28255Scgd    { 'c' },
8461f28255Scgd };
8561f28255Scgd 
8661f28255Scgd /*
8761f28255Scgd  * Default disk block and disk block fragment
8861f28255Scgd  * sizes for each file system.  Those file systems
8961f28255Scgd  * with zero block and frag sizes are special cases
9061f28255Scgd  * (e.g. swap areas or for access to the entire device).
9161f28255Scgd  */
925c1014c6Sjoerg static struct	partition defparam[NPARTITIONS] = {
934b313df1Sdrochner 	{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },		/* a */
944b313df1Sdrochner 	{ 0, 0, { 1024 }, FS_SWAP,   8, { 0 }, },		/* b */
954b313df1Sdrochner 	{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },		/* c */
964b313df1Sdrochner 	{ 0, 0, {  512 }, FS_UNUSED, 8, { 0 }, },		/* d */
974b313df1Sdrochner 	{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },		/* e */
984b313df1Sdrochner 	{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },		/* f */
994b313df1Sdrochner 	{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },		/* g */
1004b313df1Sdrochner 	{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, }		/* h */
10161f28255Scgd };
10261f28255Scgd 
10361f28255Scgd /*
10461f28255Scgd  * Each disk has some space reserved for a bad sector
10561f28255Scgd  * forwarding table.  DEC standard 144 uses the first
10661f28255Scgd  * 5 even numbered sectors in the last track of the
10761f28255Scgd  * last cylinder for replicated storage of the bad sector
10861f28255Scgd  * table; another 126 sectors past this is needed as a
10961f28255Scgd  * pool of replacement sectors.
11061f28255Scgd  */
1115c1014c6Sjoerg static int	badsecttable = 126;	/* # sectors */
11261f28255Scgd 
1135c1014c6Sjoerg static int	pflag;			/* print device driver partition tables */
1145c1014c6Sjoerg static int	dflag;			/* print disktab entry */
11561f28255Scgd 
1165c1014c6Sjoerg static int	gettype(const char *, const char *const *);
1175c1014c6Sjoerg static struct disklabel *promptfordisk(const char *);
1185c1014c6Sjoerg __dead static void	usage(void);
11961f28255Scgd 
1201375baa2Slukem int
main(int argc,char * argv[])1215c1014c6Sjoerg main(int argc, char *argv[])
12261f28255Scgd {
12361f28255Scgd 	struct disklabel *dp;
1243d305af9Slukem 	int spc, def, part, layout, j, ch;
1253d305af9Slukem 	uint32_t curcyl;
126*81794312Sandvar 	int threshold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
1271375baa2Slukem 	off_t totsize = 0;
128b2bb6949Sxtraeme 	const char *tyname;
129b2bb6949Sxtraeme 	char *lp;
13061f28255Scgd 
1311375baa2Slukem 	while ((ch = getopt(argc, argv, "pds:")) != -1) {
1321375baa2Slukem 		switch (ch) {
1331375baa2Slukem 		case 'd':
13461f28255Scgd 			dflag++;
1351375baa2Slukem 			break;
1361375baa2Slukem 
1371375baa2Slukem 		case 'p':
1381375baa2Slukem 			pflag++;
1391375baa2Slukem 			break;
1401375baa2Slukem 
1411375baa2Slukem 		case 's':
1421375baa2Slukem 			totsize = strtoul(optarg, &lp, 10);
1431375baa2Slukem 			if (*lp != '\0')
1441375baa2Slukem 				usage();
1451375baa2Slukem 			break;
1461375baa2Slukem 
1471375baa2Slukem 		case '?':
1481375baa2Slukem 		default:
1491375baa2Slukem 			usage();
1501375baa2Slukem 			/* NOTREACHED */
15161f28255Scgd 		}
15261f28255Scgd 	}
1531375baa2Slukem 	argc -= optind;
1541375baa2Slukem 	argv += optind;
1551375baa2Slukem 
1561375baa2Slukem 	if (argc != 1) {
1571375baa2Slukem 		usage();
1581375baa2Slukem 		/* NOTREACHED */
1591375baa2Slukem 	}
1601375baa2Slukem 
16161f28255Scgd 	dp = getdiskbyname(*argv);
16261f28255Scgd 	if (dp == NULL) {
16361f28255Scgd 		if (isatty(0))
16461f28255Scgd 			dp = promptfordisk(*argv);
16561f28255Scgd 		if (dp == NULL) {
16661f28255Scgd 			fprintf(stderr, "%s: unknown disk type\n", *argv);
1671375baa2Slukem 			exit(1);
16861f28255Scgd 		}
1691375baa2Slukem 	}
17061f28255Scgd 	if (dp->d_flags & D_REMOVABLE)
17161f28255Scgd 		tyname = "removable";
17261f28255Scgd 	else if (dp->d_flags & D_RAMDISK)
17361f28255Scgd 		tyname = "simulated";
17461f28255Scgd 	else
17561f28255Scgd 		tyname = "winchester";
17661f28255Scgd 	spc = dp->d_secpercyl;
17761f28255Scgd 	/*
17861f28255Scgd 	 * Bad sector table contains one track for the replicated
17961f28255Scgd 	 * copies of the table and enough full tracks preceding
18061f28255Scgd 	 * the last track to hold the pool of free blocks to which
18161f28255Scgd 	 * bad sectors are mapped.
18261f28255Scgd 	 * If disk size was specified explicitly, use specified size.
18361f28255Scgd 	 */
18452e8eb13Schristos 	if (dp->d_type == DKTYPE_SMD && dp->d_flags & D_BADSECT &&
18561f28255Scgd 	    totsize == 0) {
18661f28255Scgd 		badsecttable = dp->d_nsectors +
18761f28255Scgd 		    roundup(badsecttable, dp->d_nsectors);
188*81794312Sandvar 		threshold = howmany(spc, badsecttable);
18961f28255Scgd 	} else {
19061f28255Scgd 		badsecttable = 0;
191*81794312Sandvar 		threshold = 0;
19261f28255Scgd 	}
19361f28255Scgd 	/*
19461f28255Scgd 	 * If disk size was specified, recompute number of cylinders
19561f28255Scgd 	 * that may be used, and set badsecttable to any remaining
19661f28255Scgd 	 * fraction of the last cylinder.
19761f28255Scgd 	 */
19861f28255Scgd 	if (totsize != 0) {
19961f28255Scgd 		dp->d_ncylinders = howmany(totsize, spc);
20061f28255Scgd 		badsecttable = spc * dp->d_ncylinders - totsize;
20161f28255Scgd 	}
20261f28255Scgd 
20361f28255Scgd 	/*
20461f28255Scgd 	 * Figure out if disk is large enough for
20561f28255Scgd 	 * expanded swap area and 'd', 'e', and 'f'
20661f28255Scgd 	 * partitions.  Otherwise, use smaller defaults
20761f28255Scgd 	 * based on RK07.
20861f28255Scgd 	 */
20961f28255Scgd 	for (def = 0; def < NDEFAULTS; def++) {
21061f28255Scgd 		curcyl = 0;
21161f28255Scgd 		for (part = PART('a'); part < NPARTITIONS; part++)
21261f28255Scgd 			curcyl += howmany(defpart[def][part], spc);
213*81794312Sandvar 		if (curcyl < dp->d_ncylinders - threshold)
21461f28255Scgd 			break;
21561f28255Scgd 	}
21661f28255Scgd 	if (def >= NDEFAULTS) {
21761f28255Scgd 		fprintf(stderr, "%s: disk too small, calculate by hand\n",
21861f28255Scgd 			*argv);
2191375baa2Slukem 		exit(1);
22061f28255Scgd 	}
22161f28255Scgd 
22261f28255Scgd 	/*
22361f28255Scgd 	 * Calculate number of cylinders allocated to each disk
22461f28255Scgd 	 * partition.  We may waste a bit of space here, but it's
22561f28255Scgd 	 * in the interest of (very backward) compatibility
22661f28255Scgd 	 * (for mixed disk systems).
22761f28255Scgd 	 */
22861f28255Scgd 	for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
22961f28255Scgd 		numcyls[part] = 0;
23061f28255Scgd 		if (defpart[def][part] != 0) {
23161f28255Scgd 			numcyls[part] = howmany(defpart[def][part], spc);
23261f28255Scgd 			curcyl += numcyls[part];
23361f28255Scgd 		}
23461f28255Scgd 	}
23561f28255Scgd 	numcyls[PART('f')] = dp->d_ncylinders - curcyl;
23661f28255Scgd 	numcyls[PART('g')] =
23761f28255Scgd 		numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')];
23861f28255Scgd 	numcyls[PART('c')] = dp->d_ncylinders;
23961f28255Scgd 	defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable;
24061f28255Scgd 	defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
24161f28255Scgd 	defpart[def][PART('c')] = numcyls[PART('c')] * spc;
24261f28255Scgd #ifndef for_now
24361f28255Scgd 	if (totsize || !pflag)
24461f28255Scgd #else
24561f28255Scgd 	if (totsize)
24661f28255Scgd #endif
24761f28255Scgd 		defpart[def][PART('c')] -= badsecttable;
24861f28255Scgd 
24961f28255Scgd 	/*
25061f28255Scgd 	 * Calculate starting cylinder number for each partition.
25161f28255Scgd 	 * Note the 'h' partition is physically located before the
25261f28255Scgd 	 * 'g' or 'd' partition.  This is reflected in the layout
25361f28255Scgd 	 * arrays defined above.
25461f28255Scgd 	 */
25561f28255Scgd 	for (layout = 0; layout < NLAYOUTS; layout++) {
25661f28255Scgd 		curcyl = 0;
25761f28255Scgd 		for (lp = layouts[layout]; *lp != 0; lp++) {
25861f28255Scgd 			startcyl[PART(*lp)] = curcyl;
25961f28255Scgd 			curcyl += numcyls[PART(*lp)];
26061f28255Scgd 		}
26161f28255Scgd 	}
26261f28255Scgd 
26361f28255Scgd 	if (pflag) {
26461f28255Scgd 		printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS);
26561f28255Scgd 		for (part = PART('a'); part < NPARTITIONS; part++) {
26661f28255Scgd 			if (numcyls[part] == 0) {
26761f28255Scgd 				printf("\t0,\t0,\n");
26861f28255Scgd 				continue;
26961f28255Scgd 			}
27052e8eb13Schristos 			if (dp->d_type != DKTYPE_MSCP) {
27161f28255Scgd 			       printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n",
27261f28255Scgd 					defpart[def][part], startcyl[part],
27361f28255Scgd 					'A' + part, startcyl[part],
27461f28255Scgd 					startcyl[part] + numcyls[part] - 1);
27561f28255Scgd 				continue;
27661f28255Scgd 			}
27761f28255Scgd 			printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n",
27861f28255Scgd 				defpart[def][part], spc * startcyl[part],
27961f28255Scgd 				'A' + part, spc * startcyl[part],
28061f28255Scgd 				spc * startcyl[part] + defpart[def][part] - 1);
28161f28255Scgd 		}
28261f28255Scgd 		exit(0);
28361f28255Scgd 	}
28461f28255Scgd 	if (dflag) {
28561f28255Scgd 		int nparts;
28661f28255Scgd 
28761f28255Scgd 		/*
28861f28255Scgd 		 * In case the disk is in the ``in-between'' range
28961f28255Scgd 		 * where the 'g' partition is smaller than the 'h'
29061f28255Scgd 		 * partition, reverse the frag sizes so the /usr partition
29161f28255Scgd 		 * is always set up with a frag size larger than the
29261f28255Scgd 		 * user's partition.
29361f28255Scgd 		 */
29461f28255Scgd 		if (defpart[def][PART('g')] < defpart[def][PART('h')]) {
29561f28255Scgd 			int temp;
29661f28255Scgd 
29761f28255Scgd 			temp = defparam[PART('h')].p_fsize;
29861f28255Scgd 			defparam[PART('h')].p_fsize =
29961f28255Scgd 				defparam[PART('g')].p_fsize;
30061f28255Scgd 			defparam[PART('g')].p_fsize = temp;
30161f28255Scgd 		}
30261f28255Scgd 		printf("%s:\\\n", dp->d_typename);
30361f28255Scgd 		printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
30461f28255Scgd 			dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
30561f28255Scgd 		if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
30661f28255Scgd 			printf("sc#%d:", dp->d_secpercyl);
30752e8eb13Schristos 		if (dp->d_type == DKTYPE_SMD && dp->d_flags & D_BADSECT)
30861f28255Scgd 			printf("sf:");
30961f28255Scgd 		printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]);
31061f28255Scgd 		for (part = NDDATA - 1; part >= 0; part--)
31161f28255Scgd 			if (dp->d_drivedata[part])
31261f28255Scgd 				break;
31361f28255Scgd 		for (j = 0; j <= part; j++)
31461f28255Scgd 			printf("d%d#%d:", j, dp->d_drivedata[j]);
31561f28255Scgd 		printf("\\\n");
31661f28255Scgd 		for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++)
31761f28255Scgd 			if (defpart[def][part] != 0)
31861f28255Scgd 				nparts++;
31961f28255Scgd 		for (part = PART('a'); part < NPARTITIONS; part++) {
32061f28255Scgd 			if (defpart[def][part] == 0)
32161f28255Scgd 				continue;
32261f28255Scgd 			printf("\t:p%c#%d:", 'a' + part, defpart[def][part]);
32361f28255Scgd 			printf("o%c#%d:b%c#%d:f%c#%d:",
32461f28255Scgd 			    'a' + part, spc * startcyl[part],
32561f28255Scgd 			    'a' + part,
32661f28255Scgd 			    defparam[part].p_frag * defparam[part].p_fsize,
32761f28255Scgd 			    'a' + part, defparam[part].p_fsize);
32861f28255Scgd 			if (defparam[part].p_fstype == FS_SWAP)
32961f28255Scgd 				printf("t%c=swap:", 'a' + part);
33061f28255Scgd 			nparts--;
33161f28255Scgd 			printf("%s\n", nparts > 0 ? "\\" : "");
33261f28255Scgd 		}
33361f28255Scgd #ifdef for_now
33461f28255Scgd 		defpart[def][PART('c')] -= badsecttable;
33561f28255Scgd 		part = PART('c');
33661f28255Scgd 		printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]);
33761f28255Scgd 		printf("o%c#%d:b%c#%d:f%c#%d:\n",
33861f28255Scgd 		    'a' + part, spc * startcyl[part],
33961f28255Scgd 		    'a' + part,
34061f28255Scgd 		    defparam[part].p_frag * defparam[part].p_fsize,
34161f28255Scgd 		    'a' + part, defparam[part].p_fsize);
34261f28255Scgd #endif
34361f28255Scgd 		exit(0);
34461f28255Scgd 	}
34561f28255Scgd 	printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
34661f28255Scgd 		dp->d_typename, dp->d_nsectors, dp->d_ntracks,
34761f28255Scgd 		dp->d_ncylinders);
34861f28255Scgd 	printf("\n    Partition\t   Size\t Offset\t   Range\n");
34961f28255Scgd 	for (part = PART('a'); part < NPARTITIONS; part++) {
35061f28255Scgd 		printf("\t%c\t", 'a' + part);
35161f28255Scgd 		if (numcyls[part] == 0) {
35261f28255Scgd 			printf(" unused\n");
35361f28255Scgd 			continue;
35461f28255Scgd 		}
35561f28255Scgd 		printf("%7d\t%7d\t%4d - %d%s\n",
35661f28255Scgd 			defpart[def][part], startcyl[part] * spc,
35761f28255Scgd 			startcyl[part], startcyl[part] + numcyls[part] - 1,
35861f28255Scgd 			defpart[def][part] % spc ? "*" : "");
35961f28255Scgd 	}
3601375baa2Slukem 	exit(0);
36161f28255Scgd }
36261f28255Scgd 
3635c1014c6Sjoerg static struct disklabel disk;
36461f28255Scgd 
3655c1014c6Sjoerg static struct	field {
366b2bb6949Sxtraeme 	const char	*f_name;
367b2bb6949Sxtraeme 	const char	*f_defaults;
368191b5f2eScgd 	u_int32_t	*f_location;
36961f28255Scgd } fields[] = {
37061f28255Scgd 	{ "sector size",		"512",	&disk.d_secsize },
371b2bb6949Sxtraeme 	{ "#sectors/track",		NULL,	&disk.d_nsectors },
372b2bb6949Sxtraeme 	{ "#tracks/cylinder",		NULL,	&disk.d_ntracks },
373b2bb6949Sxtraeme 	{ "#cylinders",			NULL,	&disk.d_ncylinders },
374b2bb6949Sxtraeme 	{ NULL, NULL, 0 },
37561f28255Scgd };
37661f28255Scgd 
3775c1014c6Sjoerg static struct disklabel *
promptfordisk(const char * name)3785c1014c6Sjoerg promptfordisk(const char *name)
37961f28255Scgd {
3801375baa2Slukem 	struct disklabel *dp = &disk;
3811375baa2Slukem 	struct field *fp;
3821375baa2Slukem 	int i;
38348ba78aaSmycroft 	const char *const *tp;
38448ba78aaSmycroft 	char buf[BUFSIZ], *cp;
38561f28255Scgd 
38661f28255Scgd 	strncpy(dp->d_typename, name, sizeof(dp->d_typename));
38761f28255Scgd 	fprintf(stderr,
38861f28255Scgd 		"%s: unknown disk type, want to supply parameters (y/n)? ",
38961f28255Scgd 		name);
3901375baa2Slukem 	if ((fgets(buf, BUFSIZ, stdin) == NULL) || buf[0] != 'y')
39161f28255Scgd 		return ((struct disklabel *)0);
39261f28255Scgd 	for (;;) {
39361f28255Scgd 		fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
3941375baa2Slukem 		if (fgets(buf, BUFSIZ, stdin) == NULL)
3951375baa2Slukem 			return ((struct disklabel *)0);
3961375baa2Slukem 		if ((cp = strchr(buf, '\n')) != NULL)
3971375baa2Slukem 			*cp = '\0';
3981375baa2Slukem 		if (buf[0] == '\0') {
39961f28255Scgd 			dp->d_type = 1;
40061f28255Scgd 			break;
4016aeb76c1Smikel 		}
4026aeb76c1Smikel 		if ((i = gettype(buf, dktypenames)) >= 0) {
4036aeb76c1Smikel 			dp->d_type = i;
4046aeb76c1Smikel 			break;
4056aeb76c1Smikel 		}
40661f28255Scgd 		fprintf(stderr, "%s: unrecognized controller type\n", buf);
4071375baa2Slukem 		fprintf(stderr, "use one of:\n");
40861f28255Scgd 		for (tp = dktypenames; *tp; tp++)
4096aeb76c1Smikel 			if (strchr(*tp, ' ') == 0)
41061f28255Scgd 				fprintf(stderr, "\t%s\n", *tp);
41161f28255Scgd 	}
41261f28255Scgd gettype:
41361f28255Scgd 	dp->d_flags = 0;
41461f28255Scgd 	fprintf(stderr, "type (winchester|removable|simulated)? ");
4151375baa2Slukem 	if (fgets(buf, BUFSIZ, stdin) == NULL)
4161375baa2Slukem 		return ((struct disklabel *)0);
4171375baa2Slukem 	if ((cp = strchr(buf, '\n')) != NULL)
4181375baa2Slukem 		*cp = '\0';
4191375baa2Slukem 	if (buf[0] == '\0')
4201375baa2Slukem 		goto gettype;
4211375baa2Slukem 	switch (buf[0]) {
4221375baa2Slukem 	case 'r':
42361f28255Scgd 		dp->d_flags = D_REMOVABLE;
4241375baa2Slukem 		break;
4251375baa2Slukem 	case 's':
42661f28255Scgd 		dp->d_flags = D_RAMDISK;
4271375baa2Slukem 		break;
4281375baa2Slukem 	case 'w':
4291375baa2Slukem 		break;
4301375baa2Slukem 	default:
43161f28255Scgd 		fprintf(stderr, "%s: bad disk type\n", buf);
4321375baa2Slukem 		/* FALLTHROUGH */
4331375baa2Slukem 	case '\0':
43461f28255Scgd 		goto gettype;
43561f28255Scgd 	}
43661f28255Scgd 	fprintf(stderr, "(type <cr> to get default value, if only one)\n");
43752e8eb13Schristos 	if (dp->d_type == DKTYPE_SMD) {
4381375baa2Slukem 		fprintf(stderr,
4391375baa2Slukem 		    "Do '%s' disks support bad144 bad block forwarding (yes)? ",
44061f28255Scgd 		    dp->d_typename);
4411375baa2Slukem 		if (fgets(buf, BUFSIZ, stdin) == NULL)
4421375baa2Slukem 			return ((struct disklabel *)0);
4431375baa2Slukem 		if (buf[0] != 'n')
44461f28255Scgd 			dp->d_flags |= D_BADSECT;
4451375baa2Slukem 	}
44661f28255Scgd 	for (fp = fields; fp->f_name != NULL; fp++) {
44761f28255Scgd again:
44861f28255Scgd 		fprintf(stderr, "%s ", fp->f_name);
44961f28255Scgd 		if (fp->f_defaults != NULL)
45061f28255Scgd 			fprintf(stderr, "(%s)", fp->f_defaults);
45161f28255Scgd 		fprintf(stderr, "? ");
4521375baa2Slukem 		if (fgets(buf, BUFSIZ, stdin) == NULL)
4531375baa2Slukem 			return ((struct disklabel *)0);
4541375baa2Slukem 		if ((cp = strchr(buf, '\n')) != NULL)
4551375baa2Slukem 			*cp = '\0';
4561375baa2Slukem 		cp = buf;
45761f28255Scgd 		if (*cp == '\0') {
45861f28255Scgd 			if (fp->f_defaults == NULL) {
45961f28255Scgd 				fprintf(stderr, "no default value\n");
46061f28255Scgd 				goto again;
46161f28255Scgd 			}
462b2bb6949Sxtraeme 			/* XXX __UNCONST */
463b2bb6949Sxtraeme 			cp = __UNCONST(fp->f_defaults);
46461f28255Scgd 		}
46561f28255Scgd 		*fp->f_location = atol(cp);
46661f28255Scgd 		if (*fp->f_location == 0) {
46761f28255Scgd 			fprintf(stderr, "%s: bad value\n", cp);
46861f28255Scgd 			goto again;
46961f28255Scgd 		}
47061f28255Scgd 	}
47161f28255Scgd 	fprintf(stderr, "sectors/cylinder (%d)? ",
47261f28255Scgd 	    dp->d_nsectors * dp->d_ntracks);
4731375baa2Slukem 	if (fgets(buf, BUFSIZ, stdin) == NULL)
4741375baa2Slukem 		return ((struct disklabel *)0);
4751375baa2Slukem 	if ((cp = strchr(buf, '\n')) != NULL)
4761375baa2Slukem 		*cp = '\0';
47761f28255Scgd 	if (buf[0] == 0)
47861f28255Scgd 		dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
47961f28255Scgd 	else
48061f28255Scgd 		dp->d_secpercyl = atol(buf);
48161f28255Scgd 	fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
48261f28255Scgd 	for (i = 0; i < NDDATA; i++) {
48361f28255Scgd 		fprintf(stderr, "d%d? ", i);
4841375baa2Slukem 		if (fgets(buf, BUFSIZ, stdin) == NULL)
4851375baa2Slukem 			return ((struct disklabel *)0);
4861375baa2Slukem 		if ((cp = strchr(buf, '\n')) != NULL)
4871375baa2Slukem 			*cp = '\0';
48861f28255Scgd 		if (buf[0] == 0)
48961f28255Scgd 			break;
49061f28255Scgd 		dp->d_drivedata[i] = atol(buf);
49161f28255Scgd 	}
49261f28255Scgd 	return (dp);
49361f28255Scgd }
49461f28255Scgd 
4955c1014c6Sjoerg static int
gettype(const char * t,const char * const * names)4965c1014c6Sjoerg gettype(const char *t, const char *const *names)
49761f28255Scgd {
49848ba78aaSmycroft 	const char *const *nm;
49961f28255Scgd 
50061f28255Scgd 	for (nm = names; *nm; nm++)
5011375baa2Slukem 		if (strcasecmp(t, *nm) == 0)
50261f28255Scgd 			return (nm - names);
503cfe7f80fSdsl 	if (isdigit((unsigned char)*t))
50461f28255Scgd 		return (atoi(t));
50561f28255Scgd 	return (-1);
50661f28255Scgd }
50761f28255Scgd 
5085c1014c6Sjoerg static void
usage(void)5091375baa2Slukem usage(void)
51061f28255Scgd {
511b635f565Sjmmv 	(void)fprintf(stderr, "usage: diskpart [-dp] [-s size] disk-type\n");
5121375baa2Slukem 	exit(1);
51361f28255Scgd }
514