diff options
Diffstat (limited to 'sbin/fdisk/fdisk.c')
-rw-r--r-- | sbin/fdisk/fdisk.c | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index 0495a69..8314906 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -49,7 +49,10 @@ __FBSDID("$FreeBSD$"); int iotest; -#define NOSECTORS ((u_int32_t)-1) +#define NO_DISK_SECTORS ((u_int32_t)-1) +#define NO_TRACK_CYLINDERS 1023 +#define NO_TRACK_HEADS 255 +#define NO_TRACK_SECTORS 63 #define LBUF 100 static char lbuf[LBUF]; @@ -62,7 +65,7 @@ static char lbuf[LBUF]; * Created. */ -#define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp +#define Decimal(str, ans, tmp, maxval) if (decimal(str, &tmp, ans, maxval)) ans = tmp #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs) @@ -247,7 +250,7 @@ static int get_params(void); static int read_s0(void); static int write_s0(void); static int ok(const char *str); -static int decimal(const char *str, int *num, int deflt); +static int decimal(const char *str, int *num, int deflt, uint32_t maxval); static int read_config(char *config_file); static void reset_boot(void); static int sanitize_partition(struct dos_partition *); @@ -572,9 +575,9 @@ change_part(int i) } do { - Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp); - Decimal("start", partp->dp_start, tmp); - Decimal("size", partp->dp_size, tmp); + Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp, 255); + Decimal("start", partp->dp_start, tmp, NO_DISK_SECTORS); + Decimal("size", partp->dp_size, tmp, NO_DISK_SECTORS); if (!sanitize_partition(partp)) { warnx("ERROR: failed to adjust; setting sysid to 0"); partp->dp_typ = 0; @@ -586,9 +589,9 @@ change_part(int i) tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect); thd = partp->dp_shd; tsec = DPSECT(partp->dp_ssect); - Decimal("beginning cylinder", tcyl, tmp); - Decimal("beginning head", thd, tmp); - Decimal("beginning sector", tsec, tmp); + Decimal("beginning cylinder", tcyl, tmp, NO_TRACK_CYLINDERS); + Decimal("beginning head", thd, tmp, NO_TRACK_HEADS); + Decimal("beginning sector", tsec, tmp, NO_TRACK_SECTORS); partp->dp_scyl = DOSCYL(tcyl); partp->dp_ssect = DOSSECT(tsec,tcyl); partp->dp_shd = thd; @@ -596,9 +599,9 @@ change_part(int i) tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect); thd = partp->dp_ehd; tsec = DPSECT(partp->dp_esect); - Decimal("ending cylinder", tcyl, tmp); - Decimal("ending head", thd, tmp); - Decimal("ending sector", tsec, tmp); + Decimal("ending cylinder", tcyl, tmp, NO_TRACK_CYLINDERS); + Decimal("ending head", thd, tmp, NO_TRACK_HEADS); + Decimal("ending sector", tsec, tmp, NO_TRACK_SECTORS); partp->dp_ecyl = DOSCYL(tcyl); partp->dp_esect = DOSSECT(tsec,tcyl); partp->dp_ehd = thd; @@ -647,7 +650,7 @@ change_active(int which) setactive: do { new = active; - Decimal("active partition", new, tmp); + Decimal("active partition", new, tmp, 0); if (new < 1 || new > 4) { printf("Active partition number must be in range 1-4." " Try again.\n"); @@ -677,9 +680,9 @@ get_params_to_use() { do { - Decimal("BIOS's idea of #cylinders", dos_cyls, tmp); - Decimal("BIOS's idea of #heads", dos_heads, tmp); - Decimal("BIOS's idea of #sectors", dos_sectors, tmp); + Decimal("BIOS's idea of #cylinders", dos_cyls, tmp, 0); + Decimal("BIOS's idea of #heads", dos_heads, tmp, 0); + Decimal("BIOS's idea of #sectors", dos_sectors, tmp, 0); dos_cylsecs = dos_heads * dos_sectors; print_params(); } @@ -915,9 +918,10 @@ ok(const char *str) } static int -decimal(const char *str, int *num, int deflt) +decimal(const char *str, int *num, int deflt, uint32_t maxval) { - int acc = 0, c; + long long acc = 0; + int c; char *cp; while (1) { @@ -935,14 +939,21 @@ decimal(const char *str, int *num, int deflt) if (!c) return 0; while ((c = *cp++)) { - if (c <= '9' && c >= '0') - acc = acc * 10 + c - '0'; - else + if (c <= '9' && c >= '0') { + if (maxval > 0 && acc <= maxval) + acc = acc * 10 + c - '0'; + } else break; } if (c == ' ' || c == '\t') while ((c = *cp) && (c == ' ' || c == '\t')) cp++; if (!c) { + if (maxval > 0 && acc > maxval) { + acc = maxval; + printf("%s exceeds maximum value allowed for " + "this field. The value has been reduced " + "to %lld\n", lbuf, acc); + } *num = acc; return 1; } else @@ -1097,7 +1108,7 @@ str2sectors(const char *str) if (str == end || *end == '\0') { warnx("ERROR line %d: unexpected size: \'%s\'", current_line_number, str); - return NOSECTORS; + return NO_DISK_SECTORS; } if (*end == 'K') @@ -1109,7 +1120,7 @@ str2sectors(const char *str) else { warnx("ERROR line %d: unexpected modifier: %c " "(not K/M/G)", current_line_number, *end); - return NOSECTORS; + return NO_DISK_SECTORS; } return val; @@ -1159,7 +1170,7 @@ process_partition(CMD *command) } } else { partp->dp_start = str2sectors(command->args[2].arg_str); - if (partp->dp_start == NOSECTORS) + if (partp->dp_start == NO_DISK_SECTORS) break; } } else @@ -1171,7 +1182,7 @@ process_partition(CMD *command) dos_cylsecs) - partp->dp_start; else { partp->dp_size = str2sectors(command->args[3].arg_str); - if (partp->dp_size == NOSECTORS) + if (partp->dp_size == NO_DISK_SECTORS) break; } prev_cyl_boundary = ((partp->dp_start + partp->dp_size) / |