+//usage:#define nanddump_trivial_usage
+//usage: "[-no]" IF_LONG_OPTS(" [--bb padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
+//usage:#define nanddump_full_usage "\n\n"
+//usage: "Dump MTD_DEVICE\n"
+//usage: "\n -n Read without ecc"
+//usage: "\n -o Dump oob data"
+//usage: "\n -s ADDR Start address"
+//usage: "\n -l LEN Length"
+//usage: "\n -f FILE Dump to file ('-' for stdout)"
+//usage: IF_LONG_OPTS(
+//usage: "\n --bb METHOD"
+//usage: "\n skipbad: skip bad blocks"
+//usage: "\n padbad: substitute bad blocks by 0xff (default)"
+//usage: )
+
+#include "libbb.h"
+#include <mtd/mtd-user.h>
+
+/* Old headers call it MTD_MODE_RAW.
+ * FIXME: In kernel headers, MTD_FILE_MODE_RAW is not a define,
+ * it's an enum. How I can test for existence of an enum?
+ */
+#if !defined(MTD_FILE_MODE_RAW)
+# define MTD_FILE_MODE_RAW 3
+#endif
+
+
+#define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
+#define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
+
+#define OPT_p (1 << 0) /* nandwrite only */
+#define OPT_o (1 << 0) /* nanddump only */
+#define OPT_n (1 << 1)
+#define OPT_s (1 << 2)
+#define OPT_f (1 << 3)
+#define OPT_l (1 << 4)
+#define OPT_bb (1 << 5) /* must be the last one in the list */
+
+#define BB_PADBAD (1 << 0)
+#define BB_SKIPBAD (1 << 1)
+
+/* helper for writing out 0xff for bad blocks pad */
+static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
+{
+ unsigned char buf[meminfo->writesize];
+ unsigned count;
+
+ /* round len to the next page only if len is not already on a page */
+ len = ((len - 1) | (meminfo->writesize - 1)) + 1;
+
+ memset(buf, 0xff, sizeof(buf));
+ for (count = 0; count < len; count += meminfo->writesize) {
+ xwrite(STDOUT_FILENO, buf, meminfo->writesize);
+ if (oob)
+ xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
+ }
+}
+