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