Skip to contentSkip to conversation

Add +size syntax to nand commands to pad lengths to page sizes.

PATCH New Josh Karabin 2 participants 4 messages May 1, 2009 – May 11, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add +size syntax to nand commands to pad lengths to page sizes.

This change implements the suggestion from an earlier thread for how to handle padding of non-page sized writes to NAND flashes.

See http://lists.denx.de/pipermail/u-boot/2009-February/047795.html
for the original discussion. Note that validity of tail page's memory is the reponsibility of the caller.

---
 common/cmd_nand.c |   41 +++++++++++++++++++++++++++++++----------
 1 files changed, 31 insertions(+), 10 deletions(-)
common/cmd_nand.c +31 -10
index 1992531..f094101 100644
@@ -102,9 +102,11 @@ static inline int str2long(char *p, ulong *num)
 }
 
 static int
-arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
+arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off,
+	      size_t *size, int *plussed)
 {
 	int idx = nand_curr_device;
+	char *ps = argv[1];
 #if defined(CONFIG_CMD_MTDPARTS)
 	struct mtd_device *dev;
 	struct part_info *part;
@@ -119,8 +121,12 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size
 			}
 			*off = part->offset;
 			if (argc >= 2) {
-				if (!(str2long(argv[1], (ulong *)size))) {
-					printf("'%s' is not a number\n", argv[1]);
+				if (plussed && *ps == '+') {
+					*plussed = 1;
+					ps++;
+				}
+				if (!(str2long(ps, (ulong *)size))) {
+					printf("'%s' is not a number\n", ps);
 					return -1;
 				}
 				if (*size > part->size)
@@ -145,8 +151,12 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size
 	}
 
 	if (argc >= 2) {
-		if (!(str2long(argv[1], (ulong *)size))) {
-			printf("'%s' is not a number\n", argv[1]);
+		if (plussed && *ps == '+') {
+			*plussed = 1;
+			ps++;
+		}
+		if (!(str2long(ps, (ulong *)size))) {
+			printf("'%s' is not a number\n", ps);
 			return -1;
 		}
 	} else {
@@ -317,7 +327,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
 		printf("\nNAND %s: ", scrub ? "scrub" : "erase");
 		/* skip first two or three arguments, look for offset and size */
-		if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
+		if (arg_off_size(argc - o, argv + o, nand, &off, &size, NULL) != 0)
 			return 1;
 
 		memset(&opts, 0, sizeof(opts));
@@ -378,8 +388,18 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
 		read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
 		printf("\nNAND %s: ", read ? "read" : "write");
-		if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
+		if (read && arg_off_size(argc - 3, argv + 3, nand, &off, &size, NULL) != 0)
 			return 1;
+		else if (!read) {
+			int plussed = 0;
+			if (arg_off_size(argc - 3, argv + 3, nand, &off, &size, &plussed) != 0)
+				return 1;
+			if (plussed) {
+				int tailsize = size & (nand->writesize - 1);
+				memset ((u_char *)addr + size, 0xff, nand->writesize - tailsize);
+				size += nand->writesize - tailsize;
+			}
+		}
 
 		s = strchr(cmd, '.');
 		if (!s || !strcmp(s, ".jffs2") ||
@@ -457,7 +477,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 	}
 
 	if (strcmp(cmd, "unlock") == 0) {
-		if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
+		if (arg_off_size(argc - 2, argv + 2, nand, &off, &size, NULL) < 0)
 			return 1;
 
 		if (!nand_unlock(nand, off, size)) {
@@ -481,9 +501,10 @@ U_BOOT_CMD(nand, 5, 1, do_nand,
 	   "info - show available NAND devices\n"
 	   "nand device [dev] - show or set current device\n"
 	   "nand read - addr off|partition size\n"
-	   "nand write - addr off|partition size\n"
+	   "nand write - addr off|partition [+]size\n"
 	   "    read/write 'size' bytes starting@offset 'off'\n"
-	   "    to/from memory address 'addr', skipping bad blocks.\n"
+	   "    to/from memory address 'addr', skipping bad blocks,\n"
+	   "    rounding up to a page size if '+' is specified.\n"
 	   "nand erase [clean] [off size] - erase 'size' bytes from\n"
 	   "    offset 'off' (entire device if not specified)\n"
 	   "nand bad - show bad blocks\n"
Signature · 1 line
1.6.0.6
#1 lore raw

PATCH 1/1 Add +size syntax to nand commands to pad lengths to page sizes.

Quoted 18 lines · Josh Karabin
common/cmd_nand.c +6 -2
@@ -119,8 +121,12 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size
 			}
 			*off = part->offset;
 			if (argc >= 2) {
-				if (!(str2long(argv[1], (ulong *)size))) {
-					printf("'%s' is not a number\n", argv[1]);
+				if (plussed && *ps == '+') {
+					*plussed = 1;
+					ps++;
+				}
+				if (!(str2long(ps, (ulong *)size))) {
+					printf("'%s' is not a number\n", ps);
 					return -1;
 				}
 				if (*size > part->size)
@@ -145,8 +151,12 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size
 	}
 

common/cmd_nand.c arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size · line 160

Quoted 10 lines
common/cmd_nand.c +6 -2
 	if (argc >= 2) {
-		if (!(str2long(argv[1], (ulong *)size))) {
-			printf("'%s' is not a number\n", argv[1]);
+		if (plussed && *ps == '+') {
+			*plussed = 1;
+			ps++;
+		}
+		if (!(str2long(ps, (ulong *)size))) {
+			printf("'%s' is not a number\n", ps);
 			return -1;

Hmm... would be nice to untangle the duplicated code path rather than add more stuff to both branches.

Quoted 10 lines
common/cmd_nand.c +1 -1
@@ -317,7 +327,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
 		printf("\nNAND %s: ", scrub ? "scrub" : "erase");
 		/* skip first two or three arguments, look for offset and size */
-		if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
+		if (arg_off_size(argc - o, argv + o, nand, &off, &size, NULL) != 0)
 			return 1;
 
 		memset(&opts, 0, sizeof(opts));

common/cmd_nand.c int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) · line 396

Quoted 10 lines
common/cmd_nand.c +5 -1
@@ -378,8 +388,18 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
 		read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
 		printf("\nNAND %s: ", read ? "read" : "write");
-		if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
+		if (read && arg_off_size(argc - 3, argv + 3, nand, &off, &size, NULL) != 0)
 			return 1;
+		else if (!read) {
+			int plussed = 0;
+			if (arg_off_size(argc - 3, argv + 3, nand, &off, &size, &plussed) != 0)
+				return 1;

Why not support plussed for read as well?

common/cmd_nand.c int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) · line 400

Quoted 4 lines
common/cmd_nand.c +4 -0
+			if (plussed) {
+				int tailsize = size & (nand->writesize - 1);
+				memset ((u_char *)addr + size, 0xff, nand->writesize - tailsize);
+				size += nand->writesize - tailsize;

NACK, you cannot write to arbitrary memory beyond the end of the range specified. Allocate a buffer to hold the partial page.

Plus, this will append an entire page of padding if the size does happen to be page-aligned.

common/cmd_nand.c int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) · line 402

Quoted 2 lines
common/cmd_nand.c +2 -0
+			}
+		}

Please keep lines under 80 characters.

-Scott

#2 lore raw

PATCH 1/1 Add +size syntax to nand commands to pad lengths to page sizes.

Thanks for the review! I have some questions below that'll help me get rev 2 correct.

Quoted 19 lines · Scott Wood
Quoted 18 lines · Josh Karabin
common/cmd_nand.c +6 -2
@@ -119,8 +121,12 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size
 			}
 			*off = part->offset;
 			if (argc >= 2) {
-				if (!(str2long(argv[1], (ulong *)size))) {
-					printf("'%s' is not a number\n", argv[1]);
+				if (plussed && *ps == '+') {
+					*plussed = 1;
+					ps++;
+				}
+				if (!(str2long(ps, (ulong *)size))) {
+					printf("'%s' is not a number\n", ps);
 					return -1;
 				}
 				if (*size > part->size)
@@ -145,8 +151,12 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size
 	}
 
Quoted 13 lines

common/cmd_nand.c arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size · line 130

Quoted 10 lines
common/cmd_nand.c +6 -2
 	if (argc >= 2) {
-		if (!(str2long(argv[1], (ulong *)size))) {
-			printf("'%s' is not a number\n", argv[1]);
+		if (plussed && *ps == '+') {
+			*plussed = 1;
+			ps++;
+		}
+		if (!(str2long(ps, (ulong *)size))) {
+			printf("'%s' is not a number\n", ps);
 			return -1;

Hmm... would be nice to untangle the duplicated code path rather than add more stuff to both branches.

No problem. The two branches already duplicated the "str2long" check, which was why I left it that way, but I can certainly clean it up.

Quoted 11 lines
Quoted 11 lines
common/cmd_nand.c +1 -1
@@ -317,7 +327,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
 		printf("\nNAND %s: ", scrub ? "scrub" : "erase");
 		/* skip first two or three arguments, look for offset and size */
-		if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
+		if (arg_off_size(argc - o, argv + o, nand, &off, &size, NULL) != 0)
 			return 1;
 
 		memset(&opts, 0, sizeof(opts));
@@ -378,8 +388,18 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
Quoted 11 lines

common/cmd_nand.c int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) · line 396

Quoted 9 lines
common/cmd_nand.c +5 -1
 		read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
 		printf("\nNAND %s: ", read ? "read" : "write");
-		if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
+		if (read && arg_off_size(argc - 3, argv + 3, nand, &off, &size, NULL) != 0)
 			return 1;
+		else if (!read) {
+			int plussed = 0;
+			if (arg_off_size(argc - 3, argv + 3, nand, &off, &size, &plussed) != 0)
+				return 1;

Why not support plussed for read as well?

"read" isn't strictly necessary, since the existing code permits lengths that result in page-unaligned reads.

Other operations are a little obvious. "erase" implicitly extends the page size already if it needs to, while "unlock" requires page aligned lengths, and "lock" is whole-chip only, so the concept doesn't apply there.

"write" is the only operation that would ever need to depend on +{filesize} for which I could think of a non contrived example, so I stopped there. That said, would you prefer me to support plussed "read" or any of the other operations (erase, unlock)?

Quoted 8 lines

common/cmd_nand.c int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) · line 400

Quoted 5 lines
common/cmd_nand.c +4 -0
+			if (plussed) {
+				int tailsize = size & (nand->writesize - 1);
+				memset ((u_char *)addr + size, 0xff, nand->writesize - tailsize);
+				size += nand->writesize - tailsize;

NACK, you cannot write to arbitrary memory beyond the end of the range specified. Allocate a buffer to hold the partial page.

OK. I expected this, but I wanted to ask a question about it.

Are there actual callers for which this would cause a problem, or is this intended to make the code future proof? I couldn't find any, so I assume that the objection is the latter? Commands like "tftpboot" and "loadb" already stomp on memory without allocating it, so I wasn't sure why the nand commands should be handled differently.

That said, it's not a hard change, so I'll make it.

Quoted 2 lines

Plus, this will append an entire page of padding if the size does happen to be page-aligned.

Thanks for the catch.

Quoted 6 lines

common/cmd_nand.c int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) · line 402

Quoted 3 lines
common/cmd_nand.c +2 -0
+			}
+		}

Please keep lines under 80 characters.

Will do. I noticed a few other lines in the file that hadn't, so figured it wasn't an enforced standard. It will be corrected.

Quoted 1 line

-Scott

#3 lore raw

PATCH 1/1 Add +size syntax to nand commands to pad lengths to page sizes.

Quoted 4 lines · Josh Karabin
Quoted 1 line

Why not support plussed for read as well?

"read" isn't strictly necessary, since the existing code permits lengths that result in page-unaligned reads.

Would be nice to keep the syntax consistent and not error if the user does provide a plus, though.

Quoted 2 lines

Other operations are a little obvious. "erase" implicitly extends the page size already if it needs to,

That should probably be changed to only do so if plus is used -- it's just as dangerous as implicitly rounding up with write (more so, since the blocks are bigger).

Quoted 3 lines

"write" is the only operation that would ever need to depend on +{filesize} for which I could think of a non contrived example, so I stopped there.

Erase is also quite likely to use {filesize} -- and I could see it being used for load as well.

Quoted 2 lines

That said, would you prefer me to support plussed "read" or any of the other operations (erase, unlock)?

Yes.

Quoted 7 lines
Quoted 6 lines

common/cmd_nand.c int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) · line 400

Quoted 4 lines
common/cmd_nand.c +4 -0
+			if (plussed) {
+				int tailsize = size & (nand->writesize - 1);
+				memset ((u_char *)addr + size, 0xff, nand->writesize - tailsize);
+				size += nand->writesize - tailsize;

NACK, you cannot write to arbitrary memory beyond the end of the range specified. Allocate a buffer to hold the partial page.

Quoted 5 lines

OK. I expected this, but I wanted to ask a question about it.

Are there actual callers for which this would cause a problem, or is this intended to make the code future proof? I couldn't find any, so I assume that the objection is the latter?

The caller is the user typing a command, and while most of the time it would be harmless, it's unexpected behavior.

Quoted 3 lines

Commands like "tftpboot" and
"loadb" already stomp on memory without allocating it, so I wasn't sure why the nand commands should be handled differently.

Stomping on memory that the user asks to be stomped on is one thing -- stomping on a few extra bytes beyond the end of that region is another (and if those other commands do that, I'd rather not emulate them).

Quoted 7 lines
Quoted 4 lines

common/cmd_nand.c int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) · line 402

Quoted 2 lines
common/cmd_nand.c +2 -0
+			}
+		}

Please keep lines under 80 characters.

Will do. I noticed a few other lines in the file that hadn't, so figured it wasn't an enforced standard. It will be corrected.

Sometimes it's deliberately overlooked because the wrapped version would be too awkward, but most of the time those long lines got in there by accident.

-Scott