From patchwork Tue Apr 3 15:14:58 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerlando Falauto X-Patchwork-Id: 150468 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 AD85AB6EE7 for ; Wed, 4 Apr 2012 01:31:12 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5509B280D9; Tue, 3 Apr 2012 17:31:00 +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 qpD0xBOWOg5t; Tue, 3 Apr 2012 17:31:00 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 1D924280CF; Tue, 3 Apr 2012 17:30:44 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id BA8EA280B5 for ; Tue, 3 Apr 2012 17:30:39 +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 GBOtCWF-m6m8 for ; Tue, 3 Apr 2012 17:30:39 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail.de.keymile.com (mail.de.keymile.com [195.8.104.1]) by theia.denx.de (Postfix) with SMTP id 9C551280BC for ; Tue, 3 Apr 2012 17:30:36 +0200 (CEST) Received: from mailrelay.de.keymile.net ([10.9.1.54]) by eSafe SMTP Relay 1333437673; Tue, 03 Apr 2012 17:15:25 +0200 Received: from chber1-10555x.ch.keymile.net (chber1-10555x.ch.keymile.net [172.31.40.82]) by mailrelay.de.keymile.net (8.12.2/8.12.2) with ESMTP id q33FEXjQ002307; Tue, 3 Apr 2012 17:14:41 +0200 (MEST) From: Gerlando Falauto To: u-boot@lists.denx.de Date: Tue, 3 Apr 2012 17:14:58 +0200 Message-Id: <1333466098-31783-3-git-send-email-gerlando.falauto@keymile.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <4F5F9103.1030807@keymile.com> References: <4F5F9103.1030807@keymile.com> X-ESAFE-STATUS: [srvhellgate.de.keymile.net] Mail clean Cc: holger.brunck@keymile.com, Gerlando Falauto , valentin.longchamp@keymile.com Subject: [U-Boot] [PATCH 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 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;