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