mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Bastian Krause <bst@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Steffen Trumtrar <s.trumtrar@pengutronix.de>,
	Bastian Krause <bst@pengutronix.de>
Subject: [PATCH] mtd: spi-nor: fix page offset/remains calculation in spi_nor_write
Date: Thu,  1 Aug 2019 11:19:50 +0200	[thread overview]
Message-ID: <20190801091950.14606-1-bst@pengutronix.de> (raw)

nor->write() simply adds the number of written bytes to the pointer
given. Thus retval is incremented in each loop cycle for each
spi_nor_write() call without ever resetting it. This leads to wrong page
offset/remains calculations and an incorrect number of bytes written to
retlen.

This becomes apparant only if the calling function actually compares len
and retlen (e.g. mtd_peb_write() ). Otherwise wrong data is written:

  $ memcpy -s /dev/prng -d prng_data 0 0 10k
  $ erase /dev/mtd0.mypart
  $ cp prng_data /dev/mtd0.mypart
  $ memcmp -s prng_data -d /dev/mtd0.mypart 0 0

memcmp returned "files differ" before, with this patch it returns "OK".

Fixes: c8516869c4 ("spi: Extend the core to ease integration of SPI memory controllers")
Signed-off-by: Bastian Krause <bst@pengutronix.de>
---
 drivers/mtd/spi-nor/spi-nor.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
index b2052ce0af..9dbc8302ed 100644
--- a/drivers/mtd/spi-nor/spi-nor.c
+++ b/drivers/mtd/spi-nor/spi-nor.c
@@ -972,6 +972,7 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 
 	for (i = 0; i < len; ) {
 		ssize_t written;
+		retval = 0;
 
 		page_offset = (to + i) & (nor->page_size - 1);
 		page_remain = min_t(size_t, nor->page_size - page_offset,
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

             reply	other threads:[~2019-08-01  9:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-01  9:19 Bastian Krause [this message]
2019-08-01 13:16 ` Bastian Krause
2019-08-05  9:34 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190801091950.14606-1-bst@pengutronix.de \
    --to=bst@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.trumtrar@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox