Skip to contentSkip to conversation

Support skipping bad blocks when seeking to start address

PATCH Applied Mike Crowe 2 participants 4 messages Jan 17, 2017 – Jan 23, 2017 Memory technology devices (MTD)

lore
Share & tools
lore

Mike Crowe

author
#0 lore raw

cover Support skipping bad blocks when seeking to start address

I found myself needing to write and verify part of a partition that will be read by the bootloader by skipping blocks from the start of the partition. Initially I added separate --input-skip and --output-skip options but David Oberhollenzer suggested in <cdfdbe5d-1aa8-e424-7691-338d0d63f7b1 at sigma-star.at>
http://lists.infradead.org/pipermail/linux-mtd/2016-December/070855.html
that a simple flag to indicate that the start address should skip bad blocks would avoid ambiguity.

David also suggested that I skip the bad blocks in a separate step before the already-cluttered main loop.

This version uses is_virt_block_bad in nandwrite, so the series now depends on David Oberhollenzer's virtual block handling fixes from <[email protected]>
http://lists.infradead.org/pipermail/linux-mtd/2017-January/071544.html

Mike Crowe (2):
nandwrite: Add --skip-bad-blocks-to-start option
nanddump: Add --skip-bad-blocks-to-start option

 nand-utils/nanddump.c  | 23 +++++++++++++++++++++++
 nand-utils/nandwrite.c | 26 ++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
Signature · 1 line
2.1.4
author
#1 lore raw

PATCH 1/2 nandwrite: Add --skip-bad-blocks-to-start option

The --skip-bad-blocks-to-start option will increase the seek offset by the size of each bad block encountered between the start of the partition and the specified start address.

This can be useful when writing part way through a partition that will be read using a simple bad-block-skipping algorithm.

---
 nand-utils/nandwrite.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
nand-utils/nandwrite.c +26 -0
index c7a53d1..f29fc67 100644
@@ -56,6 +56,8 @@ static void display_help(int status)
 "  -o, --oob               Input contains oob data\n"
 "  -O, --onlyoob           Input contains oob data and only write the oob part\n"
 "  -s addr, --start=addr   Set output start address (default is 0)\n"
+"  --skip-bad-blocks-to-start"
+"                          Skip bad blocks when seeking to the start address\n"
 "  -p, --pad               Pad writes to page size\n"
 "  -b, --blockalign=1|2|4  Set multiple of eraseblocks to align to\n"
 "      --input-skip=length Skip |length| bytes of the input file\n"
@@ -96,6 +98,7 @@ static bool		autoplace = false;
 static bool		skipallffs = false;
 static bool		noskipbad = false;
 static bool		pad = false;
+static bool		skip_bad_blocks_to_start = false;
 static int		blockalign = 1; /* default to using actual block size */
 
 static void process_options(int argc, char * const argv[])
@@ -110,6 +113,7 @@ static void process_options(int argc, char * const argv[])
 			{"version", no_argument, 0, 'V'},
 			{"input-skip", required_argument, 0, 0},
 			{"input-size", required_argument, 0, 0},
+			{"skip-bad-blocks-to-start", no_argument, 0, 0},
 			{"help", no_argument, 0, 'h'},
 			{"blockalign", required_argument, 0, 'b'},
 			{"markbad", no_argument, 0, 'm'},
@@ -139,6 +143,9 @@ static void process_options(int argc, char * const argv[])
 			case 2: /* --input-size */
 				inputsize = simple_strtoll(optarg, &error);
 				break;
+			case 3: /* --skip-bad-blocks-to-start */
+				skip_bad_blocks_to_start = true;
+				break;
 			}
 			break;
 		case 'V':
@@ -365,6 +372,25 @@ int main(int argc, char * const argv[])
 		goto closeall;
 	}
 
+	/* Skip bad blocks on the way to the start address if necessary */
+	if (skip_bad_blocks_to_start) {
+		long long bbs_offset = 0;
+		while (bbs_offset < mtdoffset) {
+			ret = is_virt_block_bad(&mtd, fd, bbs_offset);
+			if (ret < 0) {
+				sys_errmsg("%s: MTD get bad block failed", mtd_device);
+				goto closeall;
+			} else if (ret == 1) {
+				if (!quiet)
+					fprintf(stderr, "Bad block at %llx, %u block(s) "
+						"from %llx will be skipped\n",
+						bbs_offset, blockalign, bbs_offset);
+				mtdoffset += ebsize_aligned;
+			}
+			bbs_offset += ebsize_aligned;
+		}
+	}
+
 	/* Check, if length fits into device */
 	if ((imglen / pagelen) * mtd.min_io_size > mtd.size - mtdoffset) {
 		fprintf(stderr, "Image %lld bytes, NAND page %d bytes, OOB area %d"
Signature · 1 line
2.1.4
author
#2 lore raw

PATCH 2/2 nanddump: Add --skip-bad-blocks-to-start option

In reply to Mike Crowe

The --skip-bad-blocks-to-start option will increase the start address by the size of each bad block encountered between the start of the partition and the specified start address.

This can be useful if other readers of the partition will be reading using a simple bad-block-skipping algorithm.

---
 nand-utils/nanddump.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
nand-utils/nanddump.c +23 -0
index a8ad334..bebb48e 100644
@@ -52,6 +52,8 @@ static void display_help(int status)
 "-p         --prettyprint        Print nice (hexdump)\n"
 "-q         --quiet              Don't display progress and status messages\n"
 "-s addr    --startaddress=addr  Start address\n"
+"           --skip-bad-blocks-to-start\n"
+"                                Skip bad blocks when seeking to the start address\n"
 "\n"
 "--bb=METHOD, where METHOD can be `padbad', `dumpbad', or `skipbad':\n"
 "    padbad:  dump flash data, substituting 0xFF for any bad blocks\n"
@@ -86,6 +88,7 @@ static const char		*dumpfile;		// dump file name
 static bool			quiet = false;		// suppress diagnostic output
 static bool			canonical = false;	// print nice + ascii
 static bool			forcebinary = false;	// force printing binary to tty
+static bool			skip_bad_blocks_to_start = false;
 
 static enum {
 	padbad,   // dump flash data, substituting 0xFF for any bad blocks
@@ -105,6 +108,7 @@ static void process_options(int argc, char * const argv[])
 			{"version", no_argument, 0, 'V'},
 			{"bb", required_argument, 0, 0},
 			{"omitoob", no_argument, 0, 0},
+			{"skip-bad-blocks-to-start", no_argument, 0, 0 },
 			{"help", no_argument, 0, 'h'},
 			{"forcebinary", no_argument, 0, 'a'},
 			{"canonicalprint", no_argument, 0, 'c'},
@@ -146,6 +150,9 @@ static void process_options(int argc, char * const argv[])
 							errmsg_die("--oob and --oomitoob are mutually exclusive");
 						}
 						break;
+					case 3: /* --skip-bad-blocks-to-start */
+						skip_bad_blocks_to_start = true;
+						break;
 				}
 				break;
 			case 'V':
@@ -397,6 +404,22 @@ int main(int argc, char * const argv[])
 				mtd.min_io_size);
 		goto closeall;
 	}
+	if (skip_bad_blocks_to_start) {
+		long long bbs_offset = 0;
+		while (bbs_offset < start_addr) {
+			err = mtd_is_bad(&mtd, fd, bbs_offset / mtd.eb_size);
+			if (err < 0) {
+				sys_errmsg("%s: MTD get bad block failed", mtddev);
+				goto closeall;
+			} else if (err == 1) {
+				if (!quiet)
+					fprintf(stderr, "Bad block at %llx\n", bbs_offset);
+				start_addr += mtd.eb_size;
+			}
+			bbs_offset += mtd.eb_size;
+		}
+	}
+
 	if (length)
 		end_addr = start_addr + length;
 	if (!length || end_addr > mtd.size)
Signature · 1 line
2.1.4
#3 lore raw
In reply to Mike Crowe
Quoted 17 lines · Mike Crowe

I found myself needing to write and verify part of a partition that will be read by the bootloader by skipping blocks from the start of the partition. Initially I added separate --input-skip and --output-skip options but David Oberhollenzer suggested in <cdfdbe5d-1aa8-e424-7691-338d0d63f7b1 at sigma-star.at>
http://lists.infradead.org/pipermail/linux-mtd/2016-December/070855.html
that a simple flag to indicate that the start address should skip bad blocks would avoid ambiguity.

David also suggested that I skip the bad blocks in a separate step before the already-cluttered main loop.

This version uses is_virt_block_bad in nandwrite, so the series now depends on David Oberhollenzer's virtual block handling fixes from <[email protected]>
http://lists.infradead.org/pipermail/linux-mtd/2017-January/071544.html

Quoted 8 lines

Mike Crowe (2):
nandwrite: Add --skip-bad-blocks-to-start option
nanddump: Add --skip-bad-blocks-to-start option

 nand-utils/nanddump.c  | 23 +++++++++++++++++++++++
 nand-utils/nandwrite.c | 26 ++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

Applied to mtd-utils.git.

Thanks,

David