2 * (C) Copyright 2007 Michal Simek
4 * Michal SIMEK <monstr@monstr.eu>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
31 #include <asm/byteorder.h>
32 #include <linux/stat.h>
33 #include <jffs2/jffs2.h>
34 #include <jffs2/load_kernel.h>
38 /* ROMFS superblock */
55 extern flash_info_t flash_info[];
56 #define PART_OFFSET(x) (x->offset + flash_info[x->dev->id->num].start[0])
57 #define ALIGN(x) (((x) & 0xfffffff0))
58 #define HEADERSIZE(name) (0x20 + ALIGN(strlen(name)))
60 static unsigned long romfs_resolve (unsigned long begin, unsigned long offset,
61 unsigned long size, int raw, char *filename)
63 unsigned long inodeoffset = 0, nextoffset;
64 struct romfs_inode *inode;
66 printf ("ROMFS_resolve: begin 0x%x, offset 0x%x, size 0x%x, raw 0x%x, \
67 filename %s\n", begin, offset, size, raw, filename);
70 while (inodeoffset < size) {
71 inode = (struct romfs_inode *)(begin + offset + inodeoffset);
73 nextoffset = ALIGN (inode->next);
75 printf("inode 0x%x, name %s - len 0x%x, next inode 0x%x, \
76 compare names 0x%x\n",
77 inode, inode->name, strlen (inode->name), nextoffset,
78 strncmp (filename, inode->name, strlen (filename)));
80 if (!strncmp (filename, inode->name, strlen (inode->name))) {
81 char *p = strtok (NULL, "/");
82 if (raw && (p == NULL || *p == '\0')) {
83 return offset + inodeoffset;
85 return romfs_resolve (begin,
86 inodeoffset + HEADERSIZE (inode->name),
89 inodeoffset = nextoffset;
92 printf ("can't find corresponding entry\n");
96 int romfs_load (char *loadoffset, struct part_info *info, char *filename)
98 struct romfs_inode *inode;
99 struct romfs_super *sb;
102 sb = (struct romfs_super *) PART_OFFSET (info);
104 unsigned long offset;
106 offset = romfs_resolve (PART_OFFSET (info), HEADERSIZE (sb->name),
107 sb->size, 1, strtok (filename, "/"));
111 inode = (struct romfs_inode *)(PART_OFFSET (info) + offset);
112 data = (char *)((int)inode + HEADERSIZE (inode->name));
115 *loadoffset++ = *data++;
120 static int romfs_list_inode (struct part_info *info, unsigned long offset)
122 struct romfs_inode *inode =
123 (struct romfs_inode *)(PART_OFFSET (info) + offset);
124 struct romfs_inode *hardlink = NULL;
127 /* mapping spec.info means
128 * 0 hard link link destination [file header]
129 * 1 directory first file's header
130 * 2 regular file unused, must be zero [MBZ]
131 * 3 symbolic link unused, MBZ (file data is the link content)
132 * 4 block device 16/16 bits major/minor number
133 * 5 char device - " -
134 * 6 socket unused, MBZ
137 switch (inode->next & 0x7) {
166 if (inode->next & 0x8) {
173 if ((str[0] == 'b') || (str[0] == 'c')) {
175 printf (" %s %3d,%3d %12s 0x%08x 0x%08x", str,
176 (inode->spec & 0xffff0000) >> 16,
177 inode->spec & 0x0000ffff, inode->name, inode,
180 printf (" %s %3d,%3d %12s", str,
181 (inode->spec & 0xffff0000) >> 16,
182 inode->spec & 0x0000ffff);
186 printf (" %s %7d %12s 0x%08x 0x%08x", str, inode->size,
187 inode->name, inode, inode->spec);
189 printf (" %s %7d %12s", str, inode->size, inode->name);
192 data = (char *)((int)inode + HEADERSIZE (inode->name));
197 hardlink = (struct romfs_inode *)(PART_OFFSET (info) +
200 puts (hardlink->name);
204 return ALIGN (inode->next);
207 int romfs_ls (struct part_info *info, char *filename)
209 struct romfs_inode *inode;
210 unsigned long inodeoffset = 0, nextoffset;
211 unsigned long offset, size;
212 struct romfs_super *sb;
213 sb = (struct romfs_super *)PART_OFFSET (info);
215 if (strlen (filename) == 0 || !strcmp (filename, "/")) {
216 offset = HEADERSIZE (sb->name);
219 offset = romfs_resolve (PART_OFFSET (info),
220 HEADERSIZE (sb->name), sb->size, 1,
221 strtok (filename, "/"));
226 inode = (struct romfs_inode *)(PART_OFFSET (info) + offset);
227 if ((inode->next & 0x7) != 1) {
228 return (romfs_list_inode (info, offset) > 0);
232 offset = offset + HEADERSIZE (inode->name);
235 inodeoffset = offset + inodeoffset;
236 while (inodeoffset < size) {
237 nextoffset = romfs_list_inode (info, inodeoffset);
240 inodeoffset = nextoffset;
245 int romfs_info (struct part_info *info)
247 struct romfs_super *sb;
248 sb = (struct romfs_super *)PART_OFFSET (info);
250 printf ("name: \t\t%s, len %d B\n", sb->name, strlen (sb->name));
251 printf ("size of SB:\t%d B\n", HEADERSIZE (sb->name));
252 printf ("full size:\t%d B\n", sb->size);
253 printf ("checksum:\t0x%x\n", sb->checksum);
257 int romfs_check (struct part_info *info)
259 struct romfs_super *sb;
260 if (info->dev->id->type != MTD_DEV_TYPE_NOR)
263 sb = (struct romfs_super *)PART_OFFSET (info);
264 if ((sb->word0 != 0x2D726F6D) || (sb->word1 != 0x3166732D)) {