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