summaryrefslogtreecommitdiff
path: root/sbin/fdisk
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2014-03-14 15:41:34 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2014-03-14 15:41:34 +0000
commit6ad247cd626ea34f6cb942bf33053b788613b984 (patch)
tree9fd8e36e7e6e3e00fbf03ab623dd5c686e01ebcc /sbin/fdisk
parent55cfab68ddb4c47c09c290067034b18712ec4b46 (diff)
Revert last -- broke building snaps.
Diffstat (limited to 'sbin/fdisk')
-rw-r--r--sbin/fdisk/cmd.c33
-rw-r--r--sbin/fdisk/disk.c83
-rw-r--r--sbin/fdisk/disk.h17
-rw-r--r--sbin/fdisk/fdisk.c53
-rw-r--r--sbin/fdisk/mbr.c12
-rw-r--r--sbin/fdisk/misc.c4
-rw-r--r--sbin/fdisk/part.c14
-rw-r--r--sbin/fdisk/user.c4
8 files changed, 139 insertions, 81 deletions
diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c
index 80a07d27d46..dcd509375c7 100644
--- a/sbin/fdisk/cmd.c
+++ b/sbin/fdisk/cmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd.c,v 1.65 2014/03/13 12:12:35 krw Exp $ */
+/* $OpenBSD: cmd.c,v 1.66 2014/03/14 15:41:33 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -67,7 +67,7 @@ Xdisk(char *args, struct disk *disk, struct mbr *mbr, struct mbr *tt,
int maxsec = 63;
/* Print out disk info */
- DISK_printgeometry(disk, args);
+ DISK_printmetrics(disk, args);
#if defined (__powerpc__) || defined (__mips__)
maxcyl = 9999999;
@@ -77,14 +77,15 @@ Xdisk(char *args, struct disk *disk, struct mbr *mbr, struct mbr *tt,
/* Ask for new info */
if (ask_yn("Change disk geometry?")) {
- disk->cylinders = ask_num("BIOS Cylinders",
- disk->cylinders, 1, maxcyl);
- disk->heads = ask_num("BIOS Heads",
- disk->heads, 1, maxhead);
- disk->sectors = ask_num("BIOS Sectors",
- disk->sectors, 1, maxsec);
-
- disk->size = disk->cylinders * disk->heads * disk->sectors;
+ disk->real->cylinders = ask_num("BIOS Cylinders",
+ disk->real->cylinders, 1, maxcyl);
+ disk->real->heads = ask_num("BIOS Heads",
+ disk->real->heads, 1, maxhead);
+ disk->real->sectors = ask_num("BIOS Sectors",
+ disk->real->sectors, 1, maxsec);
+
+ disk->real->size = disk->real->cylinders * disk->real->heads
+ * disk->real->sectors;
}
return (CMD_CONT);
@@ -169,9 +170,9 @@ Xedit(char *args, struct disk *disk, struct mbr *mbr, struct mbr *tt,
int maxcyl, maxhead, maxsect;
/* Shorter */
- maxcyl = disk->cylinders - 1;
- maxhead = disk->heads - 1;
- maxsect = disk->sectors;
+ maxcyl = disk->real->cylinders - 1;
+ maxhead = disk->real->heads - 1;
+ maxsect = disk->real->sectors;
/* Get data */
EDIT("BIOS Starting cylinder", pp->scyl, 0, maxcyl);
@@ -186,9 +187,9 @@ Xedit(char *args, struct disk *disk, struct mbr *mbr, struct mbr *tt,
PRT_fix_CHS(disk, pp);
} else {
pp->bs = getuint(disk, "Partition offset", pp->bs,
- disk->size);
+ disk->real->size);
pp->ns = getuint(disk, "Partition size", pp->ns,
- disk->size - pp->bs);
+ disk->real->size - pp->bs);
/* Fix up CHS values */
PRT_fix_CHS(disk, pp);
}
@@ -272,7 +273,7 @@ Xprint(char *args, struct disk *disk, struct mbr *mbr, struct mbr *tt,
int offset)
{
- DISK_printgeometry(disk, args);
+ DISK_printmetrics(disk, args);
printf("Offset: %d\t", offset);
MBR_print(mbr, args);
diff --git a/sbin/fdisk/disk.c b/sbin/fdisk/disk.c
index dc01f59ecc9..d79e4104612 100644
--- a/sbin/fdisk/disk.c
+++ b/sbin/fdisk/disk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: disk.c,v 1.37 2014/03/13 12:02:28 krw Exp $ */
+/* $OpenBSD: disk.c,v 1.38 2014/03/14 15:41:33 krw Exp $ */
/*
* Copyright (c) 1997, 2001 Tobias Weingartner
@@ -41,6 +41,8 @@
struct disklabel dl;
+struct DISK_metrics *DISK_getlabelmetrics(char *name);
+
int
DISK_open(char *disk, int mode)
{
@@ -57,34 +59,80 @@ DISK_open(char *disk, int mode)
return (fd);
}
-void
-DISK_getlabelgeometry(struct disk *disk)
+/* Routine to go after the disklabel for geometry
+ * information. This should work everywhere, but
+ * in the land of PC, things are not always what
+ * they seem.
+ */
+struct DISK_metrics *
+DISK_getlabelmetrics(char *name)
{
+ struct DISK_metrics *lm = NULL;
u_int64_t sz, spc;
int fd;
- /* Get label geometry. */
- if ((fd = DISK_open(disk->name, O_RDONLY)) != -1) {
+ /* Get label metrics */
+ if ((fd = DISK_open(name, O_RDONLY)) != -1) {
+ lm = malloc(sizeof(struct DISK_metrics));
+ if (lm == NULL)
+ err(1, NULL);
+
if (ioctl(fd, DIOCGPDINFO, &dl) == -1) {
warn("DIOCGPDINFO");
+ free(lm);
+ lm = NULL;
} else {
- disk->cylinders = dl.d_ncylinders;
- disk->heads = dl.d_ntracks;
- disk->sectors = dl.d_nsectors;
+ lm->cylinders = dl.d_ncylinders;
+ lm->heads = dl.d_ntracks;
+ lm->sectors = dl.d_nsectors;
/* MBR handles only first UINT32_MAX sectors. */
- spc = (u_int64_t)disk->heads * disk->sectors;
+ spc = (u_int64_t)lm->heads * lm->sectors;
sz = DL_GETDSIZE(&dl);
if (sz > UINT32_MAX) {
- disk->cylinders = UINT32_MAX / spc;
- disk->size = disk->cylinders * spc;
+ lm->cylinders = UINT32_MAX / spc;
+ lm->size = lm->cylinders * spc;
warnx("disk too large (%llu sectors)."
" size truncated.", sz);
} else
- disk->size = sz;
+ lm->size = sz;
unit_types[SECTORS].conversion = dl.d_secsize;
}
close(fd);
}
+
+ return (lm);
+}
+
+/* This is ugly, and convoluted. All the magic
+ * for disk geo/size happens here. Basically,
+ * the real size is the one we will use in the
+ * rest of the program, the label size is what we
+ * got from the disklabel. If the disklabel fails,
+ * we assume we are working with a normal file,
+ * and should request the user to specify the
+ * geometry he/she wishes to use.
+ */
+int
+DISK_getmetrics(struct disk *disk, struct DISK_metrics *user)
+{
+
+ disk->label = DISK_getlabelmetrics(disk->name);
+
+ /* If user supplied, use that */
+ if (user) {
+ disk->real = user;
+ return (0);
+ }
+
+ /* If we have a label, use that */
+ if (disk->label) {
+ disk->real = disk->label;
+ return (0);
+ }
+
+ /* Can not get geometry, punt */
+ disk->real = NULL;
+ return (1);
}
/*
@@ -92,18 +140,19 @@ DISK_getlabelgeometry(struct disk *disk)
* to indicate the units that should be used for display.
*/
int
-DISK_printgeometry(struct disk *disk, char *units)
+DISK_printmetrics(struct disk *disk, char *units)
{
const int secsize = unit_types[SECTORS].conversion;
double size;
int i;
i = unit_lookup(units);
- size = ((double)disk->size * secsize) / unit_types[i].conversion;
+ size = ((double)disk->real->size * secsize) / unit_types[i].conversion;
printf("Disk: %s\t", disk->name);
- if (disk->size) {
- printf("geometry: %d/%d/%d [%.0f ", disk->cylinders,
- disk->heads, disk->sectors, size);
+ if (disk->real) {
+ printf("geometry: %d/%d/%d [%.0f ",
+ disk->real->cylinders, disk->real->heads,
+ disk->real->sectors, size);
if (i == SECTORS && secsize != sizeof(struct dos_mbr))
printf("%d-byte ", secsize);
printf("%s]\n", unit_types[i].lname);
diff --git a/sbin/fdisk/disk.h b/sbin/fdisk/disk.h
index 9ec768cda36..d99675955a4 100644
--- a/sbin/fdisk/disk.h
+++ b/sbin/fdisk/disk.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: disk.h,v 1.13 2014/03/13 12:02:28 krw Exp $ */
+/* $OpenBSD: disk.h,v 1.14 2014/03/14 15:41:33 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -29,18 +29,23 @@
#define _DISK_H
/* Data types */
-struct disk {
- char *name;
+struct DISK_metrics {
u_int32_t cylinders;
u_int32_t heads;
u_int32_t sectors;
u_int32_t size;
};
+struct disk {
+ char *name;
+ struct DISK_metrics *label;
+ struct DISK_metrics *real;
+};
+
/* Prototypes */
-int DISK_open(char *, int);
-void DISK_getlabelgeometry(struct disk *);
-int DISK_printgeometry(struct disk *, char *);
+int DISK_open(char *, int);
+int DISK_getmetrics(struct disk *, struct DISK_metrics *);
+int DISK_printmetrics(struct disk *, char *);
extern struct disklabel dl;
diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c
index 7a3b6d45e1e..b63eafa80fc 100644
--- a/sbin/fdisk/fdisk.c
+++ b/sbin/fdisk/fdisk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fdisk.c,v 1.59 2014/03/13 12:02:28 krw Exp $ */
+/* $OpenBSD: fdisk.c,v 1.60 2014/03/14 15:41:33 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -73,6 +73,7 @@ main(int argc, char *argv[])
int c_arg = 0, h_arg = 0, s_arg = 0;
struct disk disk;
u_int32_t l_arg = 0;
+ struct DISK_metrics *usermetrics;
#ifdef HAS_MBR
char *mbrfile = _PATH_MBR;
#else
@@ -114,7 +115,7 @@ main(int argc, char *argv[])
errx(1, "Sector argument %s [1..63].", errstr);
break;
case 'l':
- l_arg = strtonum(optarg, 64, UINT32_MAX, &errstr);
+ l_arg = strtonum(optarg, 1, UINT32_MAX, &errstr);
if (errstr)
errx(1, "Block argument %s [1..%u].", errstr,
UINT32_MAX);
@@ -129,38 +130,40 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;
- memset(&disk, 0, sizeof(disk));
-
/* Argument checking */
if (argc != 1)
usage();
else
disk.name = argv[0];
+ /* Put in supplied geometry if there */
if (c_arg | h_arg | s_arg) {
- /* Use supplied geometry if it is completely specified. */
- if (c_arg && h_arg && s_arg) {
- disk.cylinders = c_arg;
- disk.heads = h_arg;
- disk.sectors = s_arg;
- disk.size = c_arg * h_arg * s_arg;
- } else
- errx(1, "Please specify a full geometry with [-chs].");
+ usermetrics = malloc(sizeof(struct DISK_metrics));
+ if (usermetrics != NULL) {
+ if (c_arg && h_arg && s_arg) {
+ usermetrics->cylinders = c_arg;
+ usermetrics->heads = h_arg;
+ usermetrics->sectors = s_arg;
+ usermetrics->size = c_arg * h_arg * s_arg;
+ } else
+ errx(1, "Please specify a full geometry with [-chs].");
+ }
} else if (l_arg) {
- /* Use supplied size to calculate a geometry. */
- disk.cylinders = l_arg / 64;
- disk.heads = 1;
- disk.sectors = 64;
- disk.size = l_arg;
- }
+ /* Force into LBA mode */
+ usermetrics = malloc(sizeof(struct DISK_metrics));
+ if (usermetrics != NULL) {
+ usermetrics->cylinders = l_arg / 64;
+ usermetrics->heads = 1;
+ usermetrics->sectors = 64;
+ usermetrics->size = l_arg;
+ }
+ } else
+ usermetrics = NULL;
- if (disk.size == 0) {
- /* Get the disklabel geometry. */
- DISK_getlabelgeometry(&disk);
- if (disk.size == 0)
- errx(1, "Can't get disk geometry, please use [-chs] "
- "to specify.");
- }
+ /* Get the geometry */
+ disk.real = NULL;
+ if (DISK_getmetrics(&disk, usermetrics))
+ errx(1, "Can't get disk geometry, please use [-chs] to specify.");
/* Print out current MBRs on disk */
diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c
index 9d8f8f367f3..691517d6e89 100644
--- a/sbin/fdisk/mbr.c
+++ b/sbin/fdisk/mbr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mbr.c,v 1.35 2014/03/13 12:02:28 krw Exp $ */
+/* $OpenBSD: mbr.c,v 1.36 2014/03/14 15:41:33 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -62,20 +62,20 @@ MBR_init(struct disk *disk, struct mbr *mbr)
/* Use whole disk. Reserve first track, or first cyl, if possible. */
mbr->part[3].id = DOSPTYP_OPENBSD;
- if (disk->heads > 1)
+ if (disk->real->heads > 1)
mbr->part[3].shead = 1;
else
mbr->part[3].shead = 0;
- if (disk->heads < 2 && disk->cylinders > 1)
+ if (disk->real->heads < 2 && disk->real->cylinders > 1)
mbr->part[3].scyl = 1;
else
mbr->part[3].scyl = 0;
mbr->part[3].ssect = 1;
/* Go right to the end */
- mbr->part[3].ecyl = disk->cylinders - 1;
- mbr->part[3].ehead = disk->heads - 1;
- mbr->part[3].esect = disk->sectors;
+ mbr->part[3].ecyl = disk->real->cylinders - 1;
+ mbr->part[3].ehead = disk->real->heads - 1;
+ mbr->part[3].esect = disk->real->sectors;
/* Fix up start/length fields */
PRT_fix_BN(disk, &mbr->part[3], 3);
diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c
index d16492897b6..ae5fd717caf 100644
--- a/sbin/fdisk/misc.c
+++ b/sbin/fdisk/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.36 2014/03/13 12:02:28 krw Exp $ */
+/* $OpenBSD: misc.c,v 1.37 2014/03/14 15:41:33 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -244,7 +244,7 @@ getuint(struct disk *disk, char *prompt, u_int32_t oval, u_int32_t maxval)
if (oval > maxval)
oval = maxval;
- secpercyl = disk->sectors * disk->heads;
+ secpercyl = disk->real->sectors * disk->real->heads;
do {
printf("%s: [%u] ", prompt, oval);
diff --git a/sbin/fdisk/part.c b/sbin/fdisk/part.c
index 11c1705dba1..e2b4627e13e 100644
--- a/sbin/fdisk/part.c
+++ b/sbin/fdisk/part.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: part.c,v 1.56 2014/03/13 12:02:28 krw Exp $ */
+/* $OpenBSD: part.c,v 1.57 2014/03/14 15:41:33 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -308,9 +308,9 @@ PRT_fix_BN(struct disk *disk, struct prt *part, int pn)
return;
}
- /* Disk geometry. */
- spt = disk->sectors;
- tpc = disk->heads;
+ /* Disk metrics */
+ spt = disk->real->sectors;
+ tpc = disk->real->heads;
spc = spt * tpc;
start += part->scyl * spc;
@@ -342,9 +342,9 @@ PRT_fix_CHS(struct disk *disk, struct prt *part)
return;
}
- /* Disk geometry. */
- spt = disk->sectors;
- tpc = disk->heads;
+ /* Disk metrics */
+ spt = disk->real->sectors;
+ tpc = disk->real->heads;
spc = spt * tpc;
start = part->bs;
diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c
index 6e903031742..ea499c278b8 100644
--- a/sbin/fdisk/user.c
+++ b/sbin/fdisk/user.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: user.c,v 1.30 2014/03/13 12:02:28 krw Exp $ */
+/* $OpenBSD: user.c,v 1.31 2014/03/14 15:41:33 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -169,7 +169,7 @@ USER_print_disk(struct disk *disk)
fd = DISK_open(disk->name, O_RDONLY);
offset = firstoff = 0;
- DISK_printgeometry(disk, NULL);
+ DISK_printmetrics(disk, NULL);
do {
error = MBR_read(fd, offset, &dos_mbr);