#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
The Regents of the University of California. All rights reserved.");
#endif
#ifndef lint
#if 0
static char sccsid[] = "from: @(#)diskpart.c 8.3 (Berkeley) 11/30/94";
#else
__RCSID("$NetBSD: diskpart.c,v 1.21 2021/08/01 18:02:22 andvar Exp $");
#endif
#endif
#include <sys/param.h>
#define DKTYPENAMES
#include <sys/disklabel.h>
#include <ctype.h>
#include <disktab.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define for_now
#define NPARTITIONS 8
#define PART(x) (x - 'a')
#define NDEFAULTS 4
static int defpart[NDEFAULTS][NPARTITIONS] = {
{ 15884, 66880, 0, 15884, 307200, 0, 0, 291346 },
{ 15884, 33440, 0, 15884, 55936, 0, 0, 291346 },
{ 15884, 33440, 0, 15884, 55936, 0, 0, 0 },
{ 15884, 10032, 0, 15884, 0, 0, 0, 0 },
};
#define NLAYOUTS 3
static char layouts[NLAYOUTS][NPARTITIONS] = {
{ 'a', 'b', 'h', 'g' },
{ 'a', 'b', 'h', 'd', 'e', 'f' },
{ 'c' },
};
static struct partition defparam[NPARTITIONS] = {
{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },
{ 0, 0, { 1024 }, FS_SWAP, 8, { 0 }, },
{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },
{ 0, 0, { 512 }, FS_UNUSED, 8, { 0 }, },
{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },
{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },
{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, },
{ 0, 0, { 1024 }, FS_UNUSED, 8, { 0 }, }
};
static int badsecttable = 126;
static int pflag;
static int dflag;
static int gettype(const char *, const char *const *);
static struct disklabel *promptfordisk(const char *);
__dead static void usage(void);
int
main(int argc, char *argv[])
{
struct disklabel *dp;
int spc, def, part, layout, j, ch;
uint32_t curcyl;
int threshold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
off_t totsize = 0;
const char *tyname;
char *lp;
while ((ch = getopt(argc, argv, "pds:")) != -1) {
switch (ch) {
case 'd':
dflag++;
break;
case 'p':
pflag++;
break;
case 's':
totsize = strtoul(optarg, &lp, 10);
if (*lp != '\0')
usage();
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1) {
usage();
}
dp = getdiskbyname(*argv);
if (dp == NULL) {
if (isatty(0))
dp = promptfordisk(*argv);
if (dp == NULL) {
fprintf(stderr, "%s: unknown disk type\n", *argv);
exit(1);
}
}
if (dp->d_flags & D_REMOVABLE)
tyname = "removable";
else if (dp->d_flags & D_RAMDISK)
tyname = "simulated";
else
tyname = "winchester";
spc = dp->d_secpercyl;
if (dp->d_type == DKTYPE_SMD && dp->d_flags & D_BADSECT &&
totsize == 0) {
badsecttable = dp->d_nsectors +
roundup(badsecttable, dp->d_nsectors);
threshold = howmany(spc, badsecttable);
} else {
badsecttable = 0;
threshold = 0;
}
if (totsize != 0) {
dp->d_ncylinders = howmany(totsize, spc);
badsecttable = spc * dp->d_ncylinders - totsize;
}
for (def = 0; def < NDEFAULTS; def++) {
curcyl = 0;
for (part = PART('a'); part < NPARTITIONS; part++)
curcyl += howmany(defpart[def][part], spc);
if (curcyl < dp->d_ncylinders - threshold)
break;
}
if (def >= NDEFAULTS) {
fprintf(stderr, "%s: disk too small, calculate by hand\n",
*argv);
exit(1);
}
for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
numcyls[part] = 0;
if (defpart[def][part] != 0) {
numcyls[part] = howmany(defpart[def][part], spc);
curcyl += numcyls[part];
}
}
numcyls[PART('f')] = dp->d_ncylinders - curcyl;
numcyls[PART('g')] =
numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')];
numcyls[PART('c')] = dp->d_ncylinders;
defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable;
defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
defpart[def][PART('c')] = numcyls[PART('c')] * spc;
#ifndef for_now
if (totsize || !pflag)
#else
if (totsize)
#endif
defpart[def][PART('c')] -= badsecttable;
for (layout = 0; layout < NLAYOUTS; layout++) {
curcyl = 0;
for (lp = layouts[layout]; *lp != 0; lp++) {
startcyl[PART(*lp)] = curcyl;
curcyl += numcyls[PART(*lp)];
}
}
if (pflag) {
printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS);
for (part = PART('a'); part < NPARTITIONS; part++) {
if (numcyls[part] == 0) {
printf("\t0,\t0,\n");
continue;
}
if (dp->d_type != DKTYPE_MSCP) {
printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n",
defpart[def][part], startcyl[part],
'A' + part, startcyl[part],
startcyl[part] + numcyls[part] - 1);
continue;
}
printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n",
defpart[def][part], spc * startcyl[part],
'A' + part, spc * startcyl[part],
spc * startcyl[part] + defpart[def][part] - 1);
}
exit(0);
}
if (dflag) {
int nparts;
if (defpart[def][PART('g')] < defpart[def][PART('h')]) {
int temp;
temp = defparam[PART('h')].p_fsize;
defparam[PART('h')].p_fsize =
defparam[PART('g')].p_fsize;
defparam[PART('g')].p_fsize = temp;
}
printf("%s:\\\n", dp->d_typename);
printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
printf("sc#%d:", dp->d_secpercyl);
if (dp->d_type == DKTYPE_SMD && dp->d_flags & D_BADSECT)
printf("sf:");
printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]);
for (part = NDDATA - 1; part >= 0; part--)
if (dp->d_drivedata[part])
break;
for (j = 0; j <= part; j++)
printf("d%d#%d:", j, dp->d_drivedata[j]);
printf("\\\n");
for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++)
if (defpart[def][part] != 0)
nparts++;
for (part = PART('a'); part < NPARTITIONS; part++) {
if (defpart[def][part] == 0)
continue;
printf("\t:p%c#%d:", 'a' + part, defpart[def][part]);
printf("o%c#%d:b%c#%d:f%c#%d:",
'a' + part, spc * startcyl[part],
'a' + part,
defparam[part].p_frag * defparam[part].p_fsize,
'a' + part, defparam[part].p_fsize);
if (defparam[part].p_fstype == FS_SWAP)
printf("t%c=swap:", 'a' + part);
nparts--;
printf("%s\n", nparts > 0 ? "\\" : "");
}
#ifdef for_now
defpart[def][PART('c')] -= badsecttable;
part = PART('c');
printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]);
printf("o%c#%d:b%c#%d:f%c#%d:\n",
'a' + part, spc * startcyl[part],
'a' + part,
defparam[part].p_frag * defparam[part].p_fsize,
'a' + part, defparam[part].p_fsize);
#endif
exit(0);
}
printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
dp->d_typename, dp->d_nsectors, dp->d_ntracks,
dp->d_ncylinders);
printf("\n Partition\t Size\t Offset\t Range\n");
for (part = PART('a'); part < NPARTITIONS; part++) {
printf("\t%c\t", 'a' + part);
if (numcyls[part] == 0) {
printf(" unused\n");
continue;
}
printf("%7d\t%7d\t%4d - %d%s\n",
defpart[def][part], startcyl[part] * spc,
startcyl[part], startcyl[part] + numcyls[part] - 1,
defpart[def][part] % spc ? "*" : "");
}
exit(0);
}
static struct disklabel disk;
static struct field {
const char *f_name;
const char *f_defaults;
u_int32_t *f_location;
} fields[] = {
{ "sector size", "512", &disk.d_secsize },
{ "#sectors/track", NULL, &disk.d_nsectors },
{ "#tracks/cylinder", NULL, &disk.d_ntracks },
{ "#cylinders", NULL, &disk.d_ncylinders },
{ NULL, NULL, 0 },
};
static struct disklabel *
promptfordisk(const char *name)
{
struct disklabel *dp = &disk;
struct field *fp;
int i;
const char *const *tp;
char buf[BUFSIZ], *cp;
strncpy(dp->d_typename, name, sizeof(dp->d_typename));
fprintf(stderr,
"%s: unknown disk type, want to supply parameters (y/n)? ",
name);
if ((fgets(buf, BUFSIZ, stdin) == NULL) || buf[0] != 'y')
return ((struct disklabel *)0);
for (;;) {
fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
if (buf[0] == '\0') {
dp->d_type = 1;
break;
}
if ((i = gettype(buf, dktypenames)) >= 0) {
dp->d_type = i;
break;
}
fprintf(stderr, "%s: unrecognized controller type\n", buf);
fprintf(stderr, "use one of:\n");
for (tp = dktypenames; *tp; tp++)
if (strchr(*tp, ' ') == 0)
fprintf(stderr, "\t%s\n", *tp);
}
gettype:
dp->d_flags = 0;
fprintf(stderr, "type (winchester|removable|simulated)? ");
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
if (buf[0] == '\0')
goto gettype;
switch (buf[0]) {
case 'r':
dp->d_flags = D_REMOVABLE;
break;
case 's':
dp->d_flags = D_RAMDISK;
break;
case 'w':
break;
default:
fprintf(stderr, "%s: bad disk type\n", buf);
case '\0':
goto gettype;
}
fprintf(stderr, "(type <cr> to get default value, if only one)\n");
if (dp->d_type == DKTYPE_SMD) {
fprintf(stderr,
"Do '%s' disks support bad144 bad block forwarding (yes)? ",
dp->d_typename);
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if (buf[0] != 'n')
dp->d_flags |= D_BADSECT;
}
for (fp = fields; fp->f_name != NULL; fp++) {
again:
fprintf(stderr, "%s ", fp->f_name);
if (fp->f_defaults != NULL)
fprintf(stderr, "(%s)", fp->f_defaults);
fprintf(stderr, "? ");
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
cp = buf;
if (*cp == '\0') {
if (fp->f_defaults == NULL) {
fprintf(stderr, "no default value\n");
goto again;
}
cp = __UNCONST(fp->f_defaults);
}
*fp->f_location = atol(cp);
if (*fp->f_location == 0) {
fprintf(stderr, "%s: bad value\n", cp);
goto again;
}
}
fprintf(stderr, "sectors/cylinder (%d)? ",
dp->d_nsectors * dp->d_ntracks);
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
if (buf[0] == 0)
dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
else
dp->d_secpercyl = atol(buf);
fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
for (i = 0; i < NDDATA; i++) {
fprintf(stderr, "d%d? ", i);
if (fgets(buf, BUFSIZ, stdin) == NULL)
return ((struct disklabel *)0);
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
if (buf[0] == 0)
break;
dp->d_drivedata[i] = atol(buf);
}
return (dp);
}
static int
gettype(const char *t, const char *const *names)
{
const char *const *nm;
for (nm = names; *nm; nm++)
if (strcasecmp(t, *nm) == 0)
return (nm - names);
if (isdigit((unsigned char)*t))
return (atoi(t));
return (-1);
}
static void
usage(void)
{
(void)fprintf(stderr, "usage: diskpart [-dp] [-s size] disk-type\n");
exit(1);
}