1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <common.h>
28#include <malloc.h>
29#include <asm/byteorder.h>
30#include <linux/stat.h>
31#include <jffs2/jffs2.h>
32#include <jffs2/load_kernel.h>
33#include <cramfs/cramfs_fs.h>
34
35
36
37#define CRAMINO(x) (CRAMFS_GET_OFFSET(x) ? CRAMFS_GET_OFFSET(x)<<2 : 1)
38#define OFFSET(x) ((x)->i_ino)
39
40struct cramfs_super super;
41
42
43
44extern flash_info_t flash_info[];
45#define PART_OFFSET(x) (x->offset + flash_info[x->dev->id->num].start[0])
46
47static int cramfs_read_super (struct part_info *info)
48{
49 unsigned long root_offset;
50
51
52 memcpy (&super, (void *) PART_OFFSET(info), sizeof (super));
53
54
55 if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
56
57 memcpy (&super, (void *) PART_OFFSET(info) + 512, sizeof (super));
58 if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
59 printf ("cramfs: wrong magic\n");
60 return -1;
61 }
62 }
63
64
65 super.flags = CRAMFS_32 (super.flags);
66 super.size = CRAMFS_32 (super.size);
67
68
69 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
70 printf ("cramfs: unsupported filesystem features\n");
71 return -1;
72 }
73
74
75 if (!S_ISDIR (CRAMFS_16 (super.root.mode))) {
76 printf ("cramfs: root is not a directory\n");
77 return -1;
78 }
79 root_offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
80 if (root_offset == 0) {
81 printf ("cramfs: empty filesystem");
82 } else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
83 ((root_offset != sizeof (struct cramfs_super)) &&
84 (root_offset != 512 + sizeof (struct cramfs_super)))) {
85 printf ("cramfs: bad root offset %lu\n", root_offset);
86 return -1;
87 }
88
89 return 0;
90}
91
92static unsigned long cramfs_resolve (unsigned long begin, unsigned long offset,
93 unsigned long size, int raw,
94 char *filename)
95{
96 unsigned long inodeoffset = 0, nextoffset;
97
98 while (inodeoffset < size) {
99 struct cramfs_inode *inode;
100 char *name;
101 int namelen;
102
103 inode = (struct cramfs_inode *) (begin + offset +
104 inodeoffset);
105
106
107
108
109
110
111 namelen = CRAMFS_GET_NAMELEN (inode) << 2;
112 name = (char *) inode + sizeof (struct cramfs_inode);
113
114 nextoffset =
115 inodeoffset + sizeof (struct cramfs_inode) + namelen;
116
117 for (;;) {
118 if (!namelen)
119 return -1;
120 if (name[namelen - 1])
121 break;
122 namelen--;
123 }
124
125 if (!strncmp (filename, name, namelen)) {
126 char *p = strtok (NULL, "/");
127
128 if (raw && (p == NULL || *p == '\0'))
129 return offset + inodeoffset;
130
131 if (S_ISDIR (CRAMFS_16 (inode->mode))) {
132 return cramfs_resolve (begin,
133 CRAMFS_GET_OFFSET
134 (inode) << 2,
135 CRAMFS_24 (inode->
136 size), raw,
137 p);
138 } else if (S_ISREG (CRAMFS_16 (inode->mode))) {
139 return offset + inodeoffset;
140 } else {
141 printf ("%*.*s: unsupported file type (%x)\n",
142 namelen, namelen, name,
143 CRAMFS_16 (inode->mode));
144 return 0;
145 }
146 }
147
148 inodeoffset = nextoffset;
149 }
150
151 printf ("can't find corresponding entry\n");
152 return 0;
153}
154
155static int cramfs_uncompress (unsigned long begin, unsigned long offset,
156 unsigned long loadoffset)
157{
158 struct cramfs_inode *inode = (struct cramfs_inode *) (begin + offset);
159 unsigned long *block_ptrs = (unsigned long *)
160 (begin + (CRAMFS_GET_OFFSET (inode) << 2));
161 unsigned long curr_block = (CRAMFS_GET_OFFSET (inode) +
162 (((CRAMFS_24 (inode->size)) +
163 4095) >> 12)) << 2;
164 int size, total_size = 0;
165 int i;
166
167 cramfs_uncompress_init ();
168
169 for (i = 0; i < ((CRAMFS_24 (inode->size) + 4095) >> 12); i++) {
170 size = cramfs_uncompress_block ((void *) loadoffset,
171 (void *) (begin + curr_block),
172 (CRAMFS_32 (block_ptrs[i]) -
173 curr_block));
174 if (size < 0)
175 return size;
176 loadoffset += size;
177 total_size += size;
178 curr_block = CRAMFS_32 (block_ptrs[i]);
179 }
180
181 cramfs_uncompress_exit ();
182 return total_size;
183}
184
185int cramfs_load (char *loadoffset, struct part_info *info, char *filename)
186{
187 unsigned long offset;
188
189 if (cramfs_read_super (info))
190 return -1;
191
192 offset = cramfs_resolve (PART_OFFSET(info),
193 CRAMFS_GET_OFFSET (&(super.root)) << 2,
194 CRAMFS_24 (super.root.size), 0,
195 strtok (filename, "/"));
196
197 if (offset <= 0)
198 return offset;
199
200 return cramfs_uncompress (PART_OFFSET(info), offset,
201 (unsigned long) loadoffset);
202}
203
204static int cramfs_list_inode (struct part_info *info, unsigned long offset)
205{
206 struct cramfs_inode *inode = (struct cramfs_inode *)
207 (PART_OFFSET(info) + offset);
208 char *name, str[20];
209 int namelen, nextoff;
210
211
212
213
214
215
216 namelen = CRAMFS_GET_NAMELEN (inode) << 2;
217 name = (char *) inode + sizeof (struct cramfs_inode);
218 nextoff = namelen;
219
220 for (;;) {
221 if (!namelen)
222 return namelen;
223 if (name[namelen - 1])
224 break;
225 namelen--;
226 }
227
228 printf (" %s %8d %*.*s", mkmodestr (CRAMFS_16 (inode->mode), str),
229 CRAMFS_24 (inode->size), namelen, namelen, name);
230
231 if ((CRAMFS_16 (inode->mode) & S_IFMT) == S_IFLNK) {
232
233
234
235 unsigned long size = CRAMFS_24 (inode->size);
236 char *link = malloc (size);
237
238 if (link != NULL && cramfs_uncompress (PART_OFFSET(info), offset,
239 (unsigned long) link)
240 == size)
241 printf (" -> %*.*s\n", (int) size, (int) size, link);
242 else
243 printf (" [Error reading link]\n");
244 if (link)
245 free (link);
246 } else
247 printf ("\n");
248
249 return nextoff;
250}
251
252int cramfs_ls (struct part_info *info, char *filename)
253{
254 struct cramfs_inode *inode;
255 unsigned long inodeoffset = 0, nextoffset;
256 unsigned long offset, size;
257
258 if (cramfs_read_super (info))
259 return -1;
260
261 if (strlen (filename) == 0 || !strcmp (filename, "/")) {
262
263 offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
264 size = CRAMFS_24 (super.root.size);
265 } else {
266
267 offset = cramfs_resolve (PART_OFFSET(info),
268 CRAMFS_GET_OFFSET (&(super.root)) <<
269 2, CRAMFS_24 (super.root.size), 1,
270 strtok (filename, "/"));
271
272 if (offset <= 0)
273 return offset;
274
275
276 inode = (struct cramfs_inode *) (PART_OFFSET(info) + offset);
277 if (!S_ISDIR (CRAMFS_16 (inode->mode))) {
278
279 return (cramfs_list_inode (info, offset) > 0);
280 }
281
282
283 offset = CRAMFS_GET_OFFSET (inode) << 2;
284 size = CRAMFS_24 (inode->size);
285 }
286
287
288 while (inodeoffset < size) {
289 inode = (struct cramfs_inode *) (PART_OFFSET(info) + offset +
290 inodeoffset);
291
292 nextoffset = cramfs_list_inode (info, offset + inodeoffset);
293 if (nextoffset == 0)
294 break;
295 inodeoffset += sizeof (struct cramfs_inode) + nextoffset;
296 }
297
298 return 1;
299}
300
301int cramfs_info (struct part_info *info)
302{
303 if (cramfs_read_super (info))
304 return 0;
305
306 printf ("size: 0x%x (%u)\n", super.size, super.size);
307
308 if (super.flags != 0) {
309 printf ("flags:\n");
310 if (super.flags & CRAMFS_FLAG_FSID_VERSION_2)
311 printf ("\tFSID version 2\n");
312 if (super.flags & CRAMFS_FLAG_SORTED_DIRS)
313 printf ("\tsorted dirs\n");
314 if (super.flags & CRAMFS_FLAG_HOLES)
315 printf ("\tholes\n");
316 if (super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET)
317 printf ("\tshifted root offset\n");
318 }
319
320 printf ("fsid:\n\tcrc: 0x%x\n\tedition: 0x%x\n",
321 super.fsid.crc, super.fsid.edition);
322 printf ("name: %16s\n", super.name);
323
324 return 1;
325}
326
327int cramfs_check (struct part_info *info)
328{
329 struct cramfs_super *sb;
330
331 if (info->dev->id->type != MTD_DEV_TYPE_NOR)
332 return 0;
333
334 sb = (struct cramfs_super *) PART_OFFSET(info);
335 if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
336
337 sb = (struct cramfs_super *) (PART_OFFSET(info) + 512);
338 if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC))
339 return 0;
340 }
341 return 1;
342}
343