Skip to contentSkip to conversation

nanddump/nandwrite: use "simple_" str functions

PATCH In review Brian Norris 3 participants 15 messages Nov 29, 2010 – Dec 2, 2010 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 nanddump/nandwrite: use "simple_" str functions

Per Mike Frysinger's suggestion, we check for strtoll() and strtoull() errors by using the "common.h" helper functions simple_strtoll() and simple_strtoull().

---
 nanddump.c  |    4 ++--
 nandwrite.c |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
nanddump.c +2 -2
index fe29596..bf95e81 100644
@@ -136,7 +136,7 @@ static void process_options(int argc, char * const argv[])
 				omitbad = true;
 				break;
 			case 's':
-				start_addr = strtoull(optarg, NULL, 0);
+				start_addr = simple_strtoull(optarg, &error);
 				break;
 			case 'f':
 				if (!(dumpfile = strdup(optarg))) {
@@ -145,7 +145,7 @@ static void process_options(int argc, char * const argv[])
 				}
 				break;
 			case 'l':
-				length = strtoull(optarg, NULL, 0);
+				length = simple_strtoull(optarg, &error);
 				break;
 			case 'o':
 				omitoob = true;
nandwrite.c +1 -1
index bbe69b0..5373a89 100644
@@ -201,7 +201,7 @@ static void process_options(int argc, char * const argv[])
 				writeoob = true;
 				break;
 			case 's':
-				mtdoffset = strtoll(optarg, NULL, 0);
+				mtdoffset = simple_strtoll(optarg, &error);
 				break;
 			case 'b':
 				blockalign = atoi(optarg);
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 2/8 mtd-utils: common.h: Add MAX() macro, fix MIN()

Add MAX() macro to common.h, to be used in future patches.

Also a style change in comma location on MIN().

---
 include/common.h |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
include/common.h +4 -1
index 25abc0c..eb8d5fc 100644
@@ -34,7 +34,10 @@ extern "C" {
 #endif
 
 #ifndef MIN	/* some C lib headers define this for us */
-#define MIN(a ,b) ((a) < (b) ? (a) : (b))
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+#ifndef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
 #endif
 #define min(a, b) MIN(a, b) /* glue for linux kernel source */
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Signature · 1 line
1.7.0.4
#2 lore raw

PATCH 3/8 nanddump: Refactor pretty print code into an sprintf()

In reply to Brian Norris

A do-while loop in pretty_dump_to_buffer() can be refactored into a single sprintf() statement. MAX() and MIN() are used to ensure that:
(1) We have at least a single space between hex and ASCII output
(2) We don't overflow the line buffer

This patch was suggested by Mike Frysinger.

---
 nanddump.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
nanddump.c +4 -3
index bf95e81..14a8816 100644
@@ -252,9 +252,10 @@ static void pretty_dump_to_buffer(const unsigned char *buf, size_t len,
 	if (!ascii)
 		goto nil;
 
-	do {
-		linebuf[lx++] = ' ';
-	} while (lx < (linebuflen - 1) && lx < (ascii_column - 1));
+	/* Spacing between hex and ASCII - ensure at least one space */
+	lx += sprintf(linebuf + lx, "%*s",
+			MAX((int)MIN(linebuflen, ascii_column) - 1 - lx, 1),
+			" ");
 
 	linebuf[lx++] = '|';
 	for (j = 0; (j < len) && (lx + 2) < linebuflen; j++)
Signature · 1 line
1.7.0.4
#3 lore raw

PATCH 4/8 nanddump: change "unsigned" to "signed"

In reply to Brian Norris

For consistency between nanddump and nandwrite and in order to provide better means for checking for negative inputs, the "offset" and "length" types in nanddump should be changed to signed integer types. This also solves a signed/unsigned comparison warning.

---
 nanddump.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
nanddump.c +6 -6
index 14a8816..b0dd7dc 100644
@@ -82,8 +82,8 @@ static bool			pretty_print = false;	// print nice
 static bool			noecc = false;		// don't error correct
 static bool			noskipbad = false;	// don't skip bad blocks
 static bool			omitoob = false;	// omit oob data
-static unsigned long long	start_addr;		// start address
-static unsigned long long	length;			// dump length
+static long long		start_addr;		// start address
+static long long		length;			// dump length
 static const char		*mtddev;		// mtd device name
 static const char		*dumpfile;		// dump file name
 static bool			omitbad = false;
@@ -136,7 +136,7 @@ static void process_options(int argc, char * const argv[])
 				omitbad = true;
 				break;
 			case 's':
-				start_addr = simple_strtoull(optarg, &error);
+				start_addr = simple_strtoll(optarg, &error);
 				break;
 			case 'f':
 				if (!(dumpfile = strdup(optarg))) {
@@ -145,7 +145,7 @@ static void process_options(int argc, char * const argv[])
 				}
 				break;
 			case 'l':
-				length = simple_strtoull(optarg, &error);
+				length = simple_strtoll(optarg, &error);
 				break;
 			case 'o':
 				omitoob = true;
@@ -273,8 +273,8 @@ nil:
  */
 int main(int argc, char * const argv[])
 {
-	unsigned long long ofs, end_addr = 0;
-	unsigned long long blockstart = 1;
+	long long ofs, end_addr = 0;
+	long long blockstart = 1;
 	int ret, i, fd, ofd = 0, bs, badblock = 0;
 	struct mtd_dev_info mtd;
 	char pretty_buf[PRETTY_BUF_LEN];
Signature · 1 line
1.7.0.4
#4 lore raw

PATCH 5/8 nanddump: check for negative inputs

In reply to Brian Norris
---
 nanddump.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
nanddump.c +7 -0
index b0dd7dc..96b46d4 100644
@@ -173,6 +173,13 @@ static void process_options(int argc, char * const argv[])
 		}
 	}
 
+	if (start_addr < 0)
+		errmsg_die("Can't specify a negative device offset '%lld'",
+				start_addr);
+
+	if (length < 0)
+		errmsg_die("Can't specify a negative length '%lld'", length);
+
 	if (quiet && pretty_print) {
 		fprintf(stderr, "The quiet and pretty print options are mutually-\n"
 				"exclusive. Choose one or the other.\n");
Signature · 1 line
1.7.0.4
#5 lore raw

PATCH v2 5/8 nanddump: check for negative inputs

Includes error messages for negative device offsets and negative lengths, telling the user what the offending option and value were.

---
 nanddump.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
nanddump.c +7 -0
index b0dd7dc..9f52878 100644
@@ -173,6 +173,13 @@ static void process_options(int argc, char * const argv[])
 		}
 	}
 
+	if (start_addr < 0)
+		errmsg_die("Can't specify negative offset with option -s: %lld",
+				start_addr);
+
+	if (length < 0)
+		errmsg_die("Can't specify length with option -l: %lld", length);
+
 	if (quiet && pretty_print) {
 		fprintf(stderr, "The quiet and pretty print options are mutually-\n"
 				"exclusive. Choose one or the other.\n");
Signature · 1 line
1.7.0.4
#6 lore raw

PATCH v3 5/8 nanddump: check for negative inputs

Includes error messages for negative device offsets and negative lengths, telling the user what the offending option and value were.

Previous patch left out the "negative" in the error message.

---
 nanddump.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
nanddump.c +7 -0
index b0dd7dc..6a6f904 100644
@@ -173,6 +173,13 @@ static void process_options(int argc, char * const argv[])
 		}
 	}
 
+	if (start_addr < 0)
+		errmsg_die("Can't specify negative offset with option -s: %lld",
+				start_addr);
+
+	if (length < 0)
+		errmsg_die("Can't specify negative length with option -l: %lld", length);
+
 	if (quiet && pretty_print) {
 		fprintf(stderr, "The quiet and pretty print options are mutually-\n"
 				"exclusive. Choose one or the other.\n");
Signature · 1 line
1.7.0.4
#7 lore raw

PATCH 6/8 nanddump: choose correct "printf" format-specifier

In reply to Brian Norris

The mtd-descriptor attributes contain signed data, not unsigned.

---
 nanddump.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
nanddump.c +1 -1
index 96b46d4..0e393cc 100644
@@ -381,7 +381,7 @@ int main(int argc, char * const argv[])
 
 	/* Print informative message */
 	if (!quiet) {
-		fprintf(stderr, "Block size %u, page size %u, OOB size %u\n",
+		fprintf(stderr, "Block size %d, page size %d, OOB size %d\n",
 				mtd.eb_size, mtd.min_io_size, mtd.oob_size);
 		fprintf(stderr,
 				"Dumping data starting at 0x%08llx and ending at 0x%08llx...\n",
Signature · 1 line
1.7.0.4
#8 lore raw

PATCH 7/8 nandwrite: add check for negative blockalign

In reply to Brian Norris
---
 nandwrite.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
nandwrite.c +4 -0
index 5373a89..c409878 100644
@@ -218,6 +218,10 @@ static void process_options(int argc, char * const argv[])
 		exit(EXIT_FAILURE);
 	}
 
+	if (blockalign < 0)
+		errmsg_die("Can't specify a negative blockalign '%d'",
+				blockalign);
+
 	argc -= optind;
 	argv += optind;
 
Signature · 1 line
1.7.0.4
#9 lore raw

Re: [PATCH 7/8] nandwrite: add check for negative blockalign

Quoted 2 lines · Brian Norris

+               errmsg_die("Can't specify a negative blockalign '%d'",
+                               blockalign);

personally, i'd avoid quotes on non-string types. maybe also mention the command line option that is "blockalign" ?

otherwise, these changes all look fine to me
-mike

#10 lore raw

Re: [PATCH 7/8] nandwrite: add check for negative blockalign

Quoted 6 lines · Mike Frysinger

nandwrite.c static void process_options(int argc, char * const argv[]) · line 223

Quoted 2 lines · Brian Norris
nandwrite.c +2 -0
+               errmsg_die("Can't specify a negative blockalign '%d'",
+                               blockalign);

personally, i'd avoid quotes on non-string types. maybe also mention the command line option that is "blockalign" ?

I'm sending some "v2" patches related to these err messages. I'm not sure what the best, descriptive messages are, but I tried.

Brian

#11 lore raw

PATCH v2 7/8 nandwrite: add check for negative blockalign

In reply to Brian Norris

Includes error messages for negative blockalign, telling the user what the offending option and value were.

---
 nandwrite.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
nandwrite.c +4 -0
index 5373a89..2e45136 100644
@@ -218,6 +218,10 @@ static void process_options(int argc, char * const argv[])
 		exit(EXIT_FAILURE);
 	}
 
+	if (blockalign < 0)
+		errmsg_die("Can't specify negative blockalign with option -b:"
+				" %d", blockalign);
+
 	argc -= optind;
 	argv += optind;
 
Signature · 1 line
1.7.0.4
#12 lore raw

PATCH 8/8 nandwrite: use common.h "errmsg_die"

In reply to Brian Norris

errmsg_die() should be nearly the equivalent of the error message used here. This saves a few lines.

---
 nandwrite.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
nandwrite.c +2 -4
index c409878..c51efd7 100644
@@ -212,11 +212,9 @@ static void process_options(int argc, char * const argv[])
 		}
 	}
 
-	if (mtdoffset < 0) {
-		fprintf(stderr, "Can't specify a negative device offset `%lld'\n",
+	if (mtdoffset < 0)
+		errmsg_die("Can't specify a negative device offset '%lld'",
 				mtdoffset);
-		exit(EXIT_FAILURE);
-	}
 
 	if (blockalign < 0)
 		errmsg_die("Can't specify a negative blockalign '%d'",
Signature · 1 line
1.7.0.4
#13 lore raw

PATCH v2 8/8 nandwrite: use common.h "errmsg_die"

errmsg_die() should be nearly the equivalent of the error message used here. This saves a few lines.

Also edited the error message to include the offending option and got rid of the quotes.

---
 nandwrite.c |    8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)
nandwrite.c +3 -5
index 2e45136..7f459cb 100644
@@ -212,11 +212,9 @@ static void process_options(int argc, char * const argv[])
 		}
 	}
 
-	if (mtdoffset < 0) {
-		fprintf(stderr, "Can't specify a negative device offset `%lld'\n",
-				mtdoffset);
-		exit(EXIT_FAILURE);
-	}
+	if (mtdoffset < 0)
+		errmsg_die("Can't specify negative device offset with option"
+				" -s: %lld", mtdoffset);
 
 	if (blockalign < 0)
 		errmsg_die("Can't specify negative blockalign with option -b:"
Signature · 1 line
1.7.0.4
#14 lore raw
In reply to Brian Norris
Quoted 5 lines · Brian Norris

Per Mike Frysinger's suggestion, we check for strtoll() and strtoull() errors by using the "common.h" helper functions simple_strtoll() and simple_strtoull().

Pushed the series, picked newest patches, thanks a lot!

Signature · 2 lines
Best Regards,
Artem Bityutskiy (Битюцкий Артём)