mtd-utils: add nandmarkblk tool
Subsystems (2)
cover mtd-utils: add nandmarkblk tool
Hello.
I have googled and queried this list, and it seems, as unbelievable as it is, there is no tool to mark nand blocks as bad. So I wrote one myself.
The patches will follow.
PATCH 1/2 nandmarkblk: initial version
From a0b70b6776721a059601052c8113693be9deadab Mon Sep 17 00:00:00 2001
From: Stas Sergeev <[email protected]>
Date: Tue, 22 Apr 2014 20:26:32 +0400
Subject: [PATCH 1/2] nandmarkblk: initial version
The tool to mark nand blocks as bad.
In the future - also as good.
--- Makefile | 2 +- nandmarkblk.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 nandmarkblk.c
Makefile +1 -1
@@ -20,7 +20,7 @@ MTD_BINS = \ ftl_format flash_erase nanddump doc_loadbios \ ftl_check mkfs.jffs2 flash_lock flash_unlock \ flash_otp_info flash_otp_dump flash_otp_lock flash_otp_write \ - mtd_debug flashcp nandwrite nandtest \ + mtd_debug flashcp nandwrite nandtest nandmarkblk \ jffs2dump \ nftldump nftl_format docfdisk \ rfddump rfdformat \
nandmarkblk.c new file +151 -0
151 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH 1/2 nandmarkblk: initial version
In reply to Stas SergeevTrying again, with inlined patch...
-=-=-=-=-=-=-=-=-=# Don't remove this line #=-=-=-=-=-=-=-=-=- The tool to mark nand blocks as bad.
In the future - also as good.
--- Makefile | 2 +- nandmarkblk.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 nandmarkblk.c
Makefile +1 -1
@@ -20,7 +20,7 @@ MTD_BINS = \ ftl_format flash_erase nanddump doc_loadbios \ ftl_check mkfs.jffs2 flash_lock flash_unlock \ flash_otp_info flash_otp_dump flash_otp_lock flash_otp_write \ - mtd_debug flashcp nandwrite nandtest \ + mtd_debug flashcp nandwrite nandtest nandmarkblk \ jffs2dump \ nftldump nftl_format docfdisk \ rfddump rfdformat \
nandmarkblk.c new file +66 -0
66 lines not on this page · raw message
option_index. */
+ {"version", no_argument, 0, 'v'},
+ {"help", no_argument, 0, 'h'},
+ {"markbad", required_argument, 0, 'b'},
+ {"markgood", required_argument, 0, 'g'},
+ {"quiet", no_argument, 0, 'q'},
+ {0, 0, 0, 0},+ };
+
+ int c = getopt_long(argc, argv, short_options,
+ long_options, &option_index);
+ if (c == EOF)
+ break;
+
+ switch (c) {
+ case 'v':
+ display_version();
+ break;
+ case 'q':
+ quiet = true;
+ break;
+ case 'g':
+ markgood = simple_strtoll(optarg, &error);
+ break;
+ case 'b':
+ markbad = simple_strtoll(optarg, &error);
+ break;
+ case 'h':
+ display_help(EXIT_SUCCESS);
+ break;
+ case '?':
+ error++;
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1 || error)
+ display_help(EXIT_FAILURE);
+
+ mtd_device = argv[0];
+}
+
+/*
+ * Main program
+ */
+int main(int argc, char * const argv[])
+{
+ int fd = -1; + struct mtd_dev_info mtd; + /* contains all the data read from the file so far for the current
eraseblock */
+ libmtd_t mtd_desc;
+
+ process_options(argc, argv);
+
+ /* Open the device */
+ if ((fd = open(mtd_device, O_RDWR)) == -1)
+ sys_errmsg_die("%s", mtd_device);
+
+ mtd_desc = libmtd_open();
+ if (!mtd_desc)
+ errmsg_die("can't initialize libmtd");
+
+ /* Fill in MTD device capability structure */
+ if (mtd_get_dev_info(mtd_desc, mtd_device, &mtd) < 0)
+ errmsg_die("mtd_get_dev_info failed");
+
+ if (markbad != -1 && mtd_mark_bad(&mtd, fd, markbad)) {
+ sys_errmsg("%s: MTD Mark bad block failure", mtd_device);
+ goto closeall;
+ }
+
+ if (markgood != -1) {
+ sys_errmsg("%s: MTD Mark good is not implemented", mtd_device);
+ goto closeall;
+ }
+
+closeall:
+ libmtd_close(mtd_desc);
+ close(fd);
+
+ /* Return happy */
+ return EXIT_SUCCESS;
+}
Signature · 1 line
1.7.11.7
PATCH 2/2 nandmarkblk: erase block before marking bad
In reply to Stas SergeevFrom c42a4390a7a5cc072fdac33099ff4ceb966e2a6a Mon Sep 17 00:00:00 2001
From: Stas Sergeev <[email protected]>
Date: Wed, 23 Apr 2014 17:49:31 +0400
Subject: [PATCH 2/2] nandmarkblk: erase block before marking bad
--- nandmarkblk.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
nandmarkblk.c +16 -4
@@ -29,6 +29,7 @@ static void display_help(int status)"Writes to the specified MTD device.\n" "\n" " -b num, --markbad=num Mark block bad\n" +" -n --noerase Don't erase block before marking it bad\n" " -q, --quiet Don't display progress messages\n" " -h, --help Display this help and exit\n" " --version Output version information and exit\n"
@@ -54,6 +55,7 @@ static const char *mtd_device;static bool quiet = false;static int markbad =-1 ;static int markgood =-1 ; +static bool noerase = false;static void process_options (int argc,char *const argv[]) {
@@ -61,13 +63,14 @@ static void process_options(int argc, char * const argv[])for (;;) {int option_index =0 ; -static const char short_options[] ="vhb:g:q" ; +static const char short_options[] ="vhb:g:nq" ;static const struct option long_options[] = {/* Order of these args with val==0 matters; see option_index. */ {"version" , no_argument,0 ,'v' }, {"help" , no_argument,0 ,'h' }, {"markbad" , required_argument,0 ,'b' }, {"markgood" , required_argument,0 ,'g' }, + {"noerase" , no_argument,0 ,'n' }, {"quiet" , no_argument,0 ,'q' }, {0 ,0 ,0 ,0 }, };
@@ -90,6 +93,9 @@ static void process_options(int argc, char * const argv[])case 'b' : markbad =simple_strtoll (optarg, &error);break ; +case 'n' : + noerase = true; +break ;case 'h' :display_help (EXIT_SUCCESS );break ;
@@ -132,9 +138,15 @@ int main(int argc, char * const argv[])if (mtd_get_dev_info (mtd_desc, mtd_device, &mtd) <0 )errmsg_die ("mtd_get_dev_info failed" ); -if (markbad !=-1 &&mtd_mark_bad (&mtd, fd, markbad)) { -sys_errmsg ("%s: MTD Mark bad block failure" , mtd_device); - goto closeall; +if (markbad !=-1 ) { +if (!noerase &&mtd_erase (mtd_desc, &mtd, fd, markbad) !=0 ) { +sys_errmsg ("%s: MTD Erase failure" , mtd_device); + goto closeall; + } +if (mtd_mark_bad (&mtd, fd, markbad)) { +sys_errmsg ("%s: MTD Mark bad block failure" , mtd_device); + goto closeall; + } }if (markgood !=-1 ) {
Signature · 1 line
1.7.11.7
PATCH 2/2 nandmarkblk: erase block before marking bad
In reply to Stas SergeevAdd ability to erase blocks before marking, and an option to not do that.
--- nandmarkblk.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
nandmarkblk.c +2 -0
@@ -29,6 +29,7 @@ static void display_help(int status)"Writes to the specified MTD device.\n" "\n" " -b num, --markbad=num Mark block bad\n" +" -n --noerase Don't erase block before marking it bad\n" " -q, --quiet Don't display progress messages\n" " -h, --help Display this help and exit\n" " --version Output version information and exit\n"
@@ -54,6 +55,7 @@ static const char *mtd_device;static bool quiet = false;static int markbad =-1 ;static int markgood =-1 ; +static bool noerase = false;static void process_options (int argc,char *const argv[]) {
@@ -61,13 +63,14 @@ static void process_options(int argc, char * const
argv[])
for (;;) {
int option_index = 0;
- static const char short_options[] = "vhb:g:q";
+ static const char short_options[] = "vhb:g:nq";
static const struct option long_options[] = {
/* Order of these args with val==0 matters; see
option_index. */
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{"markbad", required_argument, 0, 'b'},
{"markgood", required_argument, 0, 'g'},
+ {"noerase", no_argument, 0, 'n'},
{"quiet", no_argument, 0, 'q'},
{0, 0, 0, 0},
};
@@ -90,6 +93,9 @@ static void process_options(int argc, char * const argv[])
case 'b':
markbad = simple_strtoll(optarg, &error);
break;
+ case 'n':
+ noerase = true;
+ break;
case 'h':
display_help(EXIT_SUCCESS);
break;
@@ -132,9 +138,15 @@ int main(int argc, char * const argv[])
if (mtd_get_dev_info(mtd_desc, mtd_device, &mtd) < 0)
errmsg_die("mtd_get_dev_info failed");- if (markbad != -1 && mtd_mark_bad(&mtd, fd, markbad)) {
- sys_errmsg("%s: MTD Mark bad block failure", mtd_device);
- goto closeall;
+ if (markbad != -1) {
+ if (!noerase && mtd_erase(mtd_desc, &mtd, fd, markbad) != 0) {
+ sys_errmsg("%s: MTD Erase failure", mtd_device);
+ goto closeall;
+ }
+ if (mtd_mark_bad(&mtd, fd, markbad)) {
+ sys_errmsg("%s: MTD Mark bad block failure", mtd_device);
+ goto closeall;
+ }
}
if (markgood != -1) {Signature · 1 line
1.7.11.7