From patchwork Wed Jul 3 18:33:58 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerlando Falauto X-Patchwork-Id: 256725 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 A3C552C02B7 for ; Thu, 4 Jul 2013 04:41:33 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 54C664A027; Wed, 3 Jul 2013 20:41: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 7fi8lnyjZ1Qt; Wed, 3 Jul 2013 20:41:24 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D2CD74A044; Wed, 3 Jul 2013 20:41:21 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B4A9C4A028 for ; Wed, 3 Jul 2013 20:41:14 +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 xDLntLd1fGYZ for ; Wed, 3 Jul 2013 20:41:07 +0200 (CEST) X-Greylist: delayed 406 seconds by postgrey-1.27 at theia; Wed, 03 Jul 2013 20:41:07 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.250]) by theia.denx.de (Postfix) with ESMTPS id C42D64A025 for ; Wed, 3 Jul 2013 20:41:07 +0200 (CEST) Received: from frodo.de.keymile.net ([10.9.1.54]:35886 helo=mailrelay.de.keymile.net) by mail-de.keymile.com with esmtp (Exim 4.76) (envelope-from ) id 1UuRsj-0006BD-1N; Wed, 03 Jul 2013 20:34:21 +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 r63IW7nX019457; Wed, 3 Jul 2013 20:32:15 +0200 (MEST) From: Gerlando Falauto To: u-boot@lists.denx.de Date: Wed, 3 Jul 2013 20:33:58 +0200 Message-Id: <1372876438-18305-3-git-send-email-gerlando.falauto@keymile.com> X-Mailer: git-send-email 1.8.0.1 In-Reply-To: <1372876438-18305-1-git-send-email-gerlando.falauto@keymile.com> References: <1372876438-18305-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 v3 2/2] cmd_sf: let "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 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/common/cmd_sf.c b/common/cmd_sf.c index ab35a94..1141dc1 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 meaningful (len) */ if (memcmp(cmp_buf, buf, len) == 0) { debug("Skip region %x size %zx: no change\n", offset, len); @@ -163,8 +165,16 @@ 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"; + /* Write the initial part of the block from the source */ if (spi_flash_write(flash, offset, len, buf)) return "write"; + /* If it's a partial sector, rewrite the existing part */ + if (len != flash->sector_size) { + /* Rewrite the original data to the end of the sector */ + if (spi_flash_write(flash, offset + len, + flash->sector_size - len, &cmp_buf[len])) + return "write"; + } return NULL; }