From patchwork Thu Feb 28 09:42:09 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 223856 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:4978:20e::2]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id EA67C2C0380 for ; Thu, 28 Feb 2013 20:43:27 +1100 (EST) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UB00P-0005zs-Nk; Thu, 28 Feb 2013 09:42:25 +0000 Received: from metis.ext.pengutronix.de ([2001:6f8:1178:4:290:27ff:fe1d:cc33]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UB00L-0005zL-Ui for linux-mtd@lists.infradead.org; Thu, 28 Feb 2013 09:42:23 +0000 Received: from dude.hi.pengutronix.de ([2001:6f8:1178:2:21e:67ff:fe11:9c5c]) by metis.ext.pengutronix.de with esmtp (Exim 4.72) (envelope-from ) id 1UB00I-00067O-5P; Thu, 28 Feb 2013 10:42:18 +0100 Received: from ukl by dude.hi.pengutronix.de with local (Exim 4.80) (envelope-from ) id 1UB00H-00008s-RU; Thu, 28 Feb 2013 10:42:17 +0100 From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= To: linux-mtd@lists.infradead.org, Artem Bityutskiy Subject: [PATCH] flash_otp_write: fix writing to NAND in presence of partial reads Date: Thu, 28 Feb 2013 10:42:09 +0100 Message-Id: <1362044529-511-1-git-send-email-u.kleine-koenig@pengutronix.de> X-Mailer: git-send-email 1.7.10.4 MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:6f8:1178:2:21e:67ff:fe11:9c5c X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-mtd@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130228_044222_144418_5BDF56E8 X-CRM114-Status: GOOD ( 14.04 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: kernel@pengutronix.de X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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 --- flash_otp_write.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/flash_otp_write.c b/flash_otp_write.c index 41cf1c5..0aa872e 100644 --- a/flash_otp_write.c +++ b/flash_otp_write.c @@ -15,6 +15,23 @@ #include +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; @@ -66,7 +83,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;