All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stas Sergeev <stsp@list.ru>
To: linux-mtd@lists.infradead.org
Cc: Stas Sergeev <stsp@users.sourceforge.net>
Subject: [PATCH 1/2] nandmarkblk: initial version
Date: Wed, 23 Apr 2014 19:50:11 +0400	[thread overview]
Message-ID: <5357E133.5040205@list.ru> (raw)
In-Reply-To: <5357D808.5070101@list.ru>

Trying again, with inlined patch...


-=-=-=-=-=-=-=-=-=# Don't remove this line #=-=-=-=-=-=-=-=-=-
The tool to mark nand blocks as bad.
In the future - also as good.

Signed-off-by: Stas Sergeev <stsp@users.sourceforge.net>
---
 Makefile      |   2 +-
 nandmarkblk.c | 151
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 152 insertions(+), 1 deletion(-)
 create mode 100644 nandmarkblk.c

diff --git a/Makefile b/Makefile
index d316a3d..acb9837 100644
--- a/Makefile
+++ b/Makefile
@@ -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 \
diff --git a/nandmarkblk.c b/nandmarkblk.c
new file mode 100644
index 0000000..127219c
--- /dev/null
+++ b/nandmarkblk.c
@@ -0,0 +1,151 @@
+/*
+ *  nandmarkblk.c
+ *
+ *  Author: Stas Sergeev <stsp@users.sourceforge.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#define PROGRAM_NAME "nandmarkblk"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <getopt.h>
+#include <sys/ioctl.h>
+#include "mtd/mtd-user.h"
+#include "common.h"
+#include <libmtd.h>
+
+static void display_help(int status)
+{
+    fprintf(status == EXIT_SUCCESS ? stdout : stderr,
+"Usage: nandwrite [OPTION] MTD_DEVICE [INPUTFILE|-]\n"
+"Writes to the specified MTD device.\n"
+"\n"
+"  -b num, --markbad=num   Mark block 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"
+    );
+    exit(status);
+}
+
+static void display_version(void)
+{
+    printf("%1$s " VERSION "\n"
+            "\n"
+            "%1$s comes with NO WARRANTY\n"
+            "to the extent permitted by law.\n"
+            "\n"
+            "You may redistribute copies of %1$s\n"
+            "under the terms of the GNU General Public Licence.\n"
+            "See the file `COPYING' for more information.\n",
+            PROGRAM_NAME);
+    exit(EXIT_SUCCESS);
+}
+
+static const char    *mtd_device;
+static bool        quiet = false;
+static int        markbad = -1;
+static int        markgood = -1;
+
+static void process_options(int argc, char * const argv[])
+{
+    int error = 0;
+
+    for (;;) {
+        int option_index = 0;
+        static const char short_options[] = "vhb:g:q";
+        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'},
+            {"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;
+}
-- 
1.7.11.7

  parent reply	other threads:[~2014-04-23 15:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-23 15:11 [PATCH 0/2] mtd-utils: add nandmarkblk tool Stas Sergeev
2014-04-23 15:12 ` [PATCH 1/2] nandmarkblk: initial version Stas Sergeev
2014-04-23 15:13 ` [PATCH 2/2] nandmarkblk: erase block before marking bad Stas Sergeev
2014-04-23 15:50 ` Stas Sergeev [this message]
2014-04-23 15:54 ` Stas Sergeev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5357E133.5040205@list.ru \
    --to=stsp@list.ru \
    --cc=linux-mtd@lists.infradead.org \
    --cc=stsp@users.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.