All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gerlando Falauto <gerlando.falauto@keymile.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 2/2] cmd_sf: "sf update" preserve the final part of the last sector
Date: Tue,  3 Apr 2012 17:14:58 +0200	[thread overview]
Message-ID: <1333466098-31783-3-git-send-email-gerlando.falauto@keymile.com> (raw)
In-Reply-To: <4F5F9103.1030807@keymile.com>

Since "sf update" erases the last block as a whole, but only rewrites
the meaningful initial part of it, the rest would be left erased,
potentially erasing meaningful information.
So, as a safety measure, have it rewrite the original content.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Cc: Valentin Longchamp <valentin.longchamp@keymile.com>
Cc: Holger Brunck <holger.brunck@keymile.com>
---
 common/cmd_sf.c |   15 ++++++++++++++-
 1 files changed, 14 insertions(+), 1 deletions(-)

diff --git a/common/cmd_sf.c b/common/cmd_sf.c
index d97d4a5..0666f52 100644
--- a/common/cmd_sf.c
+++ b/common/cmd_sf.c
@@ -134,8 +134,10 @@ static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
 {
 	debug("offset=%#x, sector_size=%#x, len=%#zx\n",
 		offset, flash->sector_size, len);
-	if (spi_flash_read(flash, offset, len, cmp_buf))
+	/* Read the entire sector so to allow for rewriting */
+	if (spi_flash_read(flash, offset, flash->sector_size, cmp_buf))
 		return "read";
+	/* Compare only what is meningful (len) */
 	if (memcmp(cmp_buf, buf, len) == 0) {
 		debug("Skip region %x size %zx: no change\n",
 			offset, len);
@@ -145,6 +147,17 @@ static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
 	/* Erase the entire sector */
 	if (spi_flash_erase(flash, offset, flash->sector_size))
 		return "erase";
+	/* If it's a partial sector, preserve the existing part */
+	if (len != flash->sector_size) {
+		/* Overwrite the first part of the sector with input data */
+		memcpy(cmp_buf, buf, len);
+		/* Rewrite the whole sector with original data at the end */
+		if (spi_flash_write(flash, offset, flash->sector_size,
+					cmp_buf))
+			return "write";
+		return NULL;
+	}
+	/* Rewrite the whole block from the source */
 	if (spi_flash_write(flash, offset, len, buf))
 		return "write";
 	return NULL;
-- 
1.7.1

  parent reply	other threads:[~2012-04-03 15:14 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-13 18:25 [U-Boot] SPI flash writing Gerlando Falauto
2012-03-13 20:11 ` Mike Frysinger
2012-03-13 20:17   ` Jason Cooper
2012-03-13 20:35     ` Mike Frysinger
2012-03-13 21:31       ` Falauto, Gerlando
2012-03-14  2:16         ` Mike Frysinger
2012-03-14  6:44           ` Gerlando Falauto
2012-03-15  0:50             ` Mike Frysinger
2012-03-15  0:02         ` Tom Rini
2012-03-15  0:51           ` Mike Frysinger
2012-03-14  2:18 ` Simon Glass
2012-03-14  6:58   ` Gerlando Falauto
2012-04-03 14:34 ` [U-Boot] [PATCH] cmd_sf: add size checking to spi flash commands Gerlando Falauto
2012-04-03 19:31   ` Mike Frysinger
2012-07-21 17:29   ` [U-Boot] [PATCH v2] " Mike Frysinger
2012-04-03 15:14 ` [U-Boot] [PATCH 0/2] SPI flash update command Gerlando Falauto
2012-04-04  6:40   ` Valentin Longchamp
2012-04-03 15:14 ` [U-Boot] [PATCH 1/2] cmd_sf: let "sf update" erase last sector as a whole Gerlando Falauto
2012-04-04  0:28   ` Simon Glass
2012-04-03 15:14 ` Gerlando Falauto [this message]
2012-04-04  0:33   ` [U-Boot] [PATCH 2/2] cmd_sf: "sf update" preserve the final part of the last sector Simon Glass

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=1333466098-31783-3-git-send-email-gerlando.falauto@keymile.com \
    --to=gerlando.falauto@keymile.com \
    --cc=u-boot@lists.denx.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 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.