All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cmd: sf: Handle unaligned 'update' start offset
@ 2022-09-28 16:45 Marek Vasut
  2022-10-23  5:30 ` Jagan Teki
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Vasut @ 2022-09-28 16:45 UTC (permalink / raw)
  To: u-boot
  Cc: Marek Vasut, Bin Meng, Jagan Teki, Jan Kiszka, Pratyush Yadav,
	Sean Anderson

Currently the 'sf update' command fails in case the 'start' offset is
not aligned to SPI NOR erase block size. Add the missing alignment
calculation. In case the start offset is in the middle of erase block,
round start address down to the nearest aligned one, compare only the
updated data between what is in the SPI NOR and what is being written,
copy new data at offset of the compare buffer, and write back the entire
erase block.

This is useful e.g. on i.MX6Q where the u-boot-with-spl.imx is at
offset 0x400 in the SPI NOR, while the SPI NOR may have erase block
size e.g. 0x1000 bytes.

Signed-off-by: Marek Vasut <marex@denx.de>
---
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Jagan Teki <jagan@amarulasolutions.com>
Cc: Jan Kiszka <jan.kiszka@siemens.com>
Cc: Pratyush Yadav <p.yadav@ti.com>
Cc: Sean Anderson <seanga2@gmail.com>
---
 cmd/sf.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/cmd/sf.c b/cmd/sf.c
index bd102f5af9d..f9b2d9a47a7 100644
--- a/cmd/sf.c
+++ b/cmd/sf.c
@@ -179,16 +179,18 @@ static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
 		size_t len, const char *buf, char *cmp_buf, size_t *skipped)
 {
 	char *ptr = (char *)buf;
+	u32 start_offset = offset % flash->sector_size;
+	u32 read_offset = offset - start_offset;
 
-	debug("offset=%#x, sector_size=%#x, len=%#zx\n",
-	      offset, flash->sector_size, len);
+	debug("offset=%#x+%#x, sector_size=%#x, len=%#zx\n",
+	      read_offset, start_offset, flash->sector_size, len);
 	/* Read the entire sector so to allow for rewriting */
-	if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf))
+	if (spi_flash_read(flash, read_offset, flash->sector_size, cmp_buf))
 		return "read";
 	/* Compare only what is meaningful (len) */
-	if (memcmp(cmp_buf, buf, len) == 0) {
-		debug("Skip region %x size %zx: no change\n",
-		      offset, len);
+	if (memcmp(cmp_buf + start_offset, buf, len) == 0) {
+		debug("Skip region %x+%x size %zx: no change\n",
+		      start_offset, read_offset, len);
 		*skipped += len;
 		return NULL;
 	}
@@ -197,7 +199,7 @@ static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
 		return "erase";
 	/* If it's a partial sector, copy the data into the temp-buffer */
 	if (len != flash->sector_size) {
-		memcpy(cmp_buf, buf, len);
+		memcpy(cmp_buf + start_offset, buf, len);
 		ptr = cmp_buf;
 	}
 	/* Write one complete sector */
@@ -238,6 +240,8 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
 
 		for (; buf < end && !err_oper; buf += todo, offset += todo) {
 			todo = min_t(size_t, end - buf, flash->sector_size);
+			todo = min_t(size_t, end - buf,
+				     flash->sector_size - (offset % flash->sector_size));
 			if (get_timer(last_update) > 100) {
 				printf("   \rUpdating, %zu%% %lu B/s",
 				       100 - (end - buf) / scale,
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] cmd: sf: Handle unaligned 'update' start offset
  2022-09-28 16:45 [PATCH] cmd: sf: Handle unaligned 'update' start offset Marek Vasut
@ 2022-10-23  5:30 ` Jagan Teki
  0 siblings, 0 replies; 2+ messages in thread
From: Jagan Teki @ 2022-10-23  5:30 UTC (permalink / raw)
  To: Marek Vasut; +Cc: u-boot, Bin Meng, Jan Kiszka, Pratyush Yadav, Sean Anderson

On Wed, Sep 28, 2022 at 10:15 PM Marek Vasut <marex@denx.de> wrote:
>
> Currently the 'sf update' command fails in case the 'start' offset is
> not aligned to SPI NOR erase block size. Add the missing alignment
> calculation. In case the start offset is in the middle of erase block,
> round start address down to the nearest aligned one, compare only the
> updated data between what is in the SPI NOR and what is being written,
> copy new data at offset of the compare buffer, and write back the entire
> erase block.
>
> This is useful e.g. on i.MX6Q where the u-boot-with-spl.imx is at
> offset 0x400 in the SPI NOR, while the SPI NOR may have erase block
> size e.g. 0x1000 bytes.
>
> Signed-off-by: Marek Vasut <marex@denx.de>
> ---

Reviewed-by: Jagan Teki <jagan@amarulasolutions.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-23  5:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-28 16:45 [PATCH] cmd: sf: Handle unaligned 'update' start offset Marek Vasut
2022-10-23  5:30 ` Jagan Teki

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.