Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / diskpart / diskpart.c
blobae7e2be155a1106b6297457e9bd4906ced46ad03
1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
33 The Regents of the University of California. All rights reserved.");
34 #endif /* not lint */
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "from: @(#)diskpart.c 8.3 (Berkeley) 11/30/94";
39 #else
40 __RCSID("$NetBSD: diskpart.c,v 1.17 2008/07/21 13:36:58 lukem Exp $");
41 #endif
42 #endif /* not lint */
45 * Program to calculate standard disk partition sizes.
47 #include <sys/param.h>
48 #define DKTYPENAMES
49 #include <sys/disklabel.h>
51 #include <ctype.h>
52 #include <disktab.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
59 #define for_now /* show all of `c' partition for disklabel */
60 #define NPARTITIONS 8
61 #define PART(x) (x - 'a')
64 * Default partition sizes, where they exist.
66 #define NDEFAULTS 4
67 int defpart[NDEFAULTS][NPARTITIONS] = {
68 { 15884, 66880, 0, 15884, 307200, 0, 0, 291346 }, /* ~ 356+ Mbytes */
69 { 15884, 33440, 0, 15884, 55936, 0, 0, 291346 }, /* ~ 206-355 Mbytes */
70 { 15884, 33440, 0, 15884, 55936, 0, 0, 0 }, /* ~ 61-205 Mbytes */
71 { 15884, 10032, 0, 15884, 0, 0, 0, 0 }, /* ~ 20-60 Mbytes */
75 * Each array defines a layout for a disk;
76 * that is, the collection of partitions totally
77 * covers the physical space on a disk.
79 #define NLAYOUTS 3
80 char layouts[NLAYOUTS][NPARTITIONS] = {
81 { 'a', 'b', 'h', 'g' },
82 { 'a', 'b', 'h', 'd', 'e', 'f' },
83 { 'c' },
87 * Default disk block and disk block fragment
88 * sizes for each file system. Those file systems
89 * with zero block and frag sizes are special cases
90 * (e.g. swap areas or for access to the entire device).
92 struct partition defparam[NPARTITIONS] = {
93 { 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, }, /* a */
94 { 0, 0, { 1024 }, FS_SWAP, 8, { 0 }, }, /* b */
95 { 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, }, /* c */
96 { 0, 0, { 512 }, FS_UNUSED, 8, { 0 }, }, /* d */
97 { 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, }, /* e */
98 { 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, }, /* f */
99 { 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, }, /* g */
100 { 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, } /* h */
104 * Each disk has some space reserved for a bad sector
105 * forwarding table. DEC standard 144 uses the first
106 * 5 even numbered sectors in the last track of the
107 * last cylinder for replicated storage of the bad sector
108 * table; another 126 sectors past this is needed as a
109 * pool of replacement sectors.
111 int badsecttable = 126; /* # sectors */
113 int pflag; /* print device driver partition tables */
114 int dflag; /* print disktab entry */
116 int gettype __P((const char *, const char *const *));
117 int main __P((int, char **));
118 struct disklabel *promptfordisk __P((const char *));
119 void usage __P((void));
122 main(argc, argv)
123 int argc;
124 char *argv[];
126 struct disklabel *dp;
127 int spc, def, part, layout, j, ch;
128 uint32_t curcyl;
129 int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
130 off_t totsize = 0;
131 const char *tyname;
132 char *lp;
134 while ((ch = getopt(argc, argv, "pds:")) != -1) {
135 switch (ch) {
136 case 'd':
137 dflag++;
138 break;
140 case 'p':
141 pflag++;
142 break;
144 case 's':
145 totsize = strtoul(optarg, &lp, 10);
146 if (*lp != '\0')
147 usage();
148 break;
150 case '?':
151 default:
152 usage();
153 /* NOTREACHED */
156 argc -= optind;
157 argv += optind;
159 if (argc != 1) {
160 usage();
161 /* NOTREACHED */
164 dp = getdiskbyname(*argv);
165 if (dp == NULL) {
166 if (isatty(0))
167 dp = promptfordisk(*argv);
168 if (dp == NULL) {
169 fprintf(stderr, "%s: unknown disk type\n", *argv);
170 exit(1);
173 if (dp->d_flags & D_REMOVABLE)
174 tyname = "removable";
175 else if (dp->d_flags & D_RAMDISK)
176 tyname = "simulated";
177 else
178 tyname = "winchester";
179 spc = dp->d_secpercyl;
181 * Bad sector table contains one track for the replicated
182 * copies of the table and enough full tracks preceding
183 * the last track to hold the pool of free blocks to which
184 * bad sectors are mapped.
185 * If disk size was specified explicitly, use specified size.
187 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT &&
188 totsize == 0) {
189 badsecttable = dp->d_nsectors +
190 roundup(badsecttable, dp->d_nsectors);
191 threshhold = howmany(spc, badsecttable);
192 } else {
193 badsecttable = 0;
194 threshhold = 0;
197 * If disk size was specified, recompute number of cylinders
198 * that may be used, and set badsecttable to any remaining
199 * fraction of the last cylinder.
201 if (totsize != 0) {
202 dp->d_ncylinders = howmany(totsize, spc);
203 badsecttable = spc * dp->d_ncylinders - totsize;
207 * Figure out if disk is large enough for
208 * expanded swap area and 'd', 'e', and 'f'
209 * partitions. Otherwise, use smaller defaults
210 * based on RK07.
212 for (def = 0; def < NDEFAULTS; def++) {
213 curcyl = 0;
214 for (part = PART('a'); part < NPARTITIONS; part++)
215 curcyl += howmany(defpart[def][part], spc);
216 if (curcyl < dp->d_ncylinders - threshhold)
217 break;
219 if (def >= NDEFAULTS) {
220 fprintf(stderr, "%s: disk too small, calculate by hand\n",
221 *argv);
222 exit(1);
226 * Calculate number of cylinders allocated to each disk
227 * partition. We may waste a bit of space here, but it's
228 * in the interest of (very backward) compatibility
229 * (for mixed disk systems).
231 for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
232 numcyls[part] = 0;
233 if (defpart[def][part] != 0) {
234 numcyls[part] = howmany(defpart[def][part], spc);
235 curcyl += numcyls[part];
238 numcyls[PART('f')] = dp->d_ncylinders - curcyl;
239 numcyls[PART('g')] =
240 numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')];
241 numcyls[PART('c')] = dp->d_ncylinders;
242 defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable;
243 defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
244 defpart[def][PART('c')] = numcyls[PART('c')] * spc;
245 #ifndef for_now
246 if (totsize || !pflag)
247 #else
248 if (totsize)
249 #endif
250 defpart[def][PART('c')] -= badsecttable;
253 * Calculate starting cylinder number for each partition.
254 * Note the 'h' partition is physically located before the
255 * 'g' or 'd' partition. This is reflected in the layout
256 * arrays defined above.
258 for (layout = 0; layout < NLAYOUTS; layout++) {
259 curcyl = 0;
260 for (lp = layouts[layout]; *lp != 0; lp++) {
261 startcyl[PART(*lp)] = curcyl;
262 curcyl += numcyls[PART(*lp)];
266 if (pflag) {
267 printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS);
268 for (part = PART('a'); part < NPARTITIONS; part++) {
269 if (numcyls[part] == 0) {
270 printf("\t0,\t0,\n");
271 continue;
273 if (dp->d_type != DTYPE_MSCP) {
274 printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n",
275 defpart[def][part], startcyl[part],
276 'A' + part, startcyl[part],
277 startcyl[part] + numcyls[part] - 1);
278 continue;
280 printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n",
281 defpart[def][part], spc * startcyl[part],
282 'A' + part, spc * startcyl[part],
283 spc * startcyl[part] + defpart[def][part] - 1);
285 exit(0);
287 if (dflag) {
288 int nparts;
291 * In case the disk is in the ``in-between'' range
292 * where the 'g' partition is smaller than the 'h'
293 * partition, reverse the frag sizes so the /usr partition
294 * is always set up with a frag size larger than the
295 * user's partition.
297 if (defpart[def][PART('g')] < defpart[def][PART('h')]) {
298 int temp;
300 temp = defparam[PART('h')].p_fsize;
301 defparam[PART('h')].p_fsize =
302 defparam[PART('g')].p_fsize;
303 defparam[PART('g')].p_fsize = temp;
305 printf("%s:\\\n", dp->d_typename);
306 printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
307 dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
308 if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
309 printf("sc#%d:", dp->d_secpercyl);
310 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT)
311 printf("sf:");
312 printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]);
313 for (part = NDDATA - 1; part >= 0; part--)
314 if (dp->d_drivedata[part])
315 break;
316 for (j = 0; j <= part; j++)
317 printf("d%d#%d:", j, dp->d_drivedata[j]);
318 printf("\\\n");
319 for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++)
320 if (defpart[def][part] != 0)
321 nparts++;
322 for (part = PART('a'); part < NPARTITIONS; part++) {
323 if (defpart[def][part] == 0)
324 continue;
325 printf("\t:p%c#%d:", 'a' + part, defpart[def][part]);
326 printf("o%c#%d:b%c#%d:f%c#%d:",
327 'a' + part, spc * startcyl[part],
328 'a' + part,
329 defparam[part].p_frag * defparam[part].p_fsize,
330 'a' + part, defparam[part].p_fsize);
331 if (defparam[part].p_fstype == FS_SWAP)
332 printf("t%c=swap:", 'a' + part);
333 nparts--;
334 printf("%s\n", nparts > 0 ? "\\" : "");
336 #ifdef for_now
337 defpart[def][PART('c')] -= badsecttable;
338 part = PART('c');
339 printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]);
340 printf("o%c#%d:b%c#%d:f%c#%d:\n",
341 'a' + part, spc * startcyl[part],
342 'a' + part,
343 defparam[part].p_frag * defparam[part].p_fsize,
344 'a' + part, defparam[part].p_fsize);
345 #endif
346 exit(0);
348 printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
349 dp->d_typename, dp->d_nsectors, dp->d_ntracks,
350 dp->d_ncylinders);
351 printf("\n Partition\t Size\t Offset\t Range\n");
352 for (part = PART('a'); part < NPARTITIONS; part++) {
353 printf("\t%c\t", 'a' + part);
354 if (numcyls[part] == 0) {
355 printf(" unused\n");
356 continue;
358 printf("%7d\t%7d\t%4d - %d%s\n",
359 defpart[def][part], startcyl[part] * spc,
360 startcyl[part], startcyl[part] + numcyls[part] - 1,
361 defpart[def][part] % spc ? "*" : "");
363 exit(0);
366 struct disklabel disk;
368 struct field {
369 const char *f_name;
370 const char *f_defaults;
371 u_int32_t *f_location;
372 } fields[] = {
373 { "sector size", "512", &disk.d_secsize },
374 { "#sectors/track", NULL, &disk.d_nsectors },
375 { "#tracks/cylinder", NULL, &disk.d_ntracks },
376 { "#cylinders", NULL, &disk.d_ncylinders },
377 { NULL, NULL, 0 },
380 struct disklabel *
381 promptfordisk(name)
382 const char *name;
384 struct disklabel *dp = &disk;
385 struct field *fp;
386 int i;
387 const char *const *tp;
388 char buf[BUFSIZ], *cp;
390 strncpy(dp->d_typename, name, sizeof(dp->d_typename));
391 fprintf(stderr,
392 "%s: unknown disk type, want to supply parameters (y/n)? ",
393 name);
394 if ((fgets(buf, BUFSIZ, stdin) == NULL) || buf[0] != 'y')
395 return ((struct disklabel *)0);
396 for (;;) {
397 fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
398 if (fgets(buf, BUFSIZ, stdin) == NULL)
399 return ((struct disklabel *)0);
400 if ((cp = strchr(buf, '\n')) != NULL)
401 *cp = '\0';
402 if (buf[0] == '\0') {
403 dp->d_type = 1;
404 break;
406 if ((i = gettype(buf, dktypenames)) >= 0) {
407 dp->d_type = i;
408 break;
410 fprintf(stderr, "%s: unrecognized controller type\n", buf);
411 fprintf(stderr, "use one of:\n");
412 for (tp = dktypenames; *tp; tp++)
413 if (strchr(*tp, ' ') == 0)
414 fprintf(stderr, "\t%s\n", *tp);
416 gettype:
417 dp->d_flags = 0;
418 fprintf(stderr, "type (winchester|removable|simulated)? ");
419 if (fgets(buf, BUFSIZ, stdin) == NULL)
420 return ((struct disklabel *)0);
421 if ((cp = strchr(buf, '\n')) != NULL)
422 *cp = '\0';
423 if (buf[0] == '\0')
424 goto gettype;
425 switch (buf[0]) {
426 case 'r':
427 dp->d_flags = D_REMOVABLE;
428 break;
429 case 's':
430 dp->d_flags = D_RAMDISK;
431 break;
432 case 'w':
433 break;
434 default:
435 fprintf(stderr, "%s: bad disk type\n", buf);
436 /* FALLTHROUGH */
437 case '\0':
438 goto gettype;
440 fprintf(stderr, "(type <cr> to get default value, if only one)\n");
441 if (dp->d_type == DTYPE_SMD) {
442 fprintf(stderr,
443 "Do '%s' disks support bad144 bad block forwarding (yes)? ",
444 dp->d_typename);
445 if (fgets(buf, BUFSIZ, stdin) == NULL)
446 return ((struct disklabel *)0);
447 if (buf[0] != 'n')
448 dp->d_flags |= D_BADSECT;
450 for (fp = fields; fp->f_name != NULL; fp++) {
451 again:
452 fprintf(stderr, "%s ", fp->f_name);
453 if (fp->f_defaults != NULL)
454 fprintf(stderr, "(%s)", fp->f_defaults);
455 fprintf(stderr, "? ");
456 if (fgets(buf, BUFSIZ, stdin) == NULL)
457 return ((struct disklabel *)0);
458 if ((cp = strchr(buf, '\n')) != NULL)
459 *cp = '\0';
460 cp = buf;
461 if (*cp == '\0') {
462 if (fp->f_defaults == NULL) {
463 fprintf(stderr, "no default value\n");
464 goto again;
466 /* XXX __UNCONST */
467 cp = __UNCONST(fp->f_defaults);
469 *fp->f_location = atol(cp);
470 if (*fp->f_location == 0) {
471 fprintf(stderr, "%s: bad value\n", cp);
472 goto again;
475 fprintf(stderr, "sectors/cylinder (%d)? ",
476 dp->d_nsectors * dp->d_ntracks);
477 if (fgets(buf, BUFSIZ, stdin) == NULL)
478 return ((struct disklabel *)0);
479 if ((cp = strchr(buf, '\n')) != NULL)
480 *cp = '\0';
481 if (buf[0] == 0)
482 dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
483 else
484 dp->d_secpercyl = atol(buf);
485 fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
486 for (i = 0; i < NDDATA; i++) {
487 fprintf(stderr, "d%d? ", i);
488 if (fgets(buf, BUFSIZ, stdin) == NULL)
489 return ((struct disklabel *)0);
490 if ((cp = strchr(buf, '\n')) != NULL)
491 *cp = '\0';
492 if (buf[0] == 0)
493 break;
494 dp->d_drivedata[i] = atol(buf);
496 return (dp);
500 gettype(t, names)
501 const char *t;
502 const char *const *names;
504 const char *const *nm;
506 for (nm = names; *nm; nm++)
507 if (strcasecmp(t, *nm) == 0)
508 return (nm - names);
509 if (isdigit((unsigned char)*t))
510 return (atoi(t));
511 return (-1);
514 void
515 usage(void)
517 (void)fprintf(stderr, "usage: diskpart [-dp] [-s size] disk-type\n");
518 exit(1);