2 * jffs2 on-disk structure generator for mtd
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License v2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * JFFS2 -- Journalling Flash File System, Version 2.
16 * Copyright © 2001-2007 Red Hat, Inc.
17 * Created by David Woodhouse <dwmw2@infradead.org>
19 #include <sys/types.h>
33 #define PAD(x) (((x)+3)&~3)
35 #if BYTE_ORDER == BIG_ENDIAN
36 # define CLEANMARKER "\x19\x85\x20\x03\x00\x00\x00\x0c\xf0\x60\xdc\x98"
38 # define CLEANMARKER "\x85\x19\x03\x20\x0c\x00\x00\x00\xb1\xb0\x1e\xe4"
41 static int last_ino
= 0;
42 static int last_version
= 0;
43 static char *buf
= NULL
;
45 static int outfd
= -1;
46 static int mtdofs
= 0;
47 static int target_ino
= 0;
49 static void prep_eraseblock(void);
51 static void pad(int size
)
53 if ((ofs
% size
== 0) && (ofs
< erasesize
))
56 if (ofs
< erasesize
) {
57 memset(buf
+ ofs
, 0xff, (size
- (ofs
% size
)));
58 ofs
+= (size
- (ofs
% size
));
60 ofs
= ofs
% erasesize
;
62 mtd_erase_block(outfd
, mtdofs
);
63 write(outfd
, buf
, erasesize
);
68 static inline int rbytes(void)
70 return erasesize
- (ofs
% erasesize
);
73 static inline void add_data(char *ptr
, int len
)
75 if (ofs
+ len
> erasesize
) {
79 memcpy(buf
+ ofs
, ptr
, len
);
83 static void prep_eraseblock(void)
88 add_data(CLEANMARKER
, sizeof(CLEANMARKER
) - 1);
91 static int add_dirent(const char *name
, const char type
, int parent
)
93 struct jffs2_raw_dirent
*de
;
95 if (ofs
- erasesize
< sizeof(struct jffs2_raw_dirent
) + strlen(name
))
100 memset(buf
+ ofs
, 0, sizeof(struct jffs2_raw_dirent
));
101 de
= (struct jffs2_raw_dirent
*) (buf
+ ofs
);
103 de
->magic
= JFFS2_MAGIC_BITMASK
;
104 de
->nodetype
= JFFS2_NODETYPE_DIRENT
;
106 de
->name_crc
= crc32(0, name
, strlen(name
));
107 de
->ino
= last_ino
++;
109 de
->totlen
= sizeof(*de
) + strlen(name
);
110 de
->hdr_crc
= crc32(0, (void *) de
, sizeof(struct jffs2_unknown_node
) - 4);
111 de
->version
= last_version
++;
113 de
->nsize
= strlen(name
);
114 de
->node_crc
= crc32(0, (void *) de
, sizeof(*de
) - 8);
115 memcpy(de
->name
, name
, strlen(name
));
117 ofs
+= sizeof(struct jffs2_raw_dirent
) + de
->nsize
;
123 static int add_dir(const char *name
, int parent
)
125 struct jffs2_raw_inode ri
;
128 inode
= add_dirent(name
, IFTODT(S_IFDIR
), parent
);
130 if (rbytes() < sizeof(ri
))
134 memset(&ri
, 0, sizeof(ri
));
135 ri
.magic
= JFFS2_MAGIC_BITMASK
;
136 ri
.nodetype
= JFFS2_NODETYPE_INODE
;
137 ri
.totlen
= sizeof(ri
);
138 ri
.hdr_crc
= crc32(0, &ri
, sizeof(struct jffs2_unknown_node
) - 4);
141 ri
.mode
= S_IFDIR
| 0755;
143 ri
.atime
= ri
.ctime
= ri
.mtime
= 0;
144 ri
.isize
= ri
.csize
= ri
.dsize
= 0;
146 ri
.node_crc
= crc32(0, &ri
, sizeof(ri
) - 8);
149 add_data((char *) &ri
, sizeof(ri
));
154 static void add_file(const char *name
, int parent
)
156 int inode
, f_offset
= 0, fd
;
157 struct jffs2_raw_inode ri
;
162 if (stat(name
, &st
)) {
163 fprintf(stderr
, "File %s does not exist\n", name
);
167 fname
= strrchr(name
, '/');
173 inode
= add_dirent(fname
, IFTODT(S_IFREG
), parent
);
174 memset(&ri
, 0, sizeof(ri
));
175 ri
.magic
= JFFS2_MAGIC_BITMASK
;
176 ri
.nodetype
= JFFS2_NODETYPE_INODE
;
179 ri
.mode
= st
.st_mode
;
181 ri
.atime
= st
.st_atime
;
182 ri
.ctime
= st
.st_ctime
;
183 ri
.mtime
= st
.st_mtime
;
184 ri
.isize
= st
.st_size
;
190 fprintf(stderr
, "File %s does not exist\n", name
);
198 len
= rbytes() - sizeof(ri
);
206 if (len
> sizeof(wbuf
))
209 len
= read(fd
, wbuf
, len
);
213 ri
.totlen
= sizeof(ri
) + len
;
214 ri
.hdr_crc
= crc32(0, &ri
, sizeof(struct jffs2_unknown_node
) - 4);
215 ri
.version
= ++last_version
;
216 ri
.offset
= f_offset
;
217 ri
.csize
= ri
.dsize
= len
;
218 ri
.node_crc
= crc32(0, &ri
, sizeof(ri
) - 8);
219 ri
.data_crc
= crc32(0, wbuf
, len
);
221 add_data((char *) &ri
, sizeof(ri
));
230 int mtd_replace_jffs2(const char *mtd
, int fd
, int ofs
, const char *filename
)
235 buf
= malloc(erasesize
);
239 add_file(filename
, target_ino
);
242 /* add eof marker, pad to eraseblock size and write the data */
243 add_data(JFFS2_EOF
, sizeof(JFFS2_EOF
) - 1);
248 trx_fixup(outfd
, mtd
);
250 return (mtdofs
- ofs
);
253 void mtd_parse_jffs2data(const char *buf
, const char *dir
)
255 struct jffs2_unknown_node
*node
= (struct jffs2_unknown_node
*) buf
;
256 unsigned int ofs
= 0;
258 while (ofs
< erasesize
) {
259 node
= (struct jffs2_unknown_node
*) (buf
+ ofs
);
260 if (node
->magic
!= 0x1985)
263 ofs
+= PAD(node
->totlen
);
264 if (node
->nodetype
== JFFS2_NODETYPE_DIRENT
) {
265 struct jffs2_raw_dirent
*de
= (struct jffs2_raw_dirent
*) node
;
267 /* is this the right directory name and is it a subdirectory of / */
268 if (*dir
&& (de
->pino
== 1) && !strncmp((char *) de
->name
, dir
, de
->nsize
))
269 target_ino
= de
->ino
;
271 /* store the last inode and version numbers for adding extra files */
272 if (last_ino
< de
->ino
)
274 if (last_version
< de
->version
)
275 last_version
= de
->version
;
280 int mtd_write_jffs2(const char *mtd
, const char *filename
, const char *dir
)
282 int err
= -1, fdeof
= 0;
284 outfd
= mtd_check_open(mtd
);
289 fprintf(stderr
, "Appending %s to jffs2 partition %s\n", filename
, mtd
);
291 buf
= malloc(erasesize
);
293 fprintf(stderr
, "Out of memory!\n");
300 /* parse the structure of the jffs2 first
301 * locate the directory that the file is going to be placed in */
303 struct jffs2_unknown_node
*node
= (struct jffs2_unknown_node
*) buf
;
305 if (read(outfd
, buf
, erasesize
) != erasesize
) {
311 if (node
->magic
== 0x8519) {
312 fprintf(stderr
, "Error: wrong endianness filesystem\n");
316 /* assume no magic == end of filesystem
317 * the filesystem will probably end with be32(0xdeadc0de) */
318 if (node
->magic
!= 0x1985)
321 mtd_parse_jffs2data(buf
, dir
);
325 fprintf(stderr
, "Error: No room for additional data\n");
329 /* jump back one eraseblock */
331 lseek(outfd
, mtdofs
, SEEK_SET
);
339 target_ino
= add_dir(dir
, 1);
341 add_file(filename
, target_ino
);
344 /* add eof marker, pad to eraseblock size and write the data */
345 add_data(JFFS2_EOF
, sizeof(JFFS2_EOF
) - 1);
351 trx_fixup(outfd
, mtd
);
This page took 0.061682 seconds and 5 git commands to generate.