summaryrefslogtreecommitdiff
path: root/sbin/fdisk
diff options
context:
space:
mode:
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.c55
-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, 82 insertions, 140 deletions
diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c
index dcd509375c7..9c12742e258 100644
--- a/sbin/fdisk/cmd.c
+++ b/sbin/fdisk/cmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd.c,v 1.66 2014/03/14 15:41:33 krw Exp $ */
+/* $OpenBSD: cmd.c,v 1.67 2014/03/17 13:15:44 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_printmetrics(disk, args);
+ DISK_printgeometry(disk, args);
#if defined (__powerpc__) || defined (__mips__)
maxcyl = 9999999;
@@ -77,15 +77,14 @@ Xdisk(char *args, struct disk *disk, struct mbr *mbr, struct mbr *tt,
/* Ask for new info */
if (ask_yn("Change disk geometry?")) {
- 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;
+ 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;
}
return (CMD_CONT);
@@ -170,9 +169,9 @@ Xedit(char *args, struct disk *disk, struct mbr *mbr, struct mbr *tt,
int maxcyl, maxhead, maxsect;
/* Shorter */
- maxcyl = disk->real->cylinders - 1;
- maxhead = disk->real->heads - 1;
- maxsect = disk->real->sectors;
+ maxcyl = disk->cylinders - 1;
+ maxhead = disk->heads - 1;
+ maxsect = disk->sectors;
/* Get data */
EDIT("BIOS Starting cylinder", pp->scyl, 0, maxcyl);
@@ -187,9 +186,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->real->size);
+ disk->size);
pp->ns = getuint(disk, "Partition size", pp->ns,
- disk->real->size - pp->bs);
+ disk->size - pp->bs);
/* Fix up CHS values */
PRT_fix_CHS(disk, pp);
}
@@ -273,7 +272,7 @@ Xprint(char *args, struct disk *disk, struct mbr *mbr, struct mbr *tt,
int offset)
{
- DISK_printmetrics(disk, args);
+ DISK_printgeometry(disk, args);
printf("Offset: %d\t", offset);
MBR_print(mbr, args);
diff --git a/sbin/fdisk/disk.c b/sbin/fdisk/disk.c
index d79e4104612..7b76c2e03ad 100644
--- a/sbin/fdisk/disk.c
+++ b/sbin/fdisk/disk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: disk.c,v 1.38 2014/03/14 15:41:33 krw Exp $ */
+/* $OpenBSD: disk.c,v 1.39 2014/03/17 13:15:44 krw Exp $ */
/*
* Copyright (c) 1997, 2001 Tobias Weingartner
@@ -41,8 +41,6 @@
struct disklabel dl;
-struct DISK_metrics *DISK_getlabelmetrics(char *name);
-
int
DISK_open(char *disk, int mode)
{
@@ -59,80 +57,34 @@ DISK_open(char *disk, int mode)
return (fd);
}
-/* 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)
+void
+DISK_getlabelgeometry(struct disk *disk)
{
- struct DISK_metrics *lm = NULL;
u_int64_t sz, spc;
int fd;
- /* Get label metrics */
- if ((fd = DISK_open(name, O_RDONLY)) != -1) {
- lm = malloc(sizeof(struct DISK_metrics));
- if (lm == NULL)
- err(1, NULL);
-
+ /* Get label geometry. */
+ if ((fd = DISK_open(disk->name, O_RDONLY)) != -1) {
if (ioctl(fd, DIOCGPDINFO, &dl) == -1) {
warn("DIOCGPDINFO");
- free(lm);
- lm = NULL;
} else {
- lm->cylinders = dl.d_ncylinders;
- lm->heads = dl.d_ntracks;
- lm->sectors = dl.d_nsectors;
+ disk->cylinders = dl.d_ncylinders;
+ disk->heads = dl.d_ntracks;
+ disk->sectors = dl.d_nsectors;
/* MBR handles only first UINT32_MAX sectors. */
- spc = (u_int64_t)lm->heads * lm->sectors;
+ spc = (u_int64_t)disk->heads * disk->sectors;
sz = DL_GETDSIZE(&dl);
if (sz > UINT32_MAX) {
- lm->cylinders = UINT32_MAX / spc;
- lm->size = lm->cylinders * spc;
+ disk->cylinders = UINT32_MAX / spc;
+ disk->size = disk->cylinders * spc;
warnx("disk too large (%llu sectors)."
" size truncated.", sz);
} else
- lm->size = sz;
+ disk->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);
}
/*
@@ -140,19 +92,18 @@ DISK_getmetrics(struct disk *disk, struct DISK_metrics *user)
* to indicate the units that should be used for display.
*/
int
-DISK_printmetrics(struct disk *disk, char *units)
+DISK_printgeometry(struct disk *disk, char *units)
{
const int secsize = unit_types[SECTORS].conversion;
double size;
int i;
i = unit_lookup(units);
- size = ((double)disk->real->size * secsize) / unit_types[i].conversion;
+ size = ((double)disk->size * secsize) / unit_types[i].conversion;
printf("Disk: %s\t", disk->name);
- if (disk->real) {
- printf("geometry: %d/%d/%d [%.0f ",
- disk->real->cylinders, disk->real->heads,
- disk->real->sectors, size);
+ if (disk->size) {
+ printf("geometry: %d/%d/%d [%.0f ", disk->cylinders,
+ disk->heads, disk->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 d99675955a4..c6cbf3549f7 100644
--- a/sbin/fdisk/disk.h
+++ b/sbin/fdisk/disk.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: disk.h,v 1.14 2014/03/14 15:41:33 krw Exp $ */
+/* $OpenBSD: disk.h,v 1.15 2014/03/17 13:15:44 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -29,23 +29,18 @@
#define _DISK_H
/* Data types */
-struct DISK_metrics {
+struct disk {
+ char *name;
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);
-int DISK_getmetrics(struct disk *, struct DISK_metrics *);
-int DISK_printmetrics(struct disk *, char *);
+int DISK_open(char *, int);
+void DISK_getlabelgeometry(struct disk *);
+int DISK_printgeometry(struct disk *, char *);
extern struct disklabel dl;
diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c
index b63eafa80fc..e1fe73c51da 100644
--- a/sbin/fdisk/fdisk.c
+++ b/sbin/fdisk/fdisk.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fdisk.c,v 1.60 2014/03/14 15:41:33 krw Exp $ */
+/* $OpenBSD: fdisk.c,v 1.61 2014/03/17 13:15:44 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -36,6 +36,7 @@
#include <stdint.h>
#include "disk.h"
+#include "misc.h"
#include "user.h"
#define _PATH_MBR _PATH_BOOTDIR "mbr"
@@ -73,7 +74,6 @@ 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
@@ -115,7 +115,7 @@ main(int argc, char *argv[])
errx(1, "Sector argument %s [1..63].", errstr);
break;
case 'l':
- l_arg = strtonum(optarg, 1, UINT32_MAX, &errstr);
+ l_arg = strtonum(optarg, 64, UINT32_MAX, &errstr);
if (errstr)
errx(1, "Block argument %s [1..%u].", errstr,
UINT32_MAX);
@@ -130,41 +130,38 @@ 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 */
+ /* Start with the disklabel geometry and get the sector size. */
+ DISK_getlabelgeometry(&disk);
+
if (c_arg | h_arg | s_arg) {
- 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].");
- }
+ /* 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].");
} else if (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;
-
- /* Get the geometry */
- disk.real = NULL;
- if (DISK_getmetrics(&disk, usermetrics))
- errx(1, "Can't get disk geometry, please use [-chs] to specify.");
+ /* Use supplied size to calculate a geometry. */
+ disk.cylinders = l_arg / 64;
+ disk.heads = 1;
+ disk.sectors = 64;
+ disk.size = l_arg;
+ }
+ if (disk.size == 0 || disk.cylinders == 0 || disk.heads == 0 ||
+ disk.sectors == 0 || unit_types[SECTORS].conversion == 0)
+ errx(1, "Can't get disk geometry, please use [-chs] "
+ "to specify.");
/* Print out current MBRs on disk */
if ((i_flag + u_flag + m_flag) == 0)
diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c
index 691517d6e89..dc5fea8667d 100644
--- a/sbin/fdisk/mbr.c
+++ b/sbin/fdisk/mbr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mbr.c,v 1.36 2014/03/14 15:41:33 krw Exp $ */
+/* $OpenBSD: mbr.c,v 1.37 2014/03/17 13:15:44 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->real->heads > 1)
+ if (disk->heads > 1)
mbr->part[3].shead = 1;
else
mbr->part[3].shead = 0;
- if (disk->real->heads < 2 && disk->real->cylinders > 1)
+ if (disk->heads < 2 && disk->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->real->cylinders - 1;
- mbr->part[3].ehead = disk->real->heads - 1;
- mbr->part[3].esect = disk->real->sectors;
+ mbr->part[3].ecyl = disk->cylinders - 1;
+ mbr->part[3].ehead = disk->heads - 1;
+ mbr->part[3].esect = disk->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 ae5fd717caf..9398f6dd9c4 100644
--- a/sbin/fdisk/misc.c
+++ b/sbin/fdisk/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.37 2014/03/14 15:41:33 krw Exp $ */
+/* $OpenBSD: misc.c,v 1.38 2014/03/17 13:15:44 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->real->sectors * disk->real->heads;
+ secpercyl = disk->sectors * disk->heads;
do {
printf("%s: [%u] ", prompt, oval);
diff --git a/sbin/fdisk/part.c b/sbin/fdisk/part.c
index e2b4627e13e..7e4b593d41e 100644
--- a/sbin/fdisk/part.c
+++ b/sbin/fdisk/part.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: part.c,v 1.57 2014/03/14 15:41:33 krw Exp $ */
+/* $OpenBSD: part.c,v 1.58 2014/03/17 13:15:44 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -308,9 +308,9 @@ PRT_fix_BN(struct disk *disk, struct prt *part, int pn)
return;
}
- /* Disk metrics */
- spt = disk->real->sectors;
- tpc = disk->real->heads;
+ /* Disk geometry. */
+ spt = disk->sectors;
+ tpc = disk->heads;
spc = spt * tpc;
start += part->scyl * spc;
@@ -342,9 +342,9 @@ PRT_fix_CHS(struct disk *disk, struct prt *part)
return;
}
- /* Disk metrics */
- spt = disk->real->sectors;
- tpc = disk->real->heads;
+ /* Disk geometry. */
+ spt = disk->sectors;
+ tpc = disk->heads;
spc = spt * tpc;
start = part->bs;
diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c
index ea499c278b8..ba52bd9c16d 100644
--- a/sbin/fdisk/user.c
+++ b/sbin/fdisk/user.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: user.c,v 1.31 2014/03/14 15:41:33 krw Exp $ */
+/* $OpenBSD: user.c,v 1.32 2014/03/17 13:15:44 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_printmetrics(disk, NULL);
+ DISK_printgeometry(disk, NULL);
do {
error = MBR_read(fd, offset, &dos_mbr);