]> www.infradead.org Git - mtd-utils.git/commitdiff
flash_otp_write: fix writing to NAND in presence of partial reads
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Thu, 28 Feb 2013 09:42:09 +0000 (10:42 +0100)
committerArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
Mon, 11 Mar 2013 07:51:29 +0000 (09:51 +0200)
When doing something like:

{ printf "\xff"; printf "\xfe"; } | flash_otp_write -u /dev/mtd0 0

flash_otp_write might see only a single byte when reading from stdin for
the first tim. In this case (and without this patch) it pads to
$writesize with '\xff's and writes that out. In the next iteration it
reads the 2nd byte, pads and writes again. So the 2nd byte is written to
offset $writesize instead of 1.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
flash_otp_write.c

index 56769ec7a6aa6b3553cca3367a814265485213c4..3515eee736118cf93dd1f20a4eae0ff9b6dc962b 100644 (file)
 #include <common.h>
 #include <mtd/mtd-user.h>
 
+ssize_t xread(int fd, void *buf, size_t count)
+{
+       ssize_t ret, done = 0;
+
+retry:
+       ret = read(fd, buf + done, count - done);
+       if (ret < 0)
+               return ret;
+
+       done += ret;
+
+       if (ret == 0 /* EOF */ || done == count)
+               return done;
+       else
+               goto retry;
+}
+
 int main(int argc,char *argv[])
 {
        int fd, val, ret, size, wrote, len;
@@ -67,7 +84,7 @@ int main(int argc,char *argv[])
                len = 256;
 
        wrote = 0;
-       while ((size = read(0, buf, len))) {
+       while ((size = xread(0, buf, len))) {
                if (size < 0) {
                        perror("read()");
                        return errno;