[PATCHv3] nandwrite: new mtd-utils applet

Baruch Siach baruch at tkos.co.il
Tue Aug 24 12:19:46 UTC 2010


Signed-off-by: Baruch Siach <baruch at tkos.co.il>
---
Changes from v2:

	Don't give wrong error message when mtdoffset + meminfo.size == input 
	size

	Addressed the comments of Denys Vlasenko:

	* Remove redundant NULL initialization
	* Delegate argv[] validity check to getopt32
	* Document padding behaviour
	* Clarify error message when padding is needed
	* Simplify the padding check code

Changes from v1:

	Addressed the comments of Denys Vlasenko:

	* s/getopt/getopt32/
	* Don't use argc for smaller code
	* s/xopen/xfopen_stdin/
	* Zero pad when -p is present

 miscutils/nandwrite.c |  130 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 130 insertions(+), 0 deletions(-)
 create mode 100644 miscutils/nandwrite.c

diff --git a/miscutils/nandwrite.c b/miscutils/nandwrite.c
new file mode 100644
index 0000000..503b00e
--- /dev/null
+++ b/miscutils/nandwrite.c
@@ -0,0 +1,130 @@
+/*
+ * nandwrite.c - ported to busybox from mtd-utils
+ *
+ * Author: Baruch Siach <baruch at tkos.co.il>, Orex Computed Radiography
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
+ *
+ * TODO: add support for large (>4GB) MTD devices
+ */
+
+//applet:IF_NANDWRITE(APPLET(nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
+
+//config:config NANDWRITE
+//config:	bool "nandwrite"
+//config:	default n
+//config:	depends on PLATFORM_LINUX
+//config:	help
+//config:	  Write to the specified MTD device, with bad blocks awareness
+
+#include "libbb.h"
+#include <mtd/mtd-user.h>
+
+#define OPTION_P	(1 << 0)
+#define OPTION_S	(1 << 1)
+
+//usage:#define nandwrite_trivial_usage
+//usage:	"MTD_DEVICE [-p] [-s ADDR] [INPUTFILE | -]"
+//usage:#define nandwrite_full_usage "\n\n"
+//usage:	"Write to the specified MTD device\n"
+//usage:	"\nOptions:"
+//usage:	"\n	-p	Pad to page size"
+//usage:	"\n	-s addr	Set start address (default is 0)"
+
+static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo, 
+		unsigned block_offset)
+{
+	loff_t offs = block_offset;
+
+	while (1) {
+		if (block_offset >= meminfo->size)
+			bb_error_msg_and_die("not enough space in MTD device");
+		if (xioctl(fd, MEMGETBADBLOCK, &offs)) {
+			offs += meminfo->erasesize;
+			printf("Skipping bad block at 0x%08llx\n", offs);
+		} else
+			return offs;
+	}
+
+}
+
+int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int nandwrite_main(int argc UNUSED_PARAM, char **argv)
+{
+	unsigned opts;
+	char *opt_s;
+	char *mtd_device, *img;
+	unsigned mtdoffset = 0, blockstart;
+	int fd, ifd = STDIN_FILENO;
+	struct mtd_info_user meminfo;
+	unsigned char *filebuf;
+	ssize_t cnt = -1;
+
+	opt_complementary = "-1:?2";
+	opts = getopt32(argv, "ps:", &opt_s);
+	if (opts & OPTION_S)
+		mtdoffset = bb_strtou(opt_s, NULL, 0);
+	argv += optind;
+	mtd_device = argv[0];
+	img = argv[1];
+
+	fd = xopen(mtd_device, O_RDWR);
+
+	xioctl(fd, MEMGETINFO, &meminfo);
+
+	if (mtdoffset & (meminfo.writesize - 1))
+		bb_error_msg_and_die("start address is not page aligned");
+
+	if (img)
+		ifd = fileno(xfopen_stdin(img));
+
+	filebuf = xmalloc(meminfo.writesize);
+
+	while (mtdoffset < meminfo.size) {
+		blockstart = mtdoffset & (~meminfo.erasesize + 1);
+		if (blockstart == mtdoffset) {
+			/* starting a new eraseblock */
+			mtdoffset = next_good_eraseblock(fd, &meminfo,
+					blockstart);
+			printf("Writing at 0x%08x\n", mtdoffset);
+		}
+
+		/* get some more data from input */
+		cnt = full_read(ifd, filebuf, meminfo.writesize);
+		if (cnt == 0)
+			/*
+			 * we are done; no need for padding since we have
+			 * filled the write block
+			 */
+			break;
+		if (cnt < meminfo.writesize) {
+			if (!(opts & OPTION_P))
+				bb_error_msg_and_die("unexpected EOF, "
+						"use -p to zero pad");
+			/* zero pad to end of write block */
+			memset(filebuf + cnt, 0, meminfo.writesize - cnt);
+		}
+
+		xlseek(fd, mtdoffset, SEEK_SET);
+		xwrite(fd, filebuf, meminfo.writesize);
+		mtdoffset += meminfo.writesize;
+	}
+
+	/*
+	 * the extra full_read() check is needed for the boundary case of
+	 * mtdoffset + meminfo.size == input size
+	 */
+	if (cnt != 0 && full_read(ifd, filebuf, meminfo.writesize) != 0)
+		/* didn't reach input EOF */
+		bb_error_msg_and_die("not enough space in MTD device");
+
+	if (ENABLE_FEATURE_CLEAN_UP) {
+		free(filebuf);
+		close(ifd);
+		close(fd);
+	}
+
+	return EXIT_SUCCESS;
+}
-- 
1.7.1



More information about the busybox mailing list