Style(9) cleanup.
[dragonfly.git] / usr.sbin / diskpart / diskpart.c
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.
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 $
36  * $DragonFly: src/usr.sbin/diskpart/Attic/diskpart.c,v 1.5 2004/03/20 17:46:47 cpressey Exp $
37  */
38
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
59 int     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
72 char    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  */
84 struct  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  */
103 int     badsecttable = 126;     /* # sectors */
104
105 int     pflag;                  /* print device driver partition tables */
106 int     dflag;                  /* print disktab entry */
107
108 struct  disklabel *promptfordisk();
109 static void usage(void);
110
111 int
112 main(int argc, char **argv)
113 {
114         struct disklabel *dp;
115         int curcyl, spc, def, part, layout, j;
116         int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
117         int totsize = 0;
118         char *lp, *tyname;
119         int ch;
120
121         while ((ch = getopt(argc, argv, "dps:")) != EOF)
122                 switch (ch) {
123                         case 'd':
124                                 dflag++;
125                                 break;
126                                 
127                         case 'p':
128                                 pflag++;
129                                 break;
130
131                         case 's':
132                                 totsize = atoi(optarg);
133                                 break;
134                 }
135         argc -= optind;
136         argv += optind;
137
138         if (dflag && pflag)
139                 usage();
140         if (argc < 1)
141                 usage();
142         dp = getdiskbyname(*argv);
143         if (dp == NULL) {
144                 if (isatty(0))
145                         dp = promptfordisk(*argv);
146                 if (dp == NULL)
147                         errx(2, "%s: unknown disk type", *argv);
148         } else {
149                 if (dp->d_flags & D_REMOVABLE)
150                         tyname = "removable";
151                 else if (dp->d_flags & D_RAMDISK)
152                         tyname = "simulated";
153                 else
154                         tyname = "winchester";
155         }
156         spc = dp->d_secpercyl;
157         /*
158          * Bad sector table contains one track for the replicated
159          * copies of the table and enough full tracks preceding
160          * the last track to hold the pool of free blocks to which
161          * bad sectors are mapped.
162          * If disk size was specified explicitly, use specified size.
163          */
164         if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT &&
165             totsize == 0) {
166                 badsecttable = dp->d_nsectors +
167                     roundup(badsecttable, dp->d_nsectors);
168                 threshhold = howmany(spc, badsecttable);
169         } else {
170                 badsecttable = 0;
171                 threshhold = 0;
172         }
173         /*
174          * If disk size was specified, recompute number of cylinders
175          * that may be used, and set badsecttable to any remaining
176          * fraction of the last cylinder.
177          */
178         if (totsize != 0) {
179                 dp->d_ncylinders = howmany(totsize, spc);
180                 badsecttable = spc * dp->d_ncylinders - totsize;
181         }
182
183         /*
184          * Figure out if disk is large enough for
185          * expanded swap area and 'd', 'e', and 'f'
186          * partitions.  Otherwise, use smaller defaults
187          * based on RK07.
188          */
189         for (def = 0; def < NDEFAULTS; def++) {
190                 curcyl = 0;
191                 for (part = PART('a'); part < NPARTITIONS; part++)
192                         curcyl += howmany(defpart[def][part], spc);
193                 if (curcyl < dp->d_ncylinders - threshhold)
194                         break;
195         }
196         if (def >= NDEFAULTS)
197                 errx(3, "%s: disk too small, calculate by hand", *argv);
198
199         /*
200          * Calculate number of cylinders allocated to each disk
201          * partition.  We may waste a bit of space here, but it's
202          * in the interest of (very backward) compatibility
203          * (for mixed disk systems).
204          */
205         for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
206                 numcyls[part] = 0;
207                 if (defpart[def][part] != 0) {
208                         numcyls[part] = howmany(defpart[def][part], spc);
209                         curcyl += numcyls[part];
210                 }
211         }
212         numcyls[PART('f')] = dp->d_ncylinders - curcyl;
213         numcyls[PART('g')] =
214                 numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')];
215         numcyls[PART('c')] = dp->d_ncylinders;
216         defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable;
217         defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
218         defpart[def][PART('c')] = numcyls[PART('c')] * spc;
219 #ifndef for_now
220         if (totsize || !pflag)
221 #else
222         if (totsize)
223 #endif
224                 defpart[def][PART('c')] -= badsecttable;
225
226         /*
227          * Calculate starting cylinder number for each partition.
228          * Note the 'h' partition is physically located before the
229          * 'g' or 'd' partition.  This is reflected in the layout
230          * arrays defined above.
231          */
232         for (layout = 0; layout < NLAYOUTS; layout++) {
233                 curcyl = 0;
234                 for (lp = layouts[layout]; *lp != 0; lp++) {
235                         startcyl[PART(*lp)] = curcyl;
236                         curcyl += numcyls[PART(*lp)];
237                 }
238         }
239
240         if (pflag) {
241                 printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS);
242                 for (part = PART('a'); part < NPARTITIONS; part++) {
243                         if (numcyls[part] == 0) {
244                                 printf("\t0,\t0,\n");
245                                 continue;
246                         }
247                         if (dp->d_type != DTYPE_MSCP) {
248                                printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n",
249                                         defpart[def][part], startcyl[part],
250                                         'A' + part, startcyl[part],
251                                         startcyl[part] + numcyls[part] - 1);
252                                 continue;
253                         }
254                         printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n",
255                                 defpart[def][part], spc * startcyl[part],
256                                 'A' + part, spc * startcyl[part],
257                                 spc * startcyl[part] + defpart[def][part] - 1);
258                 }
259                 exit(0);
260         }
261         if (dflag) {
262                 int nparts;
263
264                 /*
265                  * In case the disk is in the ``in-between'' range
266                  * where the 'g' partition is smaller than the 'h'
267                  * partition, reverse the frag sizes so the /usr partition
268                  * is always set up with a frag size larger than the
269                  * user's partition.
270                  */
271                 if (defpart[def][PART('g')] < defpart[def][PART('h')]) {
272                         int temp;
273
274                         temp = defparam[PART('h')].p_fsize;
275                         defparam[PART('h')].p_fsize =
276                                 defparam[PART('g')].p_fsize;
277                         defparam[PART('g')].p_fsize = temp;
278                 }
279                 printf("%s:\\\n", dp->d_typename);
280                 printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
281                         dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
282                 if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
283                         printf("sc#%d:", dp->d_secpercyl);
284                 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT)
285                         printf("sf:");
286                 printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]);
287                 for (part = NDDATA - 1; part >= 0; part--)
288                         if (dp->d_drivedata[part])
289                                 break;
290                 for (j = 0; j <= part; j++)
291                         printf("d%d#%d:", j, dp->d_drivedata[j]);
292                 printf("\\\n");
293                 for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++)
294                         if (defpart[def][part] != 0)
295                                 nparts++;
296                 for (part = PART('a'); part < NPARTITIONS; part++) {
297                         if (defpart[def][part] == 0)
298                                 continue;
299                         printf("\t:p%c#%d:", 'a' + part, defpart[def][part]);
300                         printf("o%c#%d:b%c#%d:f%c#%d:",
301                             'a' + part, spc * startcyl[part],
302                             'a' + part,
303                             defparam[part].p_frag * defparam[part].p_fsize,
304                             'a' + part, defparam[part].p_fsize);
305                         if (defparam[part].p_fstype == FS_SWAP)
306                                 printf("t%c=swap:", 'a' + part);
307                         nparts--;
308                         printf("%s\n", nparts > 0 ? "\\" : "");
309                 }
310 #ifdef for_now
311                 defpart[def][PART('c')] -= badsecttable;
312                 part = PART('c');
313                 printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]);
314                 printf("o%c#%d:b%c#%d:f%c#%d:\n",
315                     'a' + part, spc * startcyl[part],
316                     'a' + part,
317                     defparam[part].p_frag * defparam[part].p_fsize,
318                     'a' + part, defparam[part].p_fsize);
319 #endif
320                 exit(0);
321         }
322         printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
323                 dp->d_typename, dp->d_nsectors, dp->d_ntracks,
324                 dp->d_ncylinders);
325         printf("\n    Partition\t   Size\t Offset\t   Range\n");
326         for (part = PART('a'); part < NPARTITIONS; part++) {
327                 printf("\t%c\t", 'a' + part);
328                 if (numcyls[part] == 0) {
329                         printf(" unused\n");
330                         continue;
331                 }
332                 printf("%7d\t%7d\t%4d - %d%s\n",
333                         defpart[def][part], startcyl[part] * spc,
334                         startcyl[part], startcyl[part] + numcyls[part] - 1,
335                         defpart[def][part] % spc ? "*" : "");
336         }
337 }
338
339 static void
340 usage(void)
341 {
342         fprintf(stderr, "usage: diskpart [-p] [-d] [-s size] disk-type\n");
343         exit(1);
344 }
345
346 struct disklabel disk;
347
348 struct  field {
349         char            *f_name;
350         char            *f_defaults;
351         u_int32_t       *f_location;
352 } fields[] = {
353         { "sector size",                "512",  &disk.d_secsize },
354         { "#sectors/track",             0,      &disk.d_nsectors },
355         { "#tracks/cylinder",           0,      &disk.d_ntracks },
356         { "#cylinders",                 0,      &disk.d_ncylinders },
357         { 0, 0, 0 },
358 };
359
360 char *
361 mygets(char *buf)
362 {
363         size_t len;
364
365         if (fgets(buf, BUFSIZ, stdin) == NULL)
366                 buf[0] = '\0';
367         len = strlen(buf);
368         if (len != 0 && buf[len - 1] == '\n')
369                 buf[len - 1] = '\0';
370         return (buf);
371 }
372
373 struct disklabel *
374 promptfordisk(char *name)
375 {
376         struct disklabel *dp = &disk;
377         struct field *fp;
378         int i;
379         char buf[BUFSIZ], **tp, *cp;
380
381         strncpy(dp->d_typename, name, sizeof(dp->d_typename));
382         fprintf(stderr,
383                 "%s: unknown disk type, want to supply parameters (y/n)? ",
384                 name);
385         mygets(buf);
386         if (*buf != 'y')
387                 return ((struct disklabel *)0);
388         for (;;) {
389                 fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
390                 mygets(buf);
391                 if (buf[0] == 0) {
392                         dp->d_type = 1;
393                         break;
394                 }
395                 if ((i = gettype(buf, dktypenames)) >= 0) {
396                         dp->d_type = i;
397                         break;
398                 }
399                 fprintf(stderr, "%s: unrecognized controller type\n", buf);
400                 fprintf(stderr, "use one of:\n");
401                 for (tp = dktypenames; *tp; tp++)
402                         if (index(*tp, ' ') == 0)
403                                 fprintf(stderr, "\t%s\n", *tp);
404         }
405 gettype:
406         dp->d_flags = 0;
407         fprintf(stderr, "type (winchester|removable|simulated)? ");
408         mygets(buf);
409         if (strcmp(buf, "removable") == 0)
410                 dp->d_flags = D_REMOVABLE;
411         else if (strcmp(buf, "simulated") == 0)
412                 dp->d_flags = D_RAMDISK;
413         else if (strcmp(buf, "winchester")) {
414                 fprintf(stderr, "%s: bad disk type\n", buf);
415                 goto gettype;
416         }
417         strncpy(dp->d_typename, buf, sizeof(dp->d_typename));
418         fprintf(stderr, "(type <cr> to get default value, if only one)\n");
419         if (dp->d_type == DTYPE_SMD)
420            fprintf(stderr, "Do %ss support bad144 bad block forwarding (yes)? ",
421                 dp->d_typename);
422         mygets(buf);
423         if (*buf != 'n')
424                 dp->d_flags |= D_BADSECT;
425         for (fp = fields; fp->f_name != NULL; fp++) {
426 again:
427                 fprintf(stderr, "%s ", fp->f_name);
428                 if (fp->f_defaults != NULL)
429                         fprintf(stderr, "(%s)", fp->f_defaults);
430                 fprintf(stderr, "? ");
431                 cp = mygets(buf);
432                 if (*cp == '\0') {
433                         if (fp->f_defaults == NULL) {
434                                 fprintf(stderr, "no default value\n");
435                                 goto again;
436                         }
437                         cp = fp->f_defaults;
438                 }
439                 *fp->f_location = atol(cp);
440                 if (*fp->f_location == 0) {
441                         fprintf(stderr, "%s: bad value\n", cp);
442                         goto again;
443                 }
444         }
445         fprintf(stderr, "sectors/cylinder (%d)? ",
446             dp->d_nsectors * dp->d_ntracks);
447         mygets(buf);
448         if (buf[0] == 0)
449                 dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
450         else
451                 dp->d_secpercyl = atol(buf);
452         fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
453         for (i = 0; i < NDDATA; i++) {
454                 fprintf(stderr, "d%d? ", i);
455                 mygets(buf);
456                 if (buf[0] == 0)
457                         break;
458                 dp->d_drivedata[i] = atol(buf);
459         }
460         return (dp);
461 }
462
463 gettype(char *t, char **names)
464 {
465         char **nm;
466
467         for (nm = names; *nm; nm++)
468                 if (ustrcmp(t, *nm) == 0)
469                         return (nm - names);
470         if (isdigit(*t))
471                 return (atoi(t));
472         return (-1);
473 }
474
475 ustrcmp(char *s1, char *s2)
476 {
477 #define lower(c)        (islower(c) ? (c) : tolower(c))
478
479         for (; *s1; s1++, s2++) {
480                 if (*s1 == *s2)
481                         continue;
482                 if (isalpha(*s1) && isalpha(*s2) &&
483                     lower(*s1) == lower(*s2))
484                         continue;
485                 return (*s2 - *s1);
486         }
487         return (0);
488 }