From patchwork Tue Dec 16 10:18:34 2008 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: nandwrite - handle situation when read returns less bytes the expected X-Patchwork-Submitter: Artem Bityutskiy X-Patchwork-Id: 14209 Message-Id: <1229422714.17960.2.camel@sauron> To: Hai Zaar Cc: linux-mtd@lists.infradead.org Date: Tue, 16 Dec 2008 12:18:34 +0200 From: Artem Bityutskiy List-Id: Linux MTD discussion mailing list On Tue, 2008-12-16 at 10:40 +0200, Hai Zaar wrote: > On Tue, Dec 16, 2008 at 8:28 AM, Artem Bityutskiy > 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 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 --- 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; /*