From patchwork Wed Feb 27 03:21:27 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Chapman X-Patchwork-Id: 223488 X-Patchwork-Delegate: scottwood@freescale.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 C46352C008C for ; Wed, 27 Feb 2013 14:21:58 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5F7624A028; Wed, 27 Feb 2013 04:21:56 +0100 (CET) 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 mS2ZVqCYJC-x; Wed, 27 Feb 2013 04:21:56 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 9AEE34A02D; Wed, 27 Feb 2013 04:21:54 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A28884A02D for ; Wed, 27 Feb 2013 04:21:52 +0100 (CET) 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 3dXDyBktyEBh for ; Wed, 27 Feb 2013 04:21:52 +0100 (CET) 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 hrndva-omtalb.mail.rr.com (hrndva-omtalb.mail.rr.com [71.74.56.122]) by theia.denx.de (Postfix) with ESMTP id 857184A028 for ; Wed, 27 Feb 2013 04:21:49 +0100 (CET) X-Authority-Analysis: v=2.0 cv=H5hZMpki c=1 sm=0 a=0BEmelG+NCIkmYz4MtfDKA==:17 a=KAMKcUAUZugA:10 a=okGhO3Ar3REA:10 a=h1JwTc6auzgA:10 a=wPDyFdB5xvgA:10 a=cL4bF6sHAAAA:8 a=kMM4VxVla2IA:10 a=ZoC7vrMfGXiBTAgv_8UA:9 a=HU3bICl8N38A:10 a=0BEmelG+NCIkmYz4MtfDKA==:117 X-Cloudmark-Score: 0 X-Authenticated-User: X-Originating-IP: 65.34.9.19 Received: from [65.34.9.19] ([65.34.9.19:39605] helo=blue.3gfp.com) by hrndva-oedge03.mail.rr.com (envelope-from ) (ecelerity 2.2.3.46 r()) with ESMTP id 88/55-02677-BCB7D215; Wed, 27 Feb 2013 03:21:48 +0000 Received: from localhost (localhost [127.0.0.1]) by blue.3gfp.com (Postfix) with ESMTP id 8436D10805FD; Tue, 26 Feb 2013 22:21:47 -0500 (EST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at 3gfp.com Received: from blue.3gfp.com ([127.0.0.1]) by localhost (blue.3gfp.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GP+RtN+fEyjZ; Tue, 26 Feb 2013 22:21:42 -0500 (EST) Received: from gitemail.3gfp.com (65349hfc19.tampabay.res.rr.com [65.34.9.19]) by blue.3gfp.com (Postfix) with ESMTPSA id 9A2211080068; Tue, 26 Feb 2013 22:21:42 -0500 (EST) From: Harvey Chapman To: u-boot@lists.denx.de Date: Tue, 26 Feb 2013 22:21:27 -0500 Message-Id: <1361935287-62872-1-git-send-email-hchapman@3gfp.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <07547859-2CE1-4B5D-AD74-A515BF590BF3@3gfp.com> References: <07547859-2CE1-4B5D-AD74-A515BF590BF3@3gfp.com> Cc: Scott Wood Subject: [U-Boot] [PATCH] nand: adjust erase/read/write partition/chip size for bad blocks 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 Adjust the sizes calculated for whole partition/chip operations by removing the size of bad blocks so we don't try to erase/read/write past a partition/chip boundary. Signed-off-by: Harvey Chapman --- common/cmd_nand.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/common/cmd_nand.c b/common/cmd_nand.c index 495610c..67d9b59 100644 --- a/common/cmd_nand.c +++ b/common/cmd_nand.c @@ -428,6 +428,31 @@ static int raw_access(nand_info_t *nand, ulong addr, loff_t off, ulong count, return ret; } +/* Adjust a chip/partition size down for bad blocks so we don't + * read/write/erase past the end of a chip/partition by accident. + */ +static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev) +{ + /* We grab the nand info object here fresh because this is usually + * called after arg_off_size() which can change the value of dev. + */ + nand_info_t *nand = &nand_info[dev]; + loff_t maxoffset = offset + *size; + int badblocks = 0; + + /* count badblocks in NAND from offset to offset + size */ + for (; offset < maxoffset; offset += nand->erasesize) { + if (nand_block_isbad(nand, offset)) + badblocks++; + } + /* adjust size if any bad blocks found */ + if (badblocks) { + *size -= badblocks * nand->erasesize; + printf("size adjusted to 0x%llx (%d bad blocks)\n", + (unsigned long long)*size, badblocks); + } +} + static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { int i, ret = 0; @@ -524,6 +549,7 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) int scrub = !strncmp(cmd, "scrub", 5); int spread = 0; int args = 2; + int adjust_size = 0; const char *scrub_warn = "Warning: " "scrub option will erase all factory set bad blocks!\n" @@ -540,8 +566,10 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) spread = 1; } else if (!strcmp(&cmd[5], ".part")) { args = 1; + adjust_size = 1; } else if (!strcmp(&cmd[5], ".chip")) { args = 0; + adjust_size = 1; } else { goto usage; } @@ -560,6 +588,10 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) if (arg_off_size(argc - o, argv + o, &dev, &off, &size) != 0) return 1; + /* size is unspecified */ + if (adjust_size && !scrub) + adjust_size_for_badblocks(&size, off, dev); + nand = &nand_info[dev]; memset(&opts, 0, sizeof(opts)); @@ -644,6 +676,9 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) &off, &size) != 0) return 1; + /* size is unspecified */ + if (argc < 5) + adjust_size_for_badblocks(&rwsize, off, dev); rwsize = size; }