[PATCH 3/3] dfu: dfu_sf: Support unaligned offsets for writing to serial flash

Frieder Schrempf frieder at fris.de
Thu Sep 30 18:22:08 CEST 2021


From: Frieder Schrempf <frieder.schrempf at 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 at 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



More information about the U-Boot mailing list