shithub: riscv

Download patch

ref: dd8908cff003135095996d2b0b5ea250c615a0e2
parent: 5aeddd6788347f121f1c6739a9954fe176fe539e
author: cinap_lenrek <[email protected]>
date: Mon Jan 4 22:57:50 EST 2016

fdisk: properly convert byte units K,M,G and T to cylinders/sectors

the shared command language assumed 512 byte sectors, which is
not the case for fdisk as it uses cylinders for the block unit.
so we introduce an extra argument in the Edit structure and
parseexpr() function so byte sizes are properly converted to
the block unit when the K,M,G and T postfixes are used.

--- a/sys/src/cmd/disk/prep/calc.y
+++ b/sys/src/cmd/disk/prep/calc.y
@@ -79,7 +79,7 @@
 
 static char *inp;
 static jmp_buf jmp;
-static vlong dot, size, dollar;
+static vlong dot, size, dollar, unit;
 static char** errp;
 
 static int
@@ -110,7 +110,8 @@
 			n *= 1024;
 			/* fall through */
 		case 'k':
-			n *= 2;
+			n *= 1024;
+			n /= unit;		/* convert to sectors */
 			break;
 		default:
 			--inp;
@@ -164,7 +165,7 @@
 int yyparse(void);
 
 char*
-parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong *result)
+parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong xunit, vlong *result)
 {
 	char *err;
 
@@ -176,6 +177,7 @@
 	dot = xdot;
 	size = xsize;
 	dollar = xdollar;
+	unit = xunit;
 	yyparse();
 	if(yyexp == nil)
 		return "nil yylval?";
--- a/sys/src/cmd/disk/prep/edisk.c
+++ b/sys/src/cmd/disk/prep/edisk.c
@@ -294,6 +294,7 @@
 		edit.disk->secsize = secsize;
 		edit.disk->secs = edit.disk->size / secsize;
 	}
+	edit.unitsz = edit.disk->secsize;
 	edit.end = edit.disk->secs;
 
 	if(blank)
--- a/sys/src/cmd/disk/prep/edit.c
+++ b/sys/src/cmd/disk/prep/edit.c
@@ -129,7 +129,7 @@
 	if(argc > 2)
 		return "args";
 
-	if(err = parseexpr(argv[1], edit->dot, edit->end, edit->end, &ndot))
+	if(err = parseexpr(argv[1], edit->dot, edit->end, edit->end, edit->unitsz, &ndot))
 		return err;
 
 	edit->dot = ndot;
@@ -157,7 +157,7 @@
 		fprint(2, "start %s: ", edit->unit);
 		q = getline(edit);
 	}
-	if(err = parseexpr(q, edit->dot, edit->end, edit->end, &start))
+	if(err = parseexpr(q, edit->dot, edit->end, edit->end, edit->unitsz, &start))
 		return err;
 
 	if(start < 0 || start >= edit->end)
@@ -181,7 +181,7 @@
 		fprint(2, "end [%lld..%lld] ", start, maxend);
 		q = getline(edit);
 	}
-	if(err = parseexpr(q, edit->dot, maxend, edit->end, &end))
+	if(err = parseexpr(q, edit->dot, maxend, edit->end, edit->unitsz, &end))
 		return err;
 
 	if(start == end)
--- a/sys/src/cmd/disk/prep/edit.h
+++ b/sys/src/cmd/disk/prep/edit.h
@@ -36,6 +36,7 @@
 	void *aux;
 	vlong dot;
 	vlong end;
+	vlong unitsz;
 
 	/* do not use fields below this line */
 	int changed;
@@ -48,7 +49,7 @@
 Part	*findpart(Edit*, char*);
 char	*addpart(Edit*, Part*);
 char	*delpart(Edit*, Part*);
-char *parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong *result);
+char *parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong xunit, vlong *result);
 int	ctldiff(Edit *edit, int ctlfd);
 void *emalloc(ulong);
 char *estrdup(char*);
--- a/sys/src/cmd/disk/prep/fdisk.c
+++ b/sys/src/cmd/disk/prep/fdisk.c
@@ -141,8 +141,8 @@
 		edit.disk->secsize = secsize;
 		edit.disk->secs = edit.disk->size / secsize;
 	}
-
 	sec2cyl = edit.disk->h * edit.disk->s;
+	edit.unitsz = sec2cyl * edit.disk->secsize;
 	edit.end = edit.disk->secs / sec2cyl;
 
 	findmbr(&edit);
@@ -164,7 +164,7 @@
 	if(dowrite || printflag)
 		exits(0);
 
-	fprint(2, "cylinder = %lld bytes\n", sec2cyl*edit.disk->secsize);
+	fprint(2, "%s = %lld bytes\n", edit.unit, edit.unitsz);
 	runcmd(&edit, "p");
 	for(;;) {
 		fprint(2, ">>> ");
--- a/sys/src/cmd/disk/prep/prep.c
+++ b/sys/src/cmd/disk/prep/prep.c
@@ -160,6 +160,7 @@
 		disk->secsize = secsize;
 		disk->secs = disk->size / secsize;
 	}
+	edit.unitsz = disk->secsize;
 	edit.end = disk->secs;
 
 	checkfat(disk);