[PATCH] nandwrite - handle situation when read returns less bytes the expected

Artem Bityutskiy dedekind at infradead.org
Tue Dec 16 05:18:34 EST 2008


On Tue, 2008-12-16 at 10:40 +0200, Hai Zaar wrote:
> On Tue, Dec 16, 2008 at 8:28 AM, Artem Bityutskiy
> <dedekind at infradead.org> wrote:
> >
> > Your patch does not apply. It looks like it is not against the latest
> > mtd-utils. How about this patch?
> Yes, my patch is against mtd-utils-1.2.0. Yours is surely better since
> it covers oob reading as well.

Would be nice if you tried my patch and confirmed it works, because I
did not test it.

From: Artem Bityutskiy <Artem.Bityutskiy at nokia.com>
Subject: [PATCH] nandwrite: correct data reading

The "read" syscall does not necessarily return all the requested
data, in which case the caller has to try again and read more.
Take this into account when reading input data.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy at nokia.com>
---
 nandwrite.c |   32 +++++++++++++++++++++-----------
 1 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/nandwrite.c b/nandwrite.c
index fc23e85..b4bc871 100644
--- a/nandwrite.c
+++ b/nandwrite.c
@@ -262,7 +262,6 @@ int main(int argc, char * const argv[])
 	struct nand_oobinfo old_oobinfo;
 
 	process_options(argc, argv);
-
 	erase_buffer(oobbuf, sizeof(oobbuf));
 
 	if (pad && writeoob) {
@@ -438,6 +437,8 @@ int main(int argc, char * const argv[])
 	 * length or zero.
 	 */
 	while (imglen && (mtdoffset < meminfo.size)) {
+		int tinycnt = 0;
+
 		// new eraseblock , check for bad block(s)
 		// Stay in the loop to be sure if the mtdoffset changes because
 		// of a bad block, that the next block that will be written to
@@ -484,15 +485,17 @@ int main(int argc, char * const argv[])
 			}
 
 			/* Read Page Data from input file */
-			if ((cnt = read(ifd, writebuf, readlen)) != readlen) {
-				if (cnt == 0)	// EOF
+			while(tinycnt < readlen) {
+				cnt = read(ifd, writebuf + tinycnt, readlen - tinycnt);
+				if (cnt == 0) { // EOF
 					break;
-				perror ("File I/O error on input file");
-				goto closeall;
+				} else if (cnt < 0) {
+					perror ("File I/O error on input file");
+					goto closeall;
+				}
+				tinycnt += cnt;
 			}
 		} else {
-			int tinycnt = 0;
-
 			while(tinycnt < readlen) {
 				cnt = read(ifd, writebuf + tinycnt, readlen - tinycnt);
 				if (cnt == 0) { // EOF
@@ -522,11 +525,18 @@ int main(int argc, char * const argv[])
 		}
 
 		if (writeoob) {
-			/* Read OOB data from input file, exit on failure */
-			if ((cnt = read(ifd, oobreadbuf, meminfo.oobsize)) != meminfo.oobsize) {
-				perror ("File I/O error on input file");
-				goto closeall;
+			tinycnt = 0;
+			while(tinycnt < readlen) {
+				cnt = read(ifd, oobreadbuf + tinycnt, meminfo.oobsize - tinycnt);
+				if (cnt == 0) { // EOF
+					break;
+				} else if (cnt < 0) {
+					perror ("File I/O error on input file");
+					goto closeall;
+				}
+				tinycnt += cnt;
 			}
+
 			if (!noecc) {
 				int i, start, len;
 				/*
-- 
1.5.4.3

-- 
Best regards,
Artem Bityutskiy (Битюцкий Артём)




More information about the linux-mtd mailing list