Patchwork [06/10] mtd-utils: nandwrite: Use 64-bit offset

login
register
mail settings
Submitter Brian Norris
Date Nov. 3, 2010, 8:27 a.m.
Message ID <1288772847-8120-6-git-send-email-computersforpeace@gmail.com>
Download mbox | patch
Permalink /patch/69949/
State New
Headers show

Comments

Brian Norris - Nov. 3, 2010, 8:27 a.m.
To support large NAND devices, we need 64-bit data types for
write offsets. This patch makes data type changes along with
their corresponding printf() formats and the input conversion
(i.e., use "strtoll()" instead of "strol()").

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
---
 nandwrite.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
Artem Bityutskiy - Nov. 13, 2010, 11:48 a.m.
On Wed, 2010-11-03 at 01:27 -0700, Brian Norris wrote:
> -				mtdoffset = strtol(optarg, NULL, 0);
> +				mtdoffset = strtoll(optarg, NULL, 0);

Ideally this should check for strtoll failures, but this just a side
note. Not sure if this is the best way, but I'd do it like this:

char *endp;

mtdoffset = strtoul(optarg, &endp, 0);
if (*endp != '\0' || endp == optarg)
	return errmsg("bad volume alignment: \"%s\"", optarg);

or something like this. The check for negative mtdoffset could be there
as well.
Mike Frysinger - Nov. 13, 2010, 10:45 p.m.
On Sat, Nov 13, 2010 at 06:48, Artem Bityutskiy wrote:
> On Wed, 2010-11-03 at 01:27 -0700, Brian Norris wrote:
>> -                             mtdoffset = strtol(optarg, NULL, 0);
>> +                             mtdoffset = strtoll(optarg, NULL, 0);
>
> Ideally this should check for strtoll failures, but this just a side
> note. Not sure if this is the best way, but I'd do it like this:
>
> char *endp;
>
> mtdoffset = strtoul(optarg, &endp, 0);
> if (*endp != '\0' || endp == optarg)
>        return errmsg("bad volume alignment: \"%s\"", optarg);
>
> or something like this. The check for negative mtdoffset could be there
> as well.

or use simple_strtoll() from common.h as that includes error handling ...
-mike
Artem Bityutskiy - Nov. 14, 2010, 7:49 a.m.
On Sat, 2010-11-13 at 17:45 -0500, Mike Frysinger wrote:
> On Sat, Nov 13, 2010 at 06:48, Artem Bityutskiy wrote:
> > On Wed, 2010-11-03 at 01:27 -0700, Brian Norris wrote:
> >> -                             mtdoffset = strtol(optarg, NULL, 0);
> >> +                             mtdoffset = strtoll(optarg, NULL, 0);
> >
> > Ideally this should check for strtoll failures, but this just a side
> > note. Not sure if this is the best way, but I'd do it like this:
> >
> > char *endp;
> >
> > mtdoffset = strtoul(optarg, &endp, 0);
> > if (*endp != '\0' || endp == optarg)
> >        return errmsg("bad volume alignment: \"%s\"", optarg);
> >
> > or something like this. The check for negative mtdoffset could be there
> > as well.
> 
> or use simple_strtoll() from common.h as that includes error handling ...

Oh, even better.

Patch

diff --git a/nandwrite.c b/nandwrite.c
index d05d257..b362c29 100644
--- a/nandwrite.c
+++ b/nandwrite.c
@@ -109,7 +109,7 @@  static void display_version(void)
 
 static const char	*standard_input = "-";
 static const char	*mtd_device, *img;
-static int		mtdoffset = 0;
+static long long	mtdoffset = 0;
 static bool		quiet = false;
 static bool		writeoob = false;
 static bool		rawoob = false;
@@ -201,7 +201,7 @@  static void process_options(int argc, char * const argv[])
 				writeoob = true;
 				break;
 			case 's':
-				mtdoffset = strtol(optarg, NULL, 0);
+				mtdoffset = strtoll(optarg, NULL, 0);
 				break;
 			case 'b':
 				blockalign = atoi(optarg);
@@ -213,7 +213,7 @@  static void process_options(int argc, char * const argv[])
 	}
 
 	if (mtdoffset < 0) {
-		fprintf(stderr, "Can't specify a negative device offset `%d'\n",
+		fprintf(stderr, "Can't specify a negative device offset `%lld'\n",
 				mtdoffset);
 		exit(EXIT_FAILURE);
 	}
@@ -259,7 +259,7 @@  int main(int argc, char * const argv[])
 	int ifd = -1;
 	int imglen = 0, pagelen;
 	bool baderaseblock = false;
-	int blockstart = -1;
+	long long blockstart = -1;
 	struct mtd_dev_info mtd;
 	struct mtd_oob_buf oob;
 	loff_t offs;
@@ -480,7 +480,7 @@  int main(int argc, char * const argv[])
 
 			baderaseblock = false;
 			if (!quiet)
-				fprintf(stdout, "Writing data to block %d at offset 0x%x\n",
+				fprintf(stdout, "Writing data to block %lld at offset 0x%llx\n",
 						 blockstart / ebsize_aligned, blockstart);
 
 			/* Check all the blocks in an erase block for bad blocks */
@@ -494,7 +494,7 @@  int main(int argc, char * const argv[])
 					baderaseblock = true;
 					if (!quiet)
 						fprintf(stderr, "Bad block at %x, %u block(s) "
-								"from %x will be skipped\n",
+								"from %llx will be skipped\n",
 								(int) offs, blockalign, blockstart);
 				}