xref: /original-bsd/usr.sbin/diskpart/diskpart.c (revision cf2e4d33)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1983, 1988, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)diskpart.c	8.2 (Berkeley) 08/11/94";
16 #endif /* not lint */
17 
18 /*
19  * Program to calculate standard disk partition sizes.
20  */
21 #include <sys/param.h>
22 #define DKTYPENAMES
23 #include <sys/disklabel.h>
24 
25 #include <stdio.h>
26 #include <ctype.h>
27 
28 #define	for_now			/* show all of `c' partition for disklabel */
29 #define	NPARTITIONS	8
30 #define	PART(x)		(x - 'a')
31 
32 /*
33  * Default partition sizes, where they exist.
34  */
35 #define	NDEFAULTS	4
36 int	defpart[NDEFAULTS][NPARTITIONS] = {
37    { 15884, 66880, 0, 15884, 307200, 0, 0, 291346 },	/* ~ 356+ Mbytes */
38    { 15884, 33440, 0, 15884, 55936, 0, 0, 291346 },	/* ~ 206-355 Mbytes */
39    { 15884, 33440, 0, 15884, 55936, 0, 0, 0 },		/* ~ 61-205 Mbytes */
40    { 15884, 10032, 0, 15884, 0, 0, 0, 0 },		/* ~ 20-60 Mbytes */
41 };
42 
43 /*
44  * Each array defines a layout for a disk;
45  * that is, the collection of partitions totally
46  * covers the physical space on a disk.
47  */
48 #define	NLAYOUTS	3
49 char	layouts[NLAYOUTS][NPARTITIONS] = {
50    { 'a', 'b', 'h', 'g' },
51    { 'a', 'b', 'h', 'd', 'e', 'f' },
52    { 'c' },
53 };
54 
55 /*
56  * Default disk block and disk block fragment
57  * sizes for each file system.  Those file systems
58  * with zero block and frag sizes are special cases
59  * (e.g. swap areas or for access to the entire device).
60  */
61 struct	partition defparam[NPARTITIONS] = {
62 	{ 0, 0, 1024, FS_UNUSED, 8, 0 },		/* a */
63 	{ 0, 0, 1024, FS_SWAP,   8, 0 },		/* b */
64 	{ 0, 0, 1024, FS_UNUSED, 8, 0 },		/* c */
65 	{ 0, 0,  512, FS_UNUSED, 8, 0 },		/* d */
66 	{ 0, 0, 1024, FS_UNUSED, 8, 0 },		/* e */
67 	{ 0, 0, 1024, FS_UNUSED, 8, 0 },		/* f */
68 	{ 0, 0, 1024, FS_UNUSED, 8, 0 },		/* g */
69 	{ 0, 0, 1024, FS_UNUSED, 8, 0 }			/* h */
70 };
71 
72 /*
73  * Each disk has some space reserved for a bad sector
74  * forwarding table.  DEC standard 144 uses the first
75  * 5 even numbered sectors in the last track of the
76  * last cylinder for replicated storage of the bad sector
77  * table; another 126 sectors past this is needed as a
78  * pool of replacement sectors.
79  */
80 int	badsecttable = 126;	/* # sectors */
81 
82 int	pflag;			/* print device driver partition tables */
83 int	dflag;			/* print disktab entry */
84 
85 struct	disklabel *promptfordisk();
86 
87 main(argc, argv)
88 	int argc;
89 	char *argv[];
90 {
91 	struct disklabel *dp;
92 	register int curcyl, spc, def, part, layout, j;
93 	int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
94 	int totsize = 0;
95 	char *lp, *tyname;
96 
97 	argc--, argv++;
98 	if (argc < 1) {
99 		fprintf(stderr,
100 		    "usage: disktab [ -p ] [ -d ] [ -s size ] disk-type\n");
101 		exit(1);
102 	}
103 	if (argc > 0 && strcmp(*argv, "-p") == 0) {
104 		pflag++;
105 		argc--, argv++;
106 	}
107 	if (argc > 0 && strcmp(*argv, "-d") == 0) {
108 		dflag++;
109 		argc--, argv++;
110 	}
111 	if (argc > 1 && strcmp(*argv, "-s") == 0) {
112 		totsize = atoi(argv[1]);
113 		argc += 2, argv += 2;
114 	}
115 	dp = getdiskbyname(*argv);
116 	if (dp == NULL) {
117 		if (isatty(0))
118 			dp = promptfordisk(*argv);
119 		if (dp == NULL) {
120 			fprintf(stderr, "%s: unknown disk type\n", *argv);
121 			exit(2);
122 		}
123 	} else {
124 		if (dp->d_flags & D_REMOVABLE)
125 			tyname = "removable";
126 		else if (dp->d_flags & D_RAMDISK)
127 			tyname = "simulated";
128 		else
129 			tyname = "winchester";
130 	}
131 	spc = dp->d_secpercyl;
132 	/*
133 	 * Bad sector table contains one track for the replicated
134 	 * copies of the table and enough full tracks preceding
135 	 * the last track to hold the pool of free blocks to which
136 	 * bad sectors are mapped.
137 	 * If disk size was specified explicitly, use specified size.
138 	 */
139 	if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT &&
140 	    totsize == 0) {
141 		badsecttable = dp->d_nsectors +
142 		    roundup(badsecttable, dp->d_nsectors);
143 		threshhold = howmany(spc, badsecttable);
144 	} else {
145 		badsecttable = 0;
146 		threshhold = 0;
147 	}
148 	/*
149 	 * If disk size was specified, recompute number of cylinders
150 	 * that may be used, and set badsecttable to any remaining
151 	 * fraction of the last cylinder.
152 	 */
153 	if (totsize != 0) {
154 		dp->d_ncylinders = howmany(totsize, spc);
155 		badsecttable = spc * dp->d_ncylinders - totsize;
156 	}
157 
158 	/*
159 	 * Figure out if disk is large enough for
160 	 * expanded swap area and 'd', 'e', and 'f'
161 	 * partitions.  Otherwise, use smaller defaults
162 	 * based on RK07.
163 	 */
164 	for (def = 0; def < NDEFAULTS; def++) {
165 		curcyl = 0;
166 		for (part = PART('a'); part < NPARTITIONS; part++)
167 			curcyl += howmany(defpart[def][part], spc);
168 		if (curcyl < dp->d_ncylinders - threshhold)
169 			break;
170 	}
171 	if (def >= NDEFAULTS) {
172 		fprintf(stderr, "%s: disk too small, calculate by hand\n",
173 			*argv);
174 		exit(3);
175 	}
176 
177 	/*
178 	 * Calculate number of cylinders allocated to each disk
179 	 * partition.  We may waste a bit of space here, but it's
180 	 * in the interest of (very backward) compatibility
181 	 * (for mixed disk systems).
182 	 */
183 	for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
184 		numcyls[part] = 0;
185 		if (defpart[def][part] != 0) {
186 			numcyls[part] = howmany(defpart[def][part], spc);
187 			curcyl += numcyls[part];
188 		}
189 	}
190 	numcyls[PART('f')] = dp->d_ncylinders - curcyl;
191 	numcyls[PART('g')] =
192 		numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')];
193 	numcyls[PART('c')] = dp->d_ncylinders;
194 	defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable;
195 	defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
196 	defpart[def][PART('c')] = numcyls[PART('c')] * spc;
197 #ifndef for_now
198 	if (totsize || !pflag)
199 #else
200 	if (totsize)
201 #endif
202 		defpart[def][PART('c')] -= badsecttable;
203 
204 	/*
205 	 * Calculate starting cylinder number for each partition.
206 	 * Note the 'h' partition is physically located before the
207 	 * 'g' or 'd' partition.  This is reflected in the layout
208 	 * arrays defined above.
209 	 */
210 	for (layout = 0; layout < NLAYOUTS; layout++) {
211 		curcyl = 0;
212 		for (lp = layouts[layout]; *lp != 0; lp++) {
213 			startcyl[PART(*lp)] = curcyl;
214 			curcyl += numcyls[PART(*lp)];
215 		}
216 	}
217 
218 	if (pflag) {
219 		printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS);
220 		for (part = PART('a'); part < NPARTITIONS; part++) {
221 			if (numcyls[part] == 0) {
222 				printf("\t0,\t0,\n");
223 				continue;
224 			}
225 			if (dp->d_type != DTYPE_MSCP) {
226 			       printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n",
227 					defpart[def][part], startcyl[part],
228 					'A' + part, startcyl[part],
229 					startcyl[part] + numcyls[part] - 1);
230 				continue;
231 			}
232 			printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n",
233 				defpart[def][part], spc * startcyl[part],
234 				'A' + part, spc * startcyl[part],
235 				spc * startcyl[part] + defpart[def][part] - 1);
236 		}
237 		exit(0);
238 	}
239 	if (dflag) {
240 		int nparts;
241 
242 		/*
243 		 * In case the disk is in the ``in-between'' range
244 		 * where the 'g' partition is smaller than the 'h'
245 		 * partition, reverse the frag sizes so the /usr partition
246 		 * is always set up with a frag size larger than the
247 		 * user's partition.
248 		 */
249 		if (defpart[def][PART('g')] < defpart[def][PART('h')]) {
250 			int temp;
251 
252 			temp = defparam[PART('h')].p_fsize;
253 			defparam[PART('h')].p_fsize =
254 				defparam[PART('g')].p_fsize;
255 			defparam[PART('g')].p_fsize = temp;
256 		}
257 		printf("%s:\\\n", dp->d_typename);
258 		printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
259 			dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
260 		if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
261 			printf("sc#%d:", dp->d_secpercyl);
262 		if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT)
263 			printf("sf:");
264 		printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]);
265 		for (part = NDDATA - 1; part >= 0; part--)
266 			if (dp->d_drivedata[part])
267 				break;
268 		for (j = 0; j <= part; j++)
269 			printf("d%d#%d:", j, dp->d_drivedata[j]);
270 		printf("\\\n");
271 		for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++)
272 			if (defpart[def][part] != 0)
273 				nparts++;
274 		for (part = PART('a'); part < NPARTITIONS; part++) {
275 			if (defpart[def][part] == 0)
276 				continue;
277 			printf("\t:p%c#%d:", 'a' + part, defpart[def][part]);
278 			printf("o%c#%d:b%c#%d:f%c#%d:",
279 			    'a' + part, spc * startcyl[part],
280 			    'a' + part,
281 			    defparam[part].p_frag * defparam[part].p_fsize,
282 			    'a' + part, defparam[part].p_fsize);
283 			if (defparam[part].p_fstype == FS_SWAP)
284 				printf("t%c=swap:", 'a' + part);
285 			nparts--;
286 			printf("%s\n", nparts > 0 ? "\\" : "");
287 		}
288 #ifdef for_now
289 		defpart[def][PART('c')] -= badsecttable;
290 		part = PART('c');
291 		printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]);
292 		printf("o%c#%d:b%c#%d:f%c#%d:\n",
293 		    'a' + part, spc * startcyl[part],
294 		    'a' + part,
295 		    defparam[part].p_frag * defparam[part].p_fsize,
296 		    'a' + part, defparam[part].p_fsize);
297 #endif
298 		exit(0);
299 	}
300 	printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
301 		dp->d_typename, dp->d_nsectors, dp->d_ntracks,
302 		dp->d_ncylinders);
303 	printf("\n    Partition\t   Size\t Offset\t   Range\n");
304 	for (part = PART('a'); part < NPARTITIONS; part++) {
305 		printf("\t%c\t", 'a' + part);
306 		if (numcyls[part] == 0) {
307 			printf(" unused\n");
308 			continue;
309 		}
310 		printf("%7d\t%7d\t%4d - %d%s\n",
311 			defpart[def][part], startcyl[part] * spc,
312 			startcyl[part], startcyl[part] + numcyls[part] - 1,
313 			defpart[def][part] % spc ? "*" : "");
314 	}
315 }
316 
317 struct disklabel disk;
318 
319 struct	field {
320 	char	*f_name;
321 	char	*f_defaults;
322 	u_long	*f_location;
323 } fields[] = {
324 	{ "sector size",		"512",	&disk.d_secsize },
325 	{ "#sectors/track",		0,	&disk.d_nsectors },
326 	{ "#tracks/cylinder",		0,	&disk.d_ntracks },
327 	{ "#cylinders",			0,	&disk.d_ncylinders },
328 	{ 0, 0, 0 },
329 };
330 
331 struct disklabel *
332 promptfordisk(name)
333 	char *name;
334 {
335 	register struct disklabel *dp = &disk;
336 	register struct field *fp;
337 	register i;
338 	char buf[BUFSIZ], **tp, *cp, *gets();
339 
340 	strncpy(dp->d_typename, name, sizeof(dp->d_typename));
341 	fprintf(stderr,
342 		"%s: unknown disk type, want to supply parameters (y/n)? ",
343 		name);
344 	(void) gets(buf);
345 	if (*buf != 'y')
346 		return ((struct disklabel *)0);
347 	for (;;) {
348 		fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
349 		(void) gets(buf);
350 		if (buf[0] == 0) {
351 			dp->d_type = 1;
352 			break;
353 		}
354 		if ((i = gettype(buf, dktypenames)) >= 0) {
355 			dp->d_type = i;
356 			break;
357 		}
358 		fprintf(stderr, "%s: unrecognized controller type\n", buf);
359 		fprintf(stderr, "use one of:\n", buf);
360 		for (tp = dktypenames; *tp; tp++)
361 			if (index(*tp, ' ') == 0)
362 				fprintf(stderr, "\t%s\n", *tp);
363 	}
364 gettype:
365 	dp->d_flags = 0;
366 	fprintf(stderr, "type (winchester|removable|simulated)? ");
367 	(void) gets(buf);
368 	if (strcmp(buf, "removable") == 0)
369 		dp->d_flags = D_REMOVABLE;
370 	else if (strcmp(buf, "simulated") == 0)
371 		dp->d_flags = D_RAMDISK;
372 	else if (strcmp(buf, "winchester")) {
373 		fprintf(stderr, "%s: bad disk type\n", buf);
374 		goto gettype;
375 	}
376 	strncpy(dp->d_typename, buf, sizeof(dp->d_typename));
377 	fprintf(stderr, "(type <cr> to get default value, if only one)\n");
378 	if (dp->d_type == DTYPE_SMD)
379 	   fprintf(stderr, "Do %ss support bad144 bad block forwarding (yes)? ",
380 		dp->d_typename);
381 	(void) gets(buf);
382 	if (*buf != 'n')
383 		dp->d_flags |= D_BADSECT;
384 	for (fp = fields; fp->f_name != NULL; fp++) {
385 again:
386 		fprintf(stderr, "%s ", fp->f_name);
387 		if (fp->f_defaults != NULL)
388 			fprintf(stderr, "(%s)", fp->f_defaults);
389 		fprintf(stderr, "? ");
390 		cp = gets(buf);
391 		if (*cp == '\0') {
392 			if (fp->f_defaults == NULL) {
393 				fprintf(stderr, "no default value\n");
394 				goto again;
395 			}
396 			cp = fp->f_defaults;
397 		}
398 		*fp->f_location = atol(cp);
399 		if (*fp->f_location == 0) {
400 			fprintf(stderr, "%s: bad value\n", cp);
401 			goto again;
402 		}
403 	}
404 	fprintf(stderr, "sectors/cylinder (%d)? ",
405 	    dp->d_nsectors * dp->d_ntracks);
406 	(void) gets(buf);
407 	if (buf[0] == 0)
408 		dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
409 	else
410 		dp->d_secpercyl = atol(buf);
411 	fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
412 	for (i = 0; i < NDDATA; i++) {
413 		fprintf(stderr, "d%d? ", i);
414 		(void) gets(buf);
415 		if (buf[0] == 0)
416 			break;
417 		dp->d_drivedata[i] = atol(buf);
418 	}
419 	return (dp);
420 }
421 
422 gettype(t, names)
423 	char *t;
424 	char **names;
425 {
426 	register char **nm;
427 
428 	for (nm = names; *nm; nm++)
429 		if (ustrcmp(t, *nm) == 0)
430 			return (nm - names);
431 	if (isdigit(*t))
432 		return (atoi(t));
433 	return (-1);
434 }
435 
436 ustrcmp(s1, s2)
437 	register char *s1, *s2;
438 {
439 #define	lower(c)	(islower(c) ? (c) : tolower(c))
440 
441 	for (; *s1; s1++, s2++) {
442 		if (*s1 == *s2)
443 			continue;
444 		if (isalpha(*s1) && isalpha(*s2) &&
445 		    lower(*s1) == lower(*s2))
446 			continue;
447 		return (*s2 - *s1);
448 	}
449 	return (0);
450 }
451