mtd - add partition offset parameter
[openwrt/svn-archive/archive.git] / package / mtd / src / mtd.c
index 7f5816539b2c14c7e9fd6d2c566aca193ba7ad9a..92873ca19b775eb84bb714d1cfc0e643b7ef78a4 100644 (file)
 #include <sys/stat.h>
 #include <sys/reboot.h>
 #include <linux/reboot.h>
-#include "mtd-api.h"
+#include <mtd/mtd-user.h>
 #include "fis.h"
 #include "mtd.h"
 
+#ifndef MTDREFRESH
+#define MTDREFRESH     _IO('M', 50)
+#endif
+
 #define MAX_ARGS 8
 #define JFFS2_DEFAULT_DIR      "" /* directory name without /, empty means root dir */
 
@@ -282,7 +286,7 @@ indicate_writing(const char *mtd)
 }
 
 static int
-mtd_write(int imagefd, const char *mtd, char *fis_layout)
+mtd_write(int imagefd, const char *mtd, char *fis_layout, size_t part_offset)
 {
        char *next = NULL;
        char *str = NULL;
@@ -366,6 +370,11 @@ resume:
                exit(1);
        }
 
+       if (part_offset > 0) {
+               fprintf(stderr, "Seeking on mtd device '%s' to: %u\n", mtd, part_offset);
+               lseek(fd, part_offset, SEEK_SET);
+       }
+
        indicate_writing(mtd);
 
        w = e = 0;
@@ -409,6 +418,9 @@ resume:
                                /* got an EOF marker - this is the place to add some jffs2 data */
                                skip = mtd_replace_jffs2(mtd, fd, e, jffs2file);
 
+                               /* don't add it again */
+                               jffs2file = NULL;
+
                                w += skip;
                                e += skip;
                                skip -= buflen;
@@ -502,7 +514,7 @@ static void usage(void)
            fprintf(stderr,
        "        fixtrx                  fix the checksum in a trx header on first boot\n");
        }
-    fprintf(stderr,    
+    fprintf(stderr,
        "Following options are available:\n"
        "        -q                      quiet mode (once: no [w] on writing,\n"
        "                                           twice: no status messages)\n"
@@ -511,7 +523,8 @@ static void usage(void)
        "        -f                      force write without trx checks\n"
        "        -e <device>             erase <device> before executing the command\n"
        "        -d <name>               directory for jffs2write, defaults to \"tmp\"\n"
-       "        -j <name>               integrate <file> into jffs2 data when writing an image\n");
+       "        -j <name>               integrate <file> into jffs2 data when writing an image\n"
+       "        -p                      write beginning at partition offset\n");
        if (mtd_fixtrx) {
            fprintf(stderr,
        "        -o offset               offset of the image header in the partition(for fixtrx)\n");
@@ -547,7 +560,7 @@ int main (int argc, char **argv)
        int ch, i, boot, imagefd = 0, force, unlocked;
        char *erase[MAX_ARGS], *device = NULL;
        char *fis_layout = NULL;
-       size_t offset = 0;
+       size_t offset = 0, part_offset = 0;
        enum {
                CMD_ERASE,
                CMD_WRITE,
@@ -568,7 +581,7 @@ int main (int argc, char **argv)
 #ifdef FIS_SUPPORT
                        "F:"
 #endif
-                       "frnqe:d:j:o:")) != -1)
+                       "frnqe:d:j:p:o:")) != -1)
                switch (ch) {
                        case 'f':
                                force = 1;
@@ -596,6 +609,14 @@ int main (int argc, char **argv)
                        case 'd':
                                jffs2dir = optarg;
                                break;
+                       case 'p':
+                               errno = 0;
+                               part_offset = strtoul(optarg, 0, 0);
+                               if (errno) {
+                                       fprintf(stderr, "-p: illegal numeric string\n");
+                                       usage();
+                               }
+                               break;
                        case 'o':
                                if (!mtd_fixtrx) {
                                        fprintf(stderr, "-o: is not available on this platform\n");
@@ -697,7 +718,7 @@ int main (int argc, char **argv)
                case CMD_WRITE:
                        if (!unlocked)
                                mtd_unlock(device);
-                       mtd_write(imagefd, device, fis_layout);
+                       mtd_write(imagefd, device, fis_layout, part_offset);
                        break;
                case CMD_JFFS2WRITE:
                        if (!unlocked)