1
0
mirror of git://projects.qi-hardware.com/openwrt-xburst.git synced 2024-11-24 03:30:37 +02:00

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
This commit is contained in:
nbd 2008-08-08 22:43:19 +00:00
parent ee25f77757
commit 201ca503a1
4 changed files with 110 additions and 59 deletions

View File

@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=mtd 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)

View File

@ -14,7 +14,6 @@
#define PAD(x) (((x)+3)&~3) #define PAD(x) (((x)+3)&~3)
#define CLEANMARKER "\x85\x19\x03\x20\x0c\x00\x00\x00\xb1\xb0\x1e\xe4" #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_ino = 0;
static int last_version = 0; static int last_version = 0;
@ -22,6 +21,7 @@ static char *buf = NULL;
static int ofs = 0; static int ofs = 0;
static int outfd = 0; static int outfd = 0;
static int mtdofs = 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; struct jffs2_raw_inode ri;
int inode; 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; int inode, f_offset = 0, fd;
struct jffs2_raw_inode ri; struct jffs2_raw_inode ri;
struct stat st; struct stat st;
char wbuf[4096], *fname; char wbuf[4096];
const char *fname;
FILE *f; FILE *f;
if (stat(name, &st)) { 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; int err = -1, fdeof = 0;
off_t offset; 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 */ * locate the directory that the file is going to be placed in */
for(;;) { for(;;) {
struct jffs2_unknown_node *node = (struct jffs2_unknown_node *) buf; struct jffs2_unknown_node *node = (struct jffs2_unknown_node *) buf;
unsigned int ofs = 0;
if (read(outfd, buf, erasesize) != erasesize) { if (read(outfd, buf, erasesize) != erasesize) {
fdeof = 1; fdeof = 1;
@ -248,27 +292,7 @@ int mtd_write_jffs2(char *mtd, char *filename, char *dir)
if (node->magic != 0x1985) if (node->magic != 0x1985)
break; break;
while (ofs < erasesize) { mtd_parse_jffs2data(buf, dir);
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;
}
}
} }
if (fdeof) { if (fdeof) {

View File

@ -43,11 +43,10 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/reboot.h> #include <sys/reboot.h>
#include <linux/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 MAX_ARGS 8
#define DEBUG #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 *buf = NULL;
static char *imagefile; static char *imagefile = NULL;
static int buflen; static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
static int buflen = 0;
int quiet; int quiet;
int mtdsize = 0; int mtdsize = 0;
int erasesize = 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); lseek(fd, offset, SEEK_SET);
write(fd, buf, length); write(fd, buf, length);
@ -205,6 +205,9 @@ static int mtd_check(const char *mtd)
if (!fd) if (!fd)
return 0; return 0;
if (!buf)
buf = malloc(erasesize);
close(fd); close(fd);
return 1; return 1;
} }
@ -316,15 +319,29 @@ mtd_write(int imagefd, const char *mtd)
for (;;) { for (;;) {
/* buffer may contain data already (from trx check) */ /* buffer may contain data already (from trx check) */
r = buflen; r = read(imagefd, buf + buflen, erasesize - buflen);
r += read(imagefd, buf + buflen, BUFSIZE - buflen); if (r < 0)
w += r; break;
/* EOF */ buflen += r;
if (r <= 0) break;
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) if (!quiet)
fprintf(stderr, "\b\b\b[e]"); fprintf(stderr, "\b\b\b[e]");
@ -337,7 +354,7 @@ mtd_write(int imagefd, const char *mtd)
if (!quiet) if (!quiet)
fprintf(stderr, "\b\b\b[w]"); fprintf(stderr, "\b\b\b[w]");
if ((result = write(fd, buf, r)) < r) { if ((result = write(fd, buf, buflen)) < buflen) {
if (result < 0) { if (result < 0) {
fprintf(stderr, "Error writing image.\n"); fprintf(stderr, "Error writing image.\n");
exit(1); 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; buflen = 0;
} }
if (!quiet) if (!quiet)
fprintf(stderr, "\b\b\b\b"); fprintf(stderr, "\b\b\b\b");
done:
if (quiet < 2) if (quiet < 2)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -376,6 +399,7 @@ static void usage(void)
" -f force write without trx checks\n" " -f force write without trx checks\n"
" -e <device> erase <device> before executing the command\n" " -e <device> erase <device> before executing the command\n"
" -d <name> directory for jffs2write, defaults to \"tmp\"\n" " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
" -j <name> integrate <file> into jffs2 data when writing an image\n"
"\n" "\n"
"Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n" "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
" mtd -r write linux.trx linux\n\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; int ch, i, boot, unlock, imagefd, force, unlocked;
char *erase[MAX_ARGS], *device; char *erase[MAX_ARGS], *device;
char *jffs2dir = JFFS2_DEFAULT_DIR;
enum { enum {
CMD_ERASE, CMD_ERASE,
CMD_WRITE, CMD_WRITE,
@ -414,7 +437,7 @@ int main (int argc, char **argv)
buflen = 0; buflen = 0;
quiet = 0; quiet = 0;
while ((ch = getopt(argc, argv, "frqe:d:")) != -1) while ((ch = getopt(argc, argv, "frqe:d:j:")) != -1)
switch (ch) { switch (ch) {
case 'f': case 'f':
force = 1; force = 1;
@ -422,6 +445,9 @@ int main (int argc, char **argv)
case 'r': case 'r':
boot = 1; boot = 1;
break; break;
case 'j':
jffs2file = optarg;
break;
case 'q': case 'q':
quiet++; quiet++;
break; 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 (!image_check(imagefd, device) && !force) {
if (!force) { fprintf(stderr, "Image check failed.\n");
fprintf(stderr, "Image check failed.\n"); exit(1);
exit(1);
}
} else {
if (!mtd_check(device)) {
fprintf(stderr, "Can't open device for writing!\n");
exit(1);
}
} }
} else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) { } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
cmd = CMD_JFFS2WRITE; cmd = CMD_JFFS2WRITE;

View File

@ -1,14 +1,18 @@
#ifndef __mtd_h #ifndef __mtd_h
#define __mtd_h #define __mtd_h
#define JFFS2_EOF "\xde\xad\xc0\xde"
extern int quiet; extern int quiet;
extern int mtdsize; extern int mtdsize;
extern int erasesize; extern int erasesize;
extern int mtd_open(const char *mtd); 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_buffer(int fd, const char *buf, int offset, int length);
extern int mtd_write_jffs2(char *mtd, char *filename, char *dir); 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 */