From patchwork Mon Jul 5 22:10:38 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Cernekee X-Patchwork-Id: 57934 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 76957B6F05 for ; Tue, 6 Jul 2010 08:39:40 +1000 (EST) Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.72 #1 (Red Hat Linux)) id 1OVuI1-0003Cu-OC; Mon, 05 Jul 2010 22:37:25 +0000 Received: from [69.28.251.93] (helo=b32.net) by bombadil.infradead.org with esmtps (Exim 4.72 #1 (Red Hat Linux)) id 1OVuHy-0003Bt-Rg for linux-mtd@lists.infradead.org; Mon, 05 Jul 2010 22:37:24 +0000 Received: (qmail 26075 invoked from network); 5 Jul 2010 22:37:20 -0000 Received: from unknown (HELO vps-1001064-677.cp.jvds.com) (127.0.0.1) by 127.0.0.1 with (DHE-RSA-AES128-SHA encrypted) SMTP; 5 Jul 2010 22:37:20 -0000 Received: by vps-1001064-677.cp.jvds.com (sSMTP sendmail emulation); Mon, 05 Jul 2010 15:37:20 -0700 From: Kevin Cernekee Date: Mon, 5 Jul 2010 15:10:38 -0700 Subject: [PATCH 5/5] mtd-utils: change flash_eraseall to use libmtd-wrapped ioctls Message-Id: References: <4822394121cd0a8f08b0bd3ecd6af199@localhost> In-Reply-To: <4822394121cd0a8f08b0bd3ecd6af199@localhost> To: , , User-Agent: vim 7.1 MIME-Version: 1.0 Content-Disposition: inline X-CRM114-Version: 20090807-BlameThorstenAndJenny ( TRE 0.7.6 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20100705_183723_705182_56297C8F X-CRM114-Status: GOOD ( 22.78 ) X-Spam-Score: 2.5 (++) X-Spam-Report: SpamAssassin version 3.3.1 on bombadil.infradead.org summary: Content analysis details: (2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 FREEMAIL_FROM Sender email is freemail (cernekee[at]gmail.com) 0.0 DKIM_ADSP_CUSTOM_MED No valid author signature, adsp_override is CUSTOM_MED 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS 1.2 NML_ADSP_CUSTOM_MED ADSP custom_med hit, and not from a mailing list Cc: linux-mtd@lists.infradead.org X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Signed-off-by: Kevin Cernekee --- flash_eraseall.c | 58 +++++++++++++++++++++++++++-------------------------- 1 files changed, 30 insertions(+), 28 deletions(-) diff --git a/flash_eraseall.c b/flash_eraseall.c index e6f8d50..b8222ad 100644 --- a/flash_eraseall.c +++ b/flash_eraseall.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -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(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); }