Patchwork nandwrite - handle situation when read returns less bytes the expected

login
register
mail settings
Submitter Artem Bityutskiy
Date Dec. 16, 2008, 10:18 a.m.
Message ID <1229422714.17960.2.camel@sauron>
Download mbox | patch
Permalink /patch/14209/
State New
Headers show

Comments

Artem Bityutskiy - Dec. 16, 2008, 10:18 a.m.
On Tue, 2008-12-16 at 10:40 +0200, Hai Zaar wrote:
> On Tue, Dec 16, 2008 at 8:28 AM, Artem Bityutskiy
> <dedekind@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@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@nokia.com>
---
 nandwrite.c |   32 +++++++++++++++++++++-----------
 1 files changed, 21 insertions(+), 11 deletions(-)
Hai Zaar - Dec. 24, 2008, 2:48 p.m.
On Tue, Dec 16, 2008 at 12:18 PM, Artem Bityutskiy
<dedekind@infradead.org> wrote:
> On Tue, 2008-12-16 at 10:40 +0200, Hai Zaar wrote:
>> On Tue, Dec 16, 2008 at 8:28 AM, Artem Bityutskiy
>> <dedekind@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.
I've tried the patch with the latest git version of mtd-utils and I
confirm that it works on ARM at91sam9260 board.
Thanks!

>
> From: Artem Bityutskiy <Artem.Bityutskiy@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@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 (Битюцкий Артём)
>
>
Artem Bityutskiy - Dec. 26, 2008, 12:43 p.m.
On Wed, 2008-12-24 at 16:48 +0200, Hai Zaar wrote:
> > Would be nice if you tried my patch and confirmed it works, because I
> > did not test it.
> I've tried the patch with the latest git version of mtd-utils and I
> confirm that it works on ARM at91sam9260 board.
> Thanks!

Pushed, thank you.

Patch

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;
 				/*