From patchwork Fri Jun 28 07:43:53 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerlando Falauto X-Patchwork-Id: 255288 X-Patchwork-Delegate: jagannadh.teki@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id A8EF02C0380 for ; Fri, 28 Jun 2013 17:50:33 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 6CE9B4A172; Fri, 28 Jun 2013 09:50:30 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id cd0i8+k1JKoP; Fri, 28 Jun 2013 09:50:30 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id AE5554A178; Fri, 28 Jun 2013 09:50:27 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 6AC934A178 for ; Fri, 28 Jun 2013 09:50:24 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id reld5BGla3bY for ; Fri, 28 Jun 2013 09:50:15 +0200 (CEST) X-Greylist: delayed 351 seconds by postgrey-1.27 at theia; Fri, 28 Jun 2013 09:50:04 CEST X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 BL_NJABL=ERR(-1.5) (only DNSBL check requested) Received: from mail-de.keymile.com (mail-de.keymile.com [195.8.104.250]) by theia.denx.de (Postfix) with ESMTPS id 9C0DF4A172 for ; Fri, 28 Jun 2013 09:50:04 +0200 (CEST) Received: from frodo.de.keymile.net ([10.9.1.54]:51450 helo=mailrelay.de.keymile.net) by mail-de.keymile.com with esmtp (Exim 4.76) (envelope-from ) id 1UsTLn-0001VE-1c; Fri, 28 Jun 2013 09:44:11 +0200 Received: from chber1-10555x.ch.keymile.net ([172.31.40.82]) by mailrelay.de.keymile.net (8.12.2/8.12.2) with ESMTP id r5S7fxxf009382; Fri, 28 Jun 2013 09:42:07 +0200 (MEST) From: Gerlando Falauto To: u-boot@lists.denx.de Date: Fri, 28 Jun 2013 09:43:53 +0200 Message-Id: <1372405433-30384-3-git-send-email-gerlando.falauto@keymile.com> X-Mailer: git-send-email 1.7.10.1 In-Reply-To: <1372405433-30384-1-git-send-email-gerlando.falauto@keymile.com> References: <1372405433-30384-1-git-send-email-gerlando.falauto@keymile.com> Cc: Valentin.Longchamp@keymile.com, Valentin Longchamp , Holger.Brunck@keymile.com, Gerlando Falauto , Holger Brunck Subject: [U-Boot] [PATCH v2 2/2] cmd_sf: "sf update" preserve the final part of the last sector X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de 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 Cc: Valentin Longchamp Cc: Holger Brunck Acked-by: Simon Glass --- common/cmd_sf.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/common/cmd_sf.c b/common/cmd_sf.c index ab35a94..fb87d24 100644 --- a/common/cmd_sf.c +++ b/common/cmd_sf.c @@ -152,8 +152,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); @@ -163,6 +165,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;