mtd/util eraseall.c,1.7,1.8
gleixner at infradead.org
gleixner at infradead.org
Tue Feb 18 04:04:53 EST 2003
Update of /home/cvs/mtd/util
In directory phoenix.infradead.org:/tmp/cvs-serv9931
Modified Files:
eraseall.c
Log Message:
added -j (jffs2) option to format for JFFS2 by writing cleanmarkers
Index: eraseall.c
===================================================================
RCS file: /home/cvs/mtd/util/eraseall.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- eraseall.c 25 Apr 2001 07:11:19 -0000 1.7
+++ eraseall.c 18 Feb 2003 09:04:51 -0000 1.8
@@ -18,160 +18,211 @@
$Id$
*/
-#include <unistd.h>
-#include <stdlib.h>
+#include <sys/types.h>
#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/mman.h>
#include <fcntl.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <libgen.h>
+#include <ctype.h>
#include <time.h>
+#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
-#include <string.h>
-#include <errno.h>
-#include <getopt.h>
+
+#define crc32 __complete_crap
+#include <zlib.h>
+#undef crc32
+#include "crc32.h"
#include <linux/mtd/mtd.h>
+#include "linux/jffs2.h"
#define PROGRAM "eraseall"
-#define VERSION "0.1.0"
+#define VERSION "0.1.1"
static const char *exe_name;
static const char *mtd_device;
-static int quiet; /* true -- don't output progress */
+static int quiet; /* true -- don't output progress */
+static int jffs2; // fornmat for jffs2 usage
-static void process_options( int argc, char *argv[] );
-static void display_help();
-static void display_version();
+static void process_options (int argc, char *argv[]);
+static void display_help (void);
+static void display_version (void);
+static struct jffs2_unknown_node cleanmarker;
-int main( int argc,char *argv[] )
+int main (int argc, char *argv[])
{
- mtd_info_t meminfo;
- int fd;
- erase_info_t erase;
-
- process_options( argc, argv );
-
- if( (fd = open( mtd_device, O_RDWR )) < 0 ) {
- fprintf( stderr, "%s: %s: %s\n", exe_name, mtd_device,
- strerror( errno ) );
- exit( 1 );
- }
-
-
- if( ioctl( fd, MEMGETINFO, &meminfo ) != 0 ) {
- fprintf( stderr, "%s: %s: unable to get MTD device info\n", exe_name,
- mtd_device );
- exit( 1 );
- }
-
- erase.length = meminfo.erasesize;
- for (erase.start = 0; erase.start < meminfo.size;
- erase.start += meminfo.erasesize) {
-
- if( !quiet ) {
- printf( "\rErasing %ld Kibyte @ %lx -- %2ld %% complete.",
- meminfo.erasesize/1024, erase.start,
- erase.start*100/meminfo.size );
- }
- fflush( stdout );
-
- if(ioctl( fd, MEMERASE, &erase) != 0)
- {
- fprintf( stderr, "\n%s: %s: MTD Erase failure: %s\n", exe_name,
- mtd_device, strerror( errno) );
- //exit( 1 );
- }
- }
- if( !quiet ) {
- printf( "\rErased %ld Kibyte @ %lx -- 100%% complete. \n",
- meminfo.size/1024, 0L );
- }
-
- return 0;
+ mtd_info_t meminfo;
+ int fd;
+ erase_info_t erase;
+ int isNAND;
+
+ process_options(argc, argv);
+
+
+ if ((fd = open(mtd_device, O_RDWR)) < 0) {
+ fprintf(stderr, "%s: %s: %s\n", exe_name, mtd_device, strerror(errno));
+ exit(1);
+ }
+
+
+ if (ioctl(fd, MEMGETINFO, &meminfo) != 0) {
+ fprintf(stderr, "%s: %s: unable to get MTD device info\n", exe_name, mtd_device);
+ exit(1);
+ }
+
+ if (jffs2) {
+ cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
+ cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
+ cleanmarker.totlen = cpu_to_je32 (sizeof (struct jffs2_unknown_node));
+ cleanmarker.hdr_crc = cpu_to_je32 (crc32 (0, &cleanmarker, sizeof (struct jffs2_unknown_node) - 4));
+ }
+
+ erase.length = meminfo.erasesize;
+ isNAND = meminfo.type == MTD_NANDFLASH ? 1 : 0;
+
+ for (erase.start = 0; erase.start < meminfo.size; erase.start += meminfo.erasesize) {
+
+ if (!quiet) {
+ printf
+ ("\rErasing %d Kibyte @ %x -- %2d %% complete.",
+ meminfo.erasesize / 1024, erase.start,
+ erase.start * 100 / meminfo.size);
+ }
+ fflush(stdout);
+
+ if (ioctl(fd, MEMERASE, &erase) != 0) {
+ fprintf(stderr, "\n%s: %s: MTD Erase failure: %s\n", exe_name, mtd_device, strerror(errno));
+ continue;
+ }
+
+ // format for JFFS2
+ if (!jffs2)
+ continue;
+
+ // write cleanmarker
+ if (isNAND) {
+ struct mtd_oob_buf oob;
+ oob.ptr = (unsigned char *) &cleanmarker;
+ oob.start = erase.start;
+ oob.start += meminfo.oobsize == 16 ? 8 : 6;
+ oob.length = meminfo.oobsize == 16 ? 8 : 2;
+ if (ioctl (fd, MEMWRITEOOB, &oob) != 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) {
+ fprintf(stderr, "\n%s: %s: MTD lseek failure: %s\n", exe_name, mtd_device, strerror(errno));
+ continue;
+ }
+ if (write (fd , &cleanmarker, sizeof (cleanmarker)) != 0) {
+ fprintf(stderr, "\n%s: %s: MTD write failure: %s\n", exe_name, mtd_device, strerror(errno));
+ continue;
+ }
+ }
+ if (!quiet)
+ printf ("\rCleanmarker written at %x\n", erase.start);
+ }
+
+ return 0;
}
-void process_options( int argc, char *argv[] )
+void process_options (int argc, char *argv[])
{
- int error=0;
+ int error = 0;
- exe_name=argv[0];
+ exe_name = argv[0];
- for(;;) {
- int option_index = 0;
- static const char* short_options="q";
- static const struct option long_options[] = {
- {"help", no_argument, 0, 0},
- {"version", no_argument, 0, 0},
- {"quiet", no_argument, 0, 'q'},
- {"silent", 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 0 :
- switch( option_index ) {
- case 0 :
- display_help();
- break;
- case 1 :
- display_version();
- break;
- }
- break;
- case 'q' :
- quiet=1;
- break;
- case '?' :
- error=1;
- break;
- }
- }
- if( optind==argc ) {
- fprintf( stderr, "%s: no MTD device specified\n", exe_name );
- error=1;
- }
- if( error ) {
- fprintf( stderr, "Try `%s --help' for more information.\n", exe_name );
- exit( 1 );
- }
+ for (;;) {
+ int option_index = 0;
+ static const char *short_options = "q";
+ static const struct option long_options[] = {
+ {"help", no_argument, 0, 0},
+ {"version", no_argument, 0, 0},
+ {"jffs2", no_argument, 0, 'j'},
+ {"quiet", no_argument, 0, 'q'},
+ {"silent", 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 0:
+ switch (option_index) {
+ case 0:
+ display_help();
+ break;
+ case 1:
+ display_version();
+ break;
+ }
+ break;
+ case 'q':
+ quiet = 1;
+ break;
+ case 'j':
+ jffs2 = 1;
+ break;
+ case '?':
+ error = 1;
+ break;
+ }
+ }
+ if (optind == argc) {
+ fprintf(stderr, "%s: no MTD device specified\n", exe_name);
+ error = 1;
+ }
+ if (error) {
+ fprintf(stderr, "Try `%s --help' for more information.\n",
+ exe_name);
+ exit(1);
+ }
- mtd_device=argv[optind];
+ mtd_device = argv[optind];
}
-void display_help()
+void display_help (void)
{
- printf( "Usage: %s [OPTION] MTD_DEVICE\n"
- "Erases all of the specified MTD device.\n"
- "\n"
- " -q, --quiet don't display progress messages\n"
- " --silent same as --quiet\n"
- " --help display this help and exit\n"
- " --version output version information and exit\n"
- , exe_name);
- exit( 0 );
+ printf("Usage: %s [OPTION] MTD_DEVICE\n"
+ "Erases all of the specified MTD device.\n"
+ "\n"
+ " -j, --jffs2 format the device for jffs2"
+ " -q, --quiet don't display progress messages\n"
+ " --silent same as --quiet\n"
+ " --help display this help and exit\n"
+ " --version output version information and exit\n",
+ exe_name);
+ exit(0);
}
-
-void display_version()
+
+void display_version (void)
{
- printf( PROGRAM " " VERSION "\n"
- "\n"
- "Copyright (C) 2000 Arcom Control Systems Ltd\n"
- "\n"
- PROGRAM " comes with NO WARRANTY\n"
- "to the extent permitted by law.\n"
- "\n"
- "You may redistribute copies of " PROGRAM "\n"
- "under the terms of the GNU General Public Licence.\n"
- "See the file `COPYING' for more information.\n"
- );
- exit( 0 );
+ printf(PROGRAM " " VERSION "\n"
+ "\n"
+ "Copyright (C) 2000 Arcom Control Systems Ltd\n"
+ "\n"
+ PROGRAM " comes with NO WARRANTY\n"
+ "to the extent permitted by law.\n"
+ "\n"
+ "You may redistribute copies of " PROGRAM "\n"
+ "under the terms of the GNU General Public Licence.\n"
+ "See the file `COPYING' for more information.\n");
+ exit(0);
}
More information about the linux-mtd-cvs
mailing list