All pastes #270040 Raw Edit

Stuff

public text v1 · immutable
·published 2006-12-07 00:49 UTC
rendered paste body
--- a/nandwrite.c
+++ b/nandwrite.c
@@ -103,6 +103,7 @@ void display_help(void)
 			"  -p, --pad             pad to page size\n"
 			"  -b, --blockalign=1|2|4 set multiple of eraseblocks to align to\n"
 			"  -q, --quiet		don't display progress messages\n"
+			"  -i, --stdin		read from standard input\n"
 			"      --help		display this help and exit\n"
 			"      --version		output version information and exit\n");
 	exit(0);
@@ -134,6 +135,7 @@ int		forceyaffs = 0;
 int		forcelegacy = 0;
 int		noecc = 0;
 int		pad = 0;
+int		fromstdin = 0;
 int		blockalign = 1; /*default to using 16K block size */
 
 void process_options(int argc, char *argv[])
@@ -156,6 +158,7 @@ void process_options(int argc, char *argv[])
 			{"quiet", no_argument, 0, 'q'},
 			{"start", required_argument, 0, 's'},
 			{"yaffs", no_argument, 0, 'y'},
+			{"stdin", no_argument, 0, 'i'},
 			{0, 0, 0, 0},
 		};
 
@@ -206,13 +209,16 @@ void process_options(int argc, char *argv[])
 			case 'b':
 				blockalign = atoi (optarg);
 				break;
+			case 'i':
+				fromstdin = 1;
+				break;
 			case '?':
 				error = 1;
 				break;
 		}
 	}
 
-	if ((argc - optind) != 2 || error)
+	if ((argc - optind + fromstdin) != 2 || error)
 		display_help ();
 
 	mtd_device = argv[optind++];
@@ -223,7 +229,7 @@ void process_options(int argc, char *argv[])
 int main(int argc, char **argv)
 {
 	int		cnt, fd, ifd, pagelen, baderaseblock, blockstart = -1;
-	long	imglen = 0;
+	unsigned long	imglen = 0;
 	struct	mtd_info_user meminfo;
 	struct	mtd_oob_buf oob;
 	loff_t	offs;
@@ -401,10 +407,13 @@ int main(int argc, char **argv)
 		if(!quiet)
 			printf("Server said payload size is: %ld\n", imglen);
 
-		
+	}else if (fromstdin)
+	{
+		fd = STDIN_FILENO;
+		imglen = -1ul;
 	}else
 	{
-								
+
 		if ((ifd = open(img, O_RDONLY)) == -1) {
 			perror("open input file");
 			goto restoreoob;
@@ -418,18 +427,21 @@ int main(int argc, char **argv)
 
 	pagelen = meminfo.writesize + ((writeoob == 1) ? meminfo.oobsize : 0);
 
-	/* Check, if file is pagealigned */
-	if ((!pad) && ((imglen % pagelen) != 0)) {
-		fprintf (stderr, "Input file is not page aligned\n");
-		goto closeall;
-	}
+	if (!fromstdin)
+	{
+		/* Check, if file is pagealigned */
+		if ((!pad) && ((imglen % pagelen) != 0)) {
+			fprintf (stderr, "Input file is not page aligned\n");
+			goto closeall;
+		}
 
-	/* Check, if length fits into device */
-	if ( ((imglen / pagelen) * meminfo.writesize) > (meminfo.size - mtdoffset)) {
-		fprintf (stderr, "Image %ld bytes, NAND page %d bytes, OOB area %u bytes, device size %u bytes\n",
-				imglen, pagelen, meminfo.writesize, meminfo.size);
-		perror ("Input file does not fit into device");
-		goto closeall;
+		/* Check, if length fits into device */
+		if ( ((imglen / pagelen) * meminfo.writesize) > (meminfo.size - mtdoffset)) {
+			fprintf (stderr, "Image %ld bytes, NAND page %d bytes, OOB area %u bytes, device size %u bytes\n",
+					imglen, pagelen, meminfo.writesize, meminfo.size);
+			perror ("Input file does not fit into device");
+			goto closeall;
+		}
 	}
 
 	/* Get data from input and write to the device */
@@ -488,8 +500,10 @@ int main(int argc, char **argv)
 		if(!sockfd)
 		{
 			if ((cnt = read(ifd, writebuf, readlen)) != readlen) {
-				if (cnt == 0)
+				if (cnt == 0) {
+					imglen = 0;
 					break;
+				}
 				perror ("File I/O error on input file");
 				break;
 			}