diff options
Diffstat (limited to 'sbin/fdisk/fdisk.c')
-rw-r--r-- | sbin/fdisk/fdisk.c | 55 |
1 files changed, 26 insertions, 29 deletions
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) |