mtd/util nandwrite.c,1.11,1.12

dbrown at infradead.org dbrown at infradead.org
Thu Jun 24 15:10:14 EDT 2004


Update of /home/cvs/mtd/util
In directory phoenix.infradead.org:/tmp/cvs-serv15251

Modified Files:
	nandwrite.c 
Log Message:
Add -p option to accept non-aligned images and pad them to page size.


Index: nandwrite.c
===================================================================
RCS file: /home/cvs/mtd/util/nandwrite.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- nandwrite.c	6 May 2004 06:47:13 -0000	1.11
+++ nandwrite.c	24 Jun 2004 19:10:11 -0000	1.12
@@ -77,6 +77,7 @@
 	       "  -n, --noecc		write without ecc\n"
 	       "  -o, --oob    	 	image copntains oob data\n"
 	       "  -s addr, --start=addr set start address (default is 0)\n"
+	       "  -p, --pad             pad to page size\n"
 	       "  -q, --quiet    	don't display progress messages\n"
 	       "      --help     	display this help and exit\n"
 	       "      --version  	output version information and exit\n");
@@ -106,6 +107,7 @@
 int	forcejffs2 = 0;
 int	forceyaffs = 0;
 int	noecc = 0;
+int	pad = 0;
 
 void process_options (int argc, char *argv[])
 {
@@ -113,7 +115,7 @@
 
 	for (;;) {
 		int option_index = 0;
-		static const char *short_options = "os:ajynq";
+		static const char *short_options = "os:ajynqp";
 		static const struct option long_options[] = {
 			{"help", no_argument, 0, 0},
 			{"version", no_argument, 0, 0},
@@ -124,6 +126,7 @@
 			{"yaffs", no_argument, 0, 'y'},
 			{"noecc", no_argument, 0, 'n'},
 			{"quiet", no_argument, 0, 'q'},
+			{"pad", no_argument, 0, 'p'},
 			{0, 0, 0, 0},
 		};
 
@@ -162,6 +165,9 @@
 		case 'o':
 			writeoob = 1;
 			break;
+		case 'p':
+			pad = 1;
+			break;
 		case 's':
 			mtdoffset = atoi (optarg);
 			break;
@@ -186,9 +192,16 @@
 	int cnt, fd, ifd, imglen, pagelen, blockstart = -1;
 	struct mtd_info_user meminfo;
 	struct mtd_oob_buf oob;
+	loff_t offs;
+	int ret, readlen;
 
 	process_options(argc, argv);
 
+	if (pad && writeoob) {
+		fprintf(stderr, "Can't pad when oob data is present.\n");
+		exit(1);
+	}
+
 	/* Open the device */
 	if ((fd = open(mtd_device, O_RDWR)) == -1) {
 		perror("open flash");
@@ -268,7 +281,7 @@
 	pagelen = meminfo.oobblock + ((writeoob == 1) ? meminfo.oobsize : 0);
 	
 	// Check, if file is pagealigned
-	if ( (imglen % pagelen) != 0) {
+	if ((!pad) && ((imglen % pagelen) != 0)) {
 		perror ("Input file is not page aligned");
 		close (fd);
 		close (ifd);
@@ -284,29 +297,36 @@
 	}
 	
 	/* Get data from input and write to the device */
-	while(mtdoffset < meminfo.size) {
+	while (imglen && (mtdoffset < meminfo.size)) {
 		// new eraseblock , check for bad block
 		if (blockstart != (mtdoffset & (~meminfo.erasesize + 1))) {
 			blockstart = mtdoffset & (~meminfo.erasesize + 1);
-			oob.start = blockstart;
+			offs = blockstart;
 			if (!quiet)
 				fprintf (stdout, "Writing data to block %x\n", blockstart);
-			if (ioctl(fd, MEMREADOOB, &oob) != 0) {
-				perror("ioctl(MEMREADOOB)");
+			if ((ret = ioctl(fd, MEMGETBADBLOCK, &offs)) < 0) {
+				perror("ioctl(MEMGETBADBLOCK)");
 				close (ifd);
 				close (fd);
 				exit (1);
 			}
-			if (oobbuf[5] != 0xff) {
+			if (ret == 1) {
 				if (!quiet)
 					fprintf (stderr, "Bad block at %x, will be skipped\n", blockstart);
 				mtdoffset = blockstart + meminfo.erasesize;
 				continue;					
 			}
 		}
-	
+
+		readlen = meminfo.oobblock;
+		if (pad && (imglen < readlen))
+		{
+			readlen = imglen;
+			memset(writebuf + readlen, 0xff, meminfo.oobblock - readlen);
+		}
+
 		/* Read Page Data from input file */
-		if ((cnt = read(ifd, writebuf, meminfo.oobblock)) != meminfo.oobblock) {
+		if ((cnt = read(ifd, writebuf, readlen)) != readlen) {
 			if (cnt == 0)	// EOF
 				break;
 			perror ("File I/O error on input file");
@@ -341,7 +361,7 @@
 			close (ifd);
 			exit (1);
 		}
-		imglen -= meminfo.oobblock;
+		imglen -= readlen;
 		mtdoffset += meminfo.oobblock;
 	}
 





More information about the linux-mtd-cvs mailing list