linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Cernekee <cernekee@gmail.com>
To: <dedekind1@gmail.com>, <saeed.bishara@gmail.com>,
	<jwboyer@gmail.com>, <vapier.adi@gmail.com>
Cc: linux-mtd@lists.infradead.org
Subject: [PATCHv2 5/5] mtd-utils: change flash_eraseall to use libmtd-wrapped ioctls
Date: Wed, 7 Jul 2010 17:30:14 -0700	[thread overview]
Message-ID: <5bd45316e40b10460d6290a8ca84daa9@localhost> (raw)
In-Reply-To: <eb53f5a0215250d7e523de47d15b0d12@localhost>

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
---
 flash_eraseall.c |   58 +++++++++++++++++++++++++++--------------------------
 1 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/flash_eraseall.c b/flash_eraseall.c
index e6f8d50..916a75a 100644
--- a/flash_eraseall.c
+++ b/flash_eraseall.c
@@ -36,6 +36,7 @@
 #include <sys/ioctl.h>
 #include <sys/mount.h>
 #include <crc32.h>
+#include <libmtd.h>
 
 #include <mtd/mtd-user.h>
 #include <mtd/jffs2-user.h>
@@ -49,7 +50,7 @@ static int quiet;		/* true -- don't output progress */
 static int jffs2;		// format for jffs2 usage
 
 static void process_options (int argc, char *argv[]);
-void show_progress (mtd_info_t *meminfo, erase_info_t *erase);
+void show_progress (struct mtd_dev_info *mtd, uint64_t start);
 static void display_help (void);
 static void display_version (void);
 static struct jffs2_unknown_node cleanmarker;
@@ -57,26 +58,31 @@ int target_endian = __BYTE_ORDER;
 
 int main (int argc, char *argv[])
 {
-	mtd_info_t meminfo;
-	int fd, clmpos = 0, clmlen = 8;
-	erase_info_t erase;
+	libmtd_t mtd_desc;
+	struct mtd_dev_info mtd;
+	int fd, clmpos = 0, clmlen = 8, eb;
 	int isNAND, bbtest = 1;
+	uint64_t offset = 0;
 
 	process_options(argc, argv);
 
+	mtd_desc = libmtd_open();
+	if (mtd_desc == NULL) {
+		fprintf(stderr, "%s: can't initialize libmtd\n", exe_name);
+		return 1;
+	}
+
 	if ((fd = open(mtd_device, O_RDWR)) < 0) {
 		fprintf(stderr, "%s: %s: %s\n", exe_name, mtd_device, strerror(errno));
 		return 1;
 	}
 
-
-	if (ioctl(fd, MEMGETINFO, &meminfo) != 0) {
-		fprintf(stderr, "%s: %s: unable to get MTD device info\n", exe_name, mtd_device);
+	if (mtd_get_dev_info(mtd_desc, mtd_device, &mtd) < 0) {
+		fprintf(stderr, "%s: mtd_get_dev_info failed\n", exe_name);
 		return 1;
 	}
 
-	erase.length = meminfo.erasesize;
-	isNAND = meminfo.type == MTD_NANDFLASH ? 1 : 0;
+	isNAND = mtd.type == MTD_NANDFLASH ? 1 : 0;
 
 	if (jffs2) {
 		cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
@@ -104,7 +110,7 @@ int main (int argc, char *argv[])
 					clmlen = 8;
 			} else {
 				/* Legacy mode */
-				switch (meminfo.oobsize) {
+				switch (mtd.oob_size) {
 					case 8:
 						clmpos = 6;
 						clmlen = 2;
@@ -124,13 +130,13 @@ int main (int argc, char *argv[])
 		cleanmarker.hdr_crc =  cpu_to_je32 (crc32 (0, &cleanmarker,  sizeof (struct jffs2_unknown_node) - 4));
 	}
 
-	for (erase.start = 0; erase.start < meminfo.size; erase.start += meminfo.erasesize) {
+	for (eb = 0; eb < (mtd.size / mtd.eb_size); eb++) {
+		offset = eb * mtd.eb_size;
 		if (bbtest) {
-			loff_t offset = erase.start;
-			int ret = ioctl(fd, MEMGETBADBLOCK, &offset);
+			int ret = mtd_is_bad(&mtd, fd, eb);
 			if (ret > 0) {
 				if (!quiet)
-					printf ("\nSkipping bad block at 0x%08x\n", erase.start);
+					printf ("\nSkipping bad block at 0x%08llx\n", (unsigned long long)offset);
 				continue;
 			} else if (ret < 0) {
 				if (errno == EOPNOTSUPP) {
@@ -147,9 +153,9 @@ int main (int argc, char *argv[])
 		}
 
 		if (!quiet)
-			show_progress(&meminfo, &erase);
+			show_progress(&mtd, offset);
 
-		if (ioctl(fd, MEMERASE, &erase) != 0) {
+		if (mtd_erase(&mtd, fd, eb) != 0) {
 			fprintf(stderr, "\n%s: %s: MTD Erase failure: %s\n", exe_name, mtd_device, strerror(errno));
 			continue;
 		}
@@ -160,16 +166,12 @@ int main (int argc, char *argv[])
 
 		/* write cleanmarker */
 		if (isNAND) {
-			struct mtd_oob_buf oob;
-			oob.ptr = (unsigned char *) &cleanmarker;
-			oob.start = erase.start + clmpos;
-			oob.length = clmlen;
-			if (ioctl (fd, MEMWRITEOOB, &oob) != 0) {
+			if (mtd_write_oob(&mtd, fd, offset + clmpos, clmlen, &cleanmarker) != 0) {
 				fprintf(stderr, "\n%s: %s: MTD writeoob failure: %s\n", exe_name, mtd_device, strerror(errno));
 				continue;
 			}
 		} else {
-			if (lseek (fd, erase.start, SEEK_SET) < 0) {
+			if (lseek (fd, (loff_t)offset, SEEK_SET) < 0) {
 				fprintf(stderr, "\n%s: %s: MTD lseek failure: %s\n", exe_name, mtd_device, strerror(errno));
 				continue;
 			}
@@ -179,10 +181,10 @@ int main (int argc, char *argv[])
 			}
 		}
 		if (!quiet)
-			printf (" Cleanmarker written at %x.", erase.start);
+			printf (" Cleanmarker written at %llx.", (unsigned long long)offset);
 	}
 	if (!quiet) {
-		show_progress(&meminfo, &erase);
+		show_progress(&mtd, offset);
 		printf("\n");
 	}
 
@@ -250,11 +252,11 @@ void process_options (int argc, char *argv[])
 	mtd_device = argv[optind];
 }
 
-void show_progress (mtd_info_t *meminfo, erase_info_t *erase)
+void show_progress (struct mtd_dev_info *mtd, uint64_t start)
 {
-	printf("\rErasing %d Kibyte @ %x -- %2llu %% complete.",
-		meminfo->erasesize / 1024, erase->start,
-		(unsigned long long) erase->start * 100 / meminfo->size);
+	printf("\rErasing %d Kibyte @ %llx -- %2llu %% complete.",
+		mtd->eb_size / 1024, (unsigned long long)start,
+		(unsigned long long) start * 100 / mtd->size);
 	fflush(stdout);
 }
 
-- 
1.7.0.4

  parent reply	other threads:[~2010-07-08  0:42 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-08  0:30 [PATCHv2 1/5] mtd-utils: move libmtd source files to lib/ subdirectory Kevin Cernekee
2010-07-08  0:30 ` [PATCHv2 2/5] mtd-utils: update Makefiles, source files to use common libmtd.a Kevin Cernekee
2010-07-08  0:30 ` [PATCHv2 3/5] mtd-utils: update to latest mtd-abi.h from kernel.org Kevin Cernekee
2010-07-13 10:38   ` Artem Bityutskiy
2010-07-08  0:30 ` [PATCHv2 4/5] libmtd: add support for 64-bit offsets, OOB Kevin Cernekee
2010-07-13 10:49   ` Artem Bityutskiy
2010-07-14  0:09     ` Kevin Cernekee
2010-07-14  2:51       ` Artem Bityutskiy
2010-07-17  7:35     ` Artem Bityutskiy
2010-07-13 10:50   ` Artem Bityutskiy
2010-07-17 17:08   ` Artem Bityutskiy
2010-07-18  4:26     ` Artem Bityutskiy
2010-07-24  1:07       ` Kevin Cernekee
2010-07-26  5:57         ` Artem Bityutskiy
2010-07-24  2:43     ` Kevin Cernekee
2010-07-26  5:36       ` Artem Bityutskiy
2010-07-08  0:30 ` Kevin Cernekee [this message]
2010-07-13 10:35 ` [PATCHv2 1/5] mtd-utils: move libmtd source files to lib/ subdirectory Artem Bityutskiy

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=5bd45316e40b10460d6290a8ca84daa9@localhost \
    --to=cernekee@gmail.com \
    --cc=dedekind1@gmail.com \
    --cc=jwboyer@gmail.com \
    --cc=linux-mtd@lists.infradead.org \
    --cc=saeed.bishara@gmail.com \
    --cc=vapier.adi@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).