Author: titmuss
Date: Mon May 19 06:45:07 2008
New Revision: 2478

URL: http://svn.slimdevices.com?rev=2478&root=Jive&view=rev
Log:
Bug: N/A
Description:
Apply r1949 to updated mtd-utils:
  Allow nandwrite to accept data from stdin (thanks Felix!).


Modified:
    7.1/trunk/squeezeos/src/system/mtd-utils/nandwrite.c

Modified: 7.1/trunk/squeezeos/src/system/mtd-utils/nandwrite.c
URL: 
http://svn.slimdevices.com/7.1/trunk/squeezeos/src/system/mtd-utils/nandwrite.c?rev=2478&root=Jive&r1=2477&r2=2478&view=diff
==============================================================================
--- 7.1/trunk/squeezeos/src/system/mtd-utils/nandwrite.c (original)
+++ 7.1/trunk/squeezeos/src/system/mtd-utils/nandwrite.c Mon May 19 06:45:07 
2008
@@ -72,7 +72,7 @@
 
 void display_help (void)
 {
-       printf("Usage: nandwrite [OPTION] MTD_DEVICE INPUTFILE\n"
+       printf("Usage: nandwrite [OPTION] [MTD_DEVICE INPUTFILE|-]\n"
                        "Writes to the specified MTD device.\n"
                        "\n"
                        "  -a, --autoplace      Use auto oob layout\n"
@@ -218,6 +218,7 @@
        int ret, readlen;
        int oobinfochanged = 0;
        struct nand_oobinfo old_oobinfo;
+       int read_from_stdin = 0;
 
        process_options(argc, argv);
 
@@ -334,15 +335,29 @@
        oob.length = meminfo.oobsize;
        oob.ptr = noecc ? oobreadbuf : oobbuf;
 
-       /* Open the input file */
-       if ((ifd = open(img, O_RDONLY)) == -1) {
-               perror("open input file");
-               goto restoreoob;
-       }
-
-       // get image length
-       imglen = lseek(ifd, 0, SEEK_END);
-       lseek (ifd, 0, SEEK_SET);
+       // Check if we read from stdin
+       if (strcmp(img, "-") == 0) {
+               // FIXME OOB data and stdin
+               if( writeoob) {
+                       fprintf(stderr, "Can't use oob data when stdin is 
used.\n");
+                       exit(-1);
+               }
+
+               ifd = STDIN_FILENO;
+               read_from_stdin = 1;
+               imglen = pagelen;       // used as flag in stdin case
+       }
+       else {
+               /* Open the input file */
+               if ((ifd = open(img, O_RDONLY)) == -1) {
+                       perror("open input file");
+                       goto restoreoob;
+               }
+
+               // get image length
+               imglen = lseek(ifd, 0, SEEK_END);
+               lseek (ifd, 0, SEEK_SET);
+       }
 
        pagelen = meminfo.writesize + ((writeoob == 1) ? meminfo.oobsize : 0);
 
@@ -361,6 +376,7 @@
        }
 
        /* Get data from input and write to the device */
+       /* imglen is used as flag in stdin case */
        while (imglen && (mtdoffset < meminfo.size)) {
                // new eraseblock , check for bad block(s)
                // Stay in the loop to be sure if the mtdoffset changes because
@@ -398,18 +414,51 @@
                }
 
                readlen = meminfo.writesize;
-               if (pad && (imglen < readlen))
-               {
-                       readlen = imglen;
-                       memset(writebuf + readlen, 0xff, meminfo.writesize - 
readlen);
-               }
-
-               /* Read Page Data from input file */
-               if ((cnt = read(ifd, writebuf, readlen)) != readlen) {
-                       if (cnt == 0)   // EOF
-                               break;
-                       perror ("File I/O error on input file");
-                       goto closeall;
+
+               if( !read_from_stdin) { 
+                       if (pad && (imglen < readlen))
+                       {
+                               readlen = imglen;
+                               memset(writebuf + readlen, 0xff, 
meminfo.writesize - readlen);
+                       }
+
+                       /* Read Page Data from input file */
+                       if ((cnt = read(ifd, writebuf, readlen)) != readlen) {
+                               if (cnt == 0)   // EOF
+                                       break;
+                               perror ("File I/O error on input file");
+                               goto closeall;
+                       }
+               } else {
+                       /* Read Page Data from stdin */
+                       int tinycnt = 0;
+                       while(tinycnt < readlen) {
+                               cnt = read(ifd, writebuf + tinycnt, readlen - 
tinycnt);
+                               if (cnt == 0) { // EOF
+                                       break;
+                               }
+                               else if (cnt < 0) {
+                                       perror ("File I/O error on stdin");
+                                       goto closeall;
+                               }
+                               tinycnt += cnt;
+                       }
+
+                       /* No padding needed - we are done */
+                       if (tinycnt == 0) {
+                               imglen = 0;
+                               break;
+                       }
+
+                       /* No more bytes - we are done after writing the 
remaining bytes */
+                       if (cnt == 0) {
+                               imglen = 0;
+                       }
+
+                       /* Padding */
+                       if (pad && (tinycnt < readlen)) {
+                               memset( writebuf + tinycnt, 0xff, 
meminfo.writesize - tinycnt);
+                       }
                }
 
                if (writeoob) {
@@ -493,7 +542,10 @@
 
                        continue;
                }
-               imglen -= readlen;
+
+               if (!read_from_stdin) { 
+                       imglen -= readlen;
+               }
                mtdoffset += meminfo.writesize;
        }
 

_______________________________________________
Jive-checkins mailing list
[email protected]
http://lists.slimdevices.com/cgi-bin/mailman/listinfo/jive-checkins

Reply via email to