diff mbox series

mtd-utils: flash_erase: Add an option for JFFS2 cleanmarker size

Message ID 20231107092357.26360-1-Takahiro.Kuwano@infineon.com
State New
Headers show
Series mtd-utils: flash_erase: Add an option for JFFS2 cleanmarker size | expand

Commit Message

Takahiro Kuwano Nov. 7, 2023, 9:23 a.m. UTC
From: Takahiro Kuwano <Takahiro.Kuwano@infineon.com>

JFFS2 supports buffer mode for ECC'd NOR Flash that cleanmarker size
is rounded up to mtd->writesize, while the '-j' option in flash_erase
utility uses fixed 12 bytes. That makes flash program ops unaligned to
mtd->writesize and causes program error or disables ECC.

The mkfs.jffs2 utility supports '-c' option that allows users to specify
cleanmarker size. This patch implements '-c' option in the flash_erase
that can be used along with '-j' option.

Signed-off-by: Takahiro Kuwano <Takahiro.Kuwano@infineon.com>
---
 misc-utils/flash_erase.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

Comments

David Oberhollenzer Nov. 13, 2023, 8:06 a.m. UTC | #1
Applied to mtd-utils.git master.

Thanks,

David
diff mbox series

Patch

diff --git a/misc-utils/flash_erase.c b/misc-utils/flash_erase.c
index 000f94a..c6f6f66 100644
--- a/misc-utils/flash_erase.c
+++ b/misc-utils/flash_erase.c
@@ -64,13 +64,14 @@  static void display_help (void)
 			"Erase blocks of the specified MTD device.\n"
 			"Specify a count of 0 to erase to end of device.\n"
 			"\n"
-			"  -j, --jffs2       format the device for jffs2\n"
-			"  -N, --noskipbad   don't skip bad blocks\n"
-			"  -u, --unlock      unlock sectors before erasing\n"
-			"  -q, --quiet       do not display progress messages\n"
-			"      --silent      same as --quiet\n"
-			"      --help        display this help and exit\n"
-			"      --version     output version information and exit\n",
+			"  -j, --jffs2             format the device for jffs2\n"
+			"  -c, --cleanmarker=SIZE  size of jffs2 cleanmarker (default 12)\n"
+			"  -N, --noskipbad         don't skip bad blocks\n"
+			"  -u, --unlock            unlock sectors before erasing\n"
+			"  -q, --quiet             do not display progress messages\n"
+			"      --silent            same as --quiet\n"
+			"      --help              display this help and exit\n"
+			"      --version           output version information and exit\n",
 			"\n"
 			"  MTD_DEVICE  MTD device node or 'mtd:<name>'\n"
 			PROGRAM_NAME);
@@ -115,7 +116,7 @@  int main(int argc, char *argv[])
 {
 	libmtd_t mtd_desc;
 	struct mtd_dev_info mtd;
-	int fd, cmlen = 8;
+	int fd, cmlen = 8, cmsize = sizeof(cleanmarker);
 	unsigned long long start;
 	unsigned int eb, eb_start, eb_cnt;
 	bool isNAND, erase_chip = false;
@@ -127,11 +128,12 @@  int main(int argc, char *argv[])
 	 */
 	for (;;) {
 		int option_index = 0;
-		static const char *short_options = "jNquVh";
+		static const char *short_options = "jc:NquVh";
 		static const struct option long_options[] = {
 			{"help", no_argument, 0, 'h'},
 			{"version", no_argument, 0, 'V'},
 			{"jffs2", no_argument, 0, 'j'},
+			{"cleanmarker", required_argument, 0, 'c'},
 			{"noskipbad", no_argument, 0, 'N'},
 			{"quiet", no_argument, 0, 'q'},
 			{"silent", no_argument, 0, 'q'},
@@ -155,6 +157,9 @@  int main(int argc, char *argv[])
 		case 'j':
 			jffs2 = 1;
 			break;
+		case 'c':
+			cmsize = atoi(optarg);
+			break;
 		case 'N':
 			noskipbad = 1;
 			break;
@@ -207,6 +212,10 @@  int main(int argc, char *argv[])
 
 	if (jffs2 && mtd.type == MTD_MLCNANDFLASH)
 		return errmsg("JFFS2 cannot support MLC NAND.");
+	if (jffs2 && cmsize < sizeof(cleanmarker))
+		return errmsg("cleanmarker size must be >= 12");
+	if (jffs2 && cmsize >= mtd.eb_size)
+		return errmsg("cleanmarker size must be < eraseblock size");
 
 	eb_start = start / mtd.eb_size;
 
@@ -216,7 +225,7 @@  int main(int argc, char *argv[])
 		cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
 		cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
 		if (!isNAND) {
-			cleanmarker.totlen = cpu_to_je32(sizeof(cleanmarker));
+			cleanmarker.totlen = cpu_to_je32(cmsize);
 		} else {
 			cleanmarker.totlen = cpu_to_je32(8);
 			cmlen = min(mtd.oobavail, 8);