u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
From: Frieder Schrempf <frieder@fris.de>
To: Lukasz Majewski <lukma@denx.de>, u-boot@lists.denx.de
Cc: Heiko Thiery <heiko.thiery@gmail.com>,
	Michael Walle <michael@walle.cc>,
	Frieder Schrempf <frieder.schrempf@kontron.de>,
	AKASHI Takahiro <takahiro.akashi@linaro.org>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Jaehoon Chung <jh80.chung@samsung.com>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Patrice Chotard <patrice.chotard@foss.st.com>,
	Patrick Delaunay <patrick.delaunay@foss.st.com>,
	Simon Glass <sjg@chromium.org>
Subject: [PATCH 3/3] dfu: dfu_sf: Support unaligned offsets for writing to serial flash
Date: Thu, 30 Sep 2021 18:22:08 +0200	[thread overview]
Message-ID: <20210930162208.2749155-3-frieder@fris.de> (raw)
In-Reply-To: <20210930162208.2749155-1-frieder@fris.de>

From: Frieder Schrempf <frieder.schrempf@kontron.de>

Currently using an offset that is not aligned to the erase block size
of the flash results in incorrect data on the device. To support this
use case, we prevent the first cycle to write beyond the erase block
border and return the actual bytes written to the calling functions.
That way the subsequent write cycles will be aligned and the correct
position in the source buffer can be tracked.

This was tested on a SPI NOR with 4k erase blocks and with:
dfu_alt_info=sf 0:0=flash-bin raw 0x400 0x1f0000

Please note, that this implementation doesn't preserve the data in the
first erase block (before the start offset), but neither does the
previous implementation do that for the last erase block when the end
of the data is not aligned. So this seems like an acceptable limitation.

Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
---
 drivers/dfu/dfu.c    |  9 ++++++++-
 drivers/dfu/dfu_sf.c | 12 ++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c
index 5b2659ee7d..c43db77823 100644
--- a/drivers/dfu/dfu.c
+++ b/drivers/dfu/dfu.c
@@ -242,11 +242,12 @@ static char *dfu_get_hash_algo(void)
 
 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
 {
-	long w_size;
+	long w_size, w_size_req;
 	int ret;
 
 	/* flush size? */
 	w_size = dfu->i_buf - dfu->i_buf_start;
+	w_size_req = w_size;
 	if (w_size == 0)
 		return 0;
 
@@ -264,6 +265,9 @@ static int dfu_write_buffer_drain(struct dfu_entity *dfu)
 	/* update offset */
 	dfu->offset += w_size;
 
+	/* store remaining bytes */
+	dfu->b_left = w_size_req - w_size;
+
 	puts("#");
 
 	return ret;
@@ -402,6 +406,9 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int *size, int blk_seq_num)
 		}
 	}
 
+	/* if not all bytes have been written, adjust size */
+	*size -= dfu->b_left;
+
 	return 0;
 }
 
diff --git a/drivers/dfu/dfu_sf.c b/drivers/dfu/dfu_sf.c
index c7ed674092..814df6ef88 100644
--- a/drivers/dfu/dfu_sf.c
+++ b/drivers/dfu/dfu_sf.c
@@ -38,13 +38,25 @@ static int dfu_write_medium_sf(struct dfu_entity *dfu,
 			       u64 offset, void *buf, long *len)
 {
 	u64 start = dfu->data.sf.start + offset;
+	u32 sector_offset;
+	u64 tmp = start;
 	int ret;
 
+	/* Calculate the offset into the sector for unaligned writes */
+	sector_offset = do_div(tmp, dfu->data.sf.dev->sector_size);
+
 	ret = spi_flash_erase(dfu->data.sf.dev, find_sector(dfu, start),
 			      dfu->data.sf.dev->sector_size);
 	if (ret)
 		return ret;
 
+	/*
+	 * In case of an unaligned start address, only write until the
+	 * next sector boundary
+	 */
+	if (sector_offset)
+		*len = dfu->data.sf.dev->sector_size - sector_offset;
+
 	ret = spi_flash_write(dfu->data.sf.dev, start, *len, buf);
 	if (ret)
 		return ret;
-- 
2.33.0


  parent reply	other threads:[~2021-09-30 16:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-30 16:22 [PATCH 1/3] dfu: use pointer to pass length to dfu_write() Frieder Schrempf
2021-09-30 16:22 ` [PATCH 2/3] dfu: dfu_fs: Simplify find_sector() Frieder Schrempf
2021-09-30 16:22 ` Frieder Schrempf [this message]
2022-01-28 15:21 ` [PATCH 1/3] dfu: use pointer to pass length to dfu_write() Tom Rini

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=20210930162208.2749155-3-frieder@fris.de \
    --to=frieder@fris.de \
    --cc=frieder.schrempf@kontron.de \
    --cc=heiko.thiery@gmail.com \
    --cc=jh80.chung@samsung.com \
    --cc=lukma@denx.de \
    --cc=m.szyprowski@samsung.com \
    --cc=michael@walle.cc \
    --cc=patrice.chotard@foss.st.com \
    --cc=patrick.delaunay@foss.st.com \
    --cc=sjg@chromium.org \
    --cc=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.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;
as well as URLs for NNTP newsgroup(s).