add support for appending a file to jffs2 during reflash on the fly
authorFelix Fietkau <nbd@openwrt.org>
Fri, 8 Aug 2008 22:43:19 +0000 (22:43 +0000)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 8 Aug 2008 22:43:19 +0000 (22:43 +0000)
SVN-Revision: 12250

package/mtd/Makefile
package/mtd/src/jffs2.c
package/mtd/src/mtd.c
package/mtd/src/mtd.h

index 2bdc8fabcb9b50a383af21059ee7db6b45a6c917..11a78e8454e94164866b52467de1f0c840236693 100644 (file)
@@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk
 include $(INCLUDE_DIR)/kernel.mk
 
 PKG_NAME:=mtd
 include $(INCLUDE_DIR)/kernel.mk
 
 PKG_NAME:=mtd
-PKG_RELEASE:=6
+PKG_RELEASE:=7
 
 PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
 
 
 PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
 
index f614a336ea064daa428e7cade371a11ac66356af..3b47d4158f67994f9960bf049fd19fb09070e220 100644 (file)
@@ -14,7 +14,6 @@
 #define PAD(x) (((x)+3)&~3)
 
 #define CLEANMARKER "\x85\x19\x03\x20\x0c\x00\x00\x00\xb1\xb0\x1e\xe4"
 #define PAD(x) (((x)+3)&~3)
 
 #define CLEANMARKER "\x85\x19\x03\x20\x0c\x00\x00\x00\xb1\xb0\x1e\xe4"
-#define JFFS2_EOF "\xde\xad\xc0\xde"
 
 static int last_ino = 0;
 static int last_version = 0;
 
 static int last_ino = 0;
 static int last_version = 0;
@@ -22,6 +21,7 @@ static char *buf = NULL;
 static int ofs = 0;
 static int outfd = 0;
 static int mtdofs = 0;
 static int ofs = 0;
 static int outfd = 0;
 static int mtdofs = 0;
+static int target_ino = 0;
 
 static void prep_eraseblock(void);
 
 
 static void prep_eraseblock(void);
 
@@ -65,7 +65,7 @@ static void prep_eraseblock(void)
        add_data(CLEANMARKER, sizeof(CLEANMARKER) - 1);
 }
 
        add_data(CLEANMARKER, sizeof(CLEANMARKER) - 1);
 }
 
-static int add_dirent(char *name, char type, int parent)
+static int add_dirent(const char *name, const char type, int parent)
 {
        struct jffs2_raw_dirent *de;
 
 {
        struct jffs2_raw_dirent *de;
 
@@ -97,7 +97,7 @@ static int add_dirent(char *name, char type, int parent)
        return de->ino;
 }
 
        return de->ino;
 }
 
-static int add_dir(char *name, int parent)
+static int add_dir(const char *name, int parent)
 {
        struct jffs2_raw_inode ri;
        int inode;
 {
        struct jffs2_raw_inode ri;
        int inode;
@@ -128,12 +128,13 @@ static int add_dir(char *name, int parent)
        return inode;
 }
 
        return inode;
 }
 
-static void add_file(char *name, int parent)
+static void add_file(const char *name, int parent)
 {
        int inode, f_offset = 0, fd;
        struct jffs2_raw_inode ri;
        struct stat st;
 {
        int inode, f_offset = 0, fd;
        struct jffs2_raw_inode ri;
        struct stat st;
-       char wbuf[4096], *fname;
+       char wbuf[4096];
+       const char *fname;
        FILE *f;
 
        if (stat(name, &st)) {
        FILE *f;
 
        if (stat(name, &st)) {
@@ -204,9 +205,53 @@ static void add_file(char *name, int parent)
        close(fd);
 }
 
        close(fd);
 }
 
-int mtd_write_jffs2(char *mtd, char *filename, char *dir)
+int mtd_replace_jffs2(int fd, int ofs, const char *filename)
+{
+       outfd = fd;
+       mtdofs = ofs;
+
+       buf = malloc(erasesize);
+       target_ino = 1;
+       if (!last_ino)
+               last_ino = 1;
+       add_file(filename, target_ino);
+       pad(erasesize);
+
+       /* add eof marker, pad to eraseblock size and write the data */
+       add_data(JFFS2_EOF, sizeof(JFFS2_EOF) - 1);
+       pad(erasesize);
+       free(buf);
+}
+
+void mtd_parse_jffs2data(const char *buf, const char *dir)
+{
+       struct jffs2_unknown_node *node = (struct jffs2_unknown_node *) buf;
+       unsigned int ofs = 0;
+
+       while (ofs < erasesize) {
+               node = (struct jffs2_unknown_node *) (buf + ofs);
+               if (node->magic != 0x1985)
+                       break;
+
+               ofs += PAD(node->totlen);
+               if (node->nodetype == JFFS2_NODETYPE_DIRENT) {
+                       struct jffs2_raw_dirent *de = (struct jffs2_raw_dirent *) node;
+
+                       /* is this the right directory name and is it a subdirectory of / */
+                       if (*dir && (de->pino == 1) && !strncmp(de->name, dir, de->nsize))
+                               target_ino = de->ino;
+
+                       /* store the last inode and version numbers for adding extra files */
+                       if (last_ino < de->ino)
+                               last_ino = de->ino;
+                       if (last_version < de->version)
+                               last_version = de->version;
+               }
+       }
+}
+
+int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir)
 {
 {
-       int target_ino = 0;
        int err = -1, fdeof = 0;
        off_t offset;
 
        int err = -1, fdeof = 0;
        off_t offset;
 
@@ -230,7 +275,6 @@ int mtd_write_jffs2(char *mtd, char *filename, char *dir)
         * locate the directory that the file is going to be placed in */
        for(;;) {
                struct jffs2_unknown_node *node = (struct jffs2_unknown_node *) buf;
         * locate the directory that the file is going to be placed in */
        for(;;) {
                struct jffs2_unknown_node *node = (struct jffs2_unknown_node *) buf;
-               unsigned int ofs = 0;
 
                if (read(outfd, buf, erasesize) != erasesize) {
                        fdeof = 1;
 
                if (read(outfd, buf, erasesize) != erasesize) {
                        fdeof = 1;
@@ -248,27 +292,7 @@ int mtd_write_jffs2(char *mtd, char *filename, char *dir)
                if (node->magic != 0x1985)
                        break;
 
                if (node->magic != 0x1985)
                        break;
 
-               while (ofs < erasesize) {
-                       node = (struct jffs2_unknown_node *) (buf + ofs);
-                       if (node->magic == 0x1985) {
-                               ofs += PAD(node->totlen);
-                               if (node->nodetype == JFFS2_NODETYPE_DIRENT) {
-                                       struct jffs2_raw_dirent *de = (struct jffs2_raw_dirent *) node;
-                                       
-                                       /* is this the right directory name and is it a subdirectory of / */
-                                       if (*dir && (de->pino == 1) && !strncmp(de->name, dir, de->nsize))
-                                               target_ino = de->ino;
-
-                                       /* store the last inode and version numbers for adding extra files */
-                                       if (last_ino < de->ino)
-                                               last_ino = de->ino;
-                                       if (last_version < de->version)
-                                               last_version = de->version;
-                               }
-                       } else {
-                               ofs = ~0;
-                       }
-               }
+               mtd_parse_jffs2data(buf, dir);
        }
 
        if (fdeof) {
        }
 
        if (fdeof) {
index f3a13efd24c167b282be14513a4b793cea14a5c7..027c859df3192735cd68db5e22fd6c7fe2c33a07 100644 (file)
 #include <sys/stat.h>
 #include <sys/reboot.h>
 #include <linux/reboot.h>
 #include <sys/stat.h>
 #include <sys/reboot.h>
 #include <linux/reboot.h>
-
 #include "mtd-api.h"
 #include "mtd-api.h"
+#include "mtd.h"
 
 #define TRX_MAGIC       0x30524448      /* "HDR0" */
 
 #define TRX_MAGIC       0x30524448      /* "HDR0" */
-#define BUFSIZE (16 * 1024)
 #define MAX_ARGS 8
 
 #define DEBUG
 #define MAX_ARGS 8
 
 #define DEBUG
@@ -66,9 +65,10 @@ struct trx_header {
        uint32_t offsets[3];    /* Offsets of partitions from start of header */
 };
 
        uint32_t offsets[3];    /* Offsets of partitions from start of header */
 };
 
-static char buf[BUFSIZE];
-static char *imagefile;
-static int buflen;
+static char *buf = NULL;
+static char *imagefile = NULL;
+static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
+static int buflen = 0;
 int quiet;
 int mtdsize = 0;
 int erasesize = 0;
 int quiet;
 int mtdsize = 0;
 int erasesize = 0;
@@ -134,7 +134,7 @@ int mtd_erase_block(int fd, int offset)
        }
 }
 
        }
 }
 
-int mtd_write_buffer(int fd, char *buf, int offset, int length)
+int mtd_write_buffer(int fd, const char *buf, int offset, int length)
 {
        lseek(fd, offset, SEEK_SET);
        write(fd, buf, length);
 {
        lseek(fd, offset, SEEK_SET);
        write(fd, buf, length);
@@ -205,6 +205,9 @@ static int mtd_check(const char *mtd)
        if (!fd)
                return 0;
 
        if (!fd)
                return 0;
 
+       if (!buf)
+               buf = malloc(erasesize);
+
        close(fd);
        return 1;
 }
        close(fd);
        return 1;
 }
@@ -316,15 +319,29 @@ mtd_write(int imagefd, const char *mtd)
 
        for (;;) {
                /* buffer may contain data already (from trx check) */
 
        for (;;) {
                /* buffer may contain data already (from trx check) */
-               r = buflen;
-               r += read(imagefd, buf + buflen, BUFSIZE - buflen);
-               w += r;
+               r = read(imagefd, buf + buflen, erasesize - buflen);
+               if (r < 0)
+                       break;
 
 
-               /* EOF */
-               if (r <= 0) break;
+               buflen += r;
+
+               if (jffs2file) {
+                       if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF)) == 0) {
+                               if (!quiet)
+                                       fprintf(stderr, "\b\b\b   ");
+                               if (quiet < 2)
+                                       fprintf(stderr, "\nAppending jffs2 data to from %s to %s...", jffs2file, mtd);
+                               /* got an EOF marker - this is the place to add some jffs2 data */
+                               mtd_replace_jffs2(fd, e, jffs2file);
+                               goto done;
+                       }
+                       /* no EOF marker, make sure we figure out the last inode number
+                        * before appending some data */
+                       mtd_parse_jffs2data(buf, jffs2dir);
+               }
 
                /* need to erase the next block before writing data to it */
 
                /* need to erase the next block before writing data to it */
-               while (w > e) {
+               while (w + buflen > e) {
                        if (!quiet)
                                fprintf(stderr, "\b\b\b[e]");
 
                        if (!quiet)
                                fprintf(stderr, "\b\b\b[e]");
 
@@ -337,7 +354,7 @@ mtd_write(int imagefd, const char *mtd)
                if (!quiet)
                        fprintf(stderr, "\b\b\b[w]");
                
                if (!quiet)
                        fprintf(stderr, "\b\b\b[w]");
                
-               if ((result = write(fd, buf, r)) < r) {
+               if ((result = write(fd, buf, buflen)) < buflen) {
                        if (result < 0) {
                                fprintf(stderr, "Error writing image.\n");
                                exit(1);
                        if (result < 0) {
                                fprintf(stderr, "Error writing image.\n");
                                exit(1);
@@ -346,12 +363,18 @@ mtd_write(int imagefd, const char *mtd)
                                exit(1);
                        }
                }
                                exit(1);
                        }
                }
-               
+               w += buflen;
+
+               /* not enough data - eof */
+               if (buflen < erasesize)
+                       break;
+
                buflen = 0;
        }
        if (!quiet)
                fprintf(stderr, "\b\b\b\b");
 
                buflen = 0;
        }
        if (!quiet)
                fprintf(stderr, "\b\b\b\b");
 
+done:
        if (quiet < 2)
                fprintf(stderr, "\n");
 
        if (quiet < 2)
                fprintf(stderr, "\n");
 
@@ -376,6 +399,7 @@ 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"
        "        -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"
        "\n"
        "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
        "         mtd -r write linux.trx linux\n\n");
        "\n"
        "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
        "         mtd -r write linux.trx linux\n\n");
@@ -399,7 +423,6 @@ int main (int argc, char **argv)
 {
        int ch, i, boot, unlock, imagefd, force, unlocked;
        char *erase[MAX_ARGS], *device;
 {
        int ch, i, boot, unlock, imagefd, force, unlocked;
        char *erase[MAX_ARGS], *device;
-       char *jffs2dir = JFFS2_DEFAULT_DIR;
        enum {
                CMD_ERASE,
                CMD_WRITE,
        enum {
                CMD_ERASE,
                CMD_WRITE,
@@ -414,7 +437,7 @@ int main (int argc, char **argv)
        buflen = 0;
        quiet = 0;
 
        buflen = 0;
        quiet = 0;
 
-       while ((ch = getopt(argc, argv, "frqe:d:")) != -1)
+       while ((ch = getopt(argc, argv, "frqe:d:j:")) != -1)
                switch (ch) {
                        case 'f':
                                force = 1;
                switch (ch) {
                        case 'f':
                                force = 1;
@@ -422,6 +445,9 @@ int main (int argc, char **argv)
                        case 'r':
                                boot = 1;
                                break;
                        case 'r':
                                boot = 1;
                                break;
+                       case 'j':
+                               jffs2file = optarg;
+                               break;
                        case 'q':
                                quiet++;
                                break;
                        case 'q':
                                quiet++;
                                break;
@@ -470,17 +496,14 @@ int main (int argc, char **argv)
                        }
                }
        
                        }
                }
        
+               if (!mtd_check(device)) {
+                       fprintf(stderr, "Can't open device for writing!\n");
+                       exit(1);
+               }
                /* check trx file before erasing or writing anything */
                /* check trx file before erasing or writing anything */
-               if (!image_check(imagefd, device)) {
-                       if (!force) {
-                               fprintf(stderr, "Image check failed.\n");
-                               exit(1);
-                       }
-               } else {
-                       if (!mtd_check(device)) {
-                               fprintf(stderr, "Can't open device for writing!\n");
-                               exit(1);
-                       }
+               if (!image_check(imagefd, device) && !force) {
+                       fprintf(stderr, "Image check failed.\n");
+                       exit(1);
                }
        } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
                cmd = CMD_JFFS2WRITE;
                }
        } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
                cmd = CMD_JFFS2WRITE;
index 25dcef05c929e77032ea0a8ea13b6161f3818557..42b6610eb93f9fae9811996e6839dcc4229f13cf 100644 (file)
@@ -1,14 +1,18 @@
 #ifndef __mtd_h
 #define __mtd_h
 
 #ifndef __mtd_h
 #define __mtd_h
 
+#define JFFS2_EOF "\xde\xad\xc0\xde"
+
 extern int quiet;
 extern int mtdsize;
 extern int erasesize;
 
 extern int mtd_open(const char *mtd);
 extern int quiet;
 extern int mtdsize;
 extern int erasesize;
 
 extern int mtd_open(const char *mtd);
-extern int mtd_check_open(char *mtd);
+extern int mtd_check_open(const char *mtd);
 extern int mtd_erase_block(int fd, int offset);
 extern int mtd_erase_block(int fd, int offset);
-extern int mtd_write_buffer(int fd, char *buf, int offset, int length);
-extern int mtd_write_jffs2(char *mtd, char *filename, char *dir);
+extern int mtd_write_buffer(int fd, const char *buf, int offset, int length);
+extern int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir);
+extern int mtd_replace_jffs2(int fd, int ofs, const char *filename);
+extern void mtd_parse_jffs2data(const char *buf, const char *dir);
 
 #endif /* __mtd_h */
 
 #endif /* __mtd_h */