aboutsummaryrefslogtreecommitdiffstats
path: root/package/mtd/src
diff options
context:
space:
mode:
authorFelix Fietkau <nbd@openwrt.org>2008-08-08 22:43:19 +0000
committerFelix Fietkau <nbd@openwrt.org>2008-08-08 22:43:19 +0000
commit700e5ed980b2e48e724a486e9052fa636e9501ad (patch)
tree84fc3c9085e5e86d36a328a6b2f1d2f86c605d2c /package/mtd/src
parenta2e33f9f8a4caa1ff3fb6d43b275d84c6d6747c3 (diff)
downloadupstream-700e5ed980b2e48e724a486e9052fa636e9501ad.tar.gz
upstream-700e5ed980b2e48e724a486e9052fa636e9501ad.tar.bz2
upstream-700e5ed980b2e48e724a486e9052fa636e9501ad.zip
add support for appending a file to jffs2 during reflash on the fly
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@12250 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/mtd/src')
-rw-r--r--package/mtd/src/jffs2.c82
-rw-r--r--package/mtd/src/mtd.c75
-rw-r--r--package/mtd/src/mtd.h10
3 files changed, 109 insertions, 58 deletions
diff --git a/package/mtd/src/jffs2.c b/package/mtd/src/jffs2.c
index f614a336ea..3b47d4158f 100644
--- a/package/mtd/src/jffs2.c
+++ b/package/mtd/src/jffs2.c
@@ -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 JFFS2_EOF "\xde\xad\xc0\xde"
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 target_ino = 0;
static void prep_eraseblock(void);
@@ -65,7 +65,7 @@ static void prep_eraseblock(void)
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;
@@ -97,7 +97,7 @@ static int add_dirent(char *name, char type, int parent)
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;
@@ -128,12 +128,13 @@ static int add_dir(char *name, int parent)
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;
- char wbuf[4096], *fname;
+ char wbuf[4096];
+ const char *fname;
FILE *f;
if (stat(name, &st)) {
@@ -204,9 +205,53 @@ static void add_file(char *name, int parent)
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;
@@ -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;
- unsigned int ofs = 0;
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;
- 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) {
diff --git a/package/mtd/src/mtd.c b/package/mtd/src/mtd.c
index f3a13efd24..027c859df3 100644
--- a/package/mtd/src/mtd.c
+++ b/package/mtd/src/mtd.c
@@ -43,11 +43,10 @@
#include <sys/stat.h>
#include <sys/reboot.h>
#include <linux/reboot.h>
-
#include "mtd-api.h"
+#include "mtd.h"
#define TRX_MAGIC 0x30524448 /* "HDR0" */
-#define BUFSIZE (16 * 1024)
#define MAX_ARGS 8
#define DEBUG
@@ -66,9 +65,10 @@ struct trx_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;
@@ -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);
@@ -205,6 +205,9 @@ static int mtd_check(const char *mtd)
if (!fd)
return 0;
+ if (!buf)
+ buf = malloc(erasesize);
+
close(fd);
return 1;
}
@@ -316,15 +319,29 @@ mtd_write(int imagefd, const char *mtd)
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 */
- while (w > e) {
+ while (w + buflen > 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 ((result = write(fd, buf, r)) < r) {
+ if ((result = write(fd, buf, buflen)) < buflen) {
if (result < 0) {
fprintf(stderr, "Error writing image.\n");
exit(1);
@@ -346,12 +363,18 @@ mtd_write(int imagefd, const char *mtd)
exit(1);
}
}
-
+ w += buflen;
+
+ /* not enough data - eof */
+ if (buflen < erasesize)
+ break;
+
buflen = 0;
}
if (!quiet)
fprintf(stderr, "\b\b\b\b");
+done:
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"
+ " -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");
@@ -399,7 +423,6 @@ int main (int argc, char **argv)
{
int ch, i, boot, unlock, imagefd, force, unlocked;
char *erase[MAX_ARGS], *device;
- char *jffs2dir = JFFS2_DEFAULT_DIR;
enum {
CMD_ERASE,
CMD_WRITE,
@@ -414,7 +437,7 @@ int main (int argc, char **argv)
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;
@@ -422,6 +445,9 @@ int main (int argc, char **argv)
case 'r':
boot = 1;
break;
+ case 'j':
+ jffs2file = optarg;
+ 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 */
- 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;
diff --git a/package/mtd/src/mtd.h b/package/mtd/src/mtd.h
index 25dcef05c9..42b6610eb9 100644
--- a/package/mtd/src/mtd.h
+++ b/package/mtd/src/mtd.h
@@ -1,14 +1,18 @@
#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 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_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 */