4 * Copyright (C) 1999 Linus Torvalds
6 * Copyright (C) 2000-2002 Transmeta Corporation
8 * Copyright (C) 2003 Kai-Uwe Bloem,
9 * Auerswald GmbH & Co KG, <linux-development@auerswald.de>
10 * - adapted from the www.tuxbox.org u-boot tree, added "ls" command
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License (Version 2) as
14 * published by the Free Software Foundation.
16 * Compressed ROM filesystem for Linux.
19 * add support for resolving symbolic links
23 * These are the VFS interfaces to the compressed ROM filesystem.
24 * The actual compression is based on zlib, see the other files.
30 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
32 #include <asm/byteorder.h>
33 #include <linux/stat.h>
34 #include <jffs2/jffs2.h>
35 #include <jffs2/load_kernel.h>
36 #include <cramfs/cramfs_fs.h>
38 /* These two macros may change in future, to provide better st_ino
40 #define CRAMINO(x) (CRAMFS_GET_OFFSET(x) ? CRAMFS_GET_OFFSET(x)<<2 : 1)
41 #define OFFSET(x) ((x)->i_ino)
43 struct cramfs_super super
;
45 /* CPU address space offset calculation macro, struct part_info offset is
46 * device address space offset, so we need to shift it by a device start address. */
47 extern flash_info_t flash_info
[];
48 #define PART_OFFSET(x) (x->offset + flash_info[x->dev->id->num].start[0])
50 static int cramfs_read_super (struct part_info
*info
)
52 unsigned long root_offset
;
54 /* Read the first block and get the superblock from it */
55 memcpy (&super
, (void *) PART_OFFSET(info
), sizeof (super
));
57 /* Do sanity checks on the superblock */
58 if (super
.magic
!= CRAMFS_32 (CRAMFS_MAGIC
)) {
59 /* check at 512 byte offset */
60 memcpy (&super
, (void *) PART_OFFSET(info
) + 512, sizeof (super
));
61 if (super
.magic
!= CRAMFS_32 (CRAMFS_MAGIC
)) {
62 printf ("cramfs: wrong magic\n");
67 /* flags is reused several times, so swab it once */
68 super
.flags
= CRAMFS_32 (super
.flags
);
69 super
.size
= CRAMFS_32 (super
.size
);
71 /* get feature flags first */
72 if (super
.flags
& ~CRAMFS_SUPPORTED_FLAGS
) {
73 printf ("cramfs: unsupported filesystem features\n");
77 /* Check that the root inode is in a sane state */
78 if (!S_ISDIR (CRAMFS_16 (super
.root
.mode
))) {
79 printf ("cramfs: root is not a directory\n");
82 root_offset
= CRAMFS_GET_OFFSET (&(super
.root
)) << 2;
83 if (root_offset
== 0) {
84 printf ("cramfs: empty filesystem");
85 } else if (!(super
.flags
& CRAMFS_FLAG_SHIFTED_ROOT_OFFSET
) &&
86 ((root_offset
!= sizeof (struct cramfs_super
)) &&
87 (root_offset
!= 512 + sizeof (struct cramfs_super
)))) {
88 printf ("cramfs: bad root offset %lu\n", root_offset
);
95 static unsigned long cramfs_resolve (unsigned long begin
, unsigned long offset
,
96 unsigned long size
, int raw
,
99 unsigned long inodeoffset
= 0, nextoffset
;
101 while (inodeoffset
< size
) {
102 struct cramfs_inode
*inode
;
106 inode
= (struct cramfs_inode
*) (begin
+ offset
+
110 * Namelengths on disk are shifted by two
111 * and the name padded out to 4-byte boundaries
114 namelen
= CRAMFS_GET_NAMELEN (inode
) << 2;
115 name
= (char *) inode
+ sizeof (struct cramfs_inode
);
118 inodeoffset
+ sizeof (struct cramfs_inode
) + namelen
;
123 if (name
[namelen
- 1])
128 if (!strncmp (filename
, name
, namelen
)) {
129 char *p
= strtok (NULL
, "/");
131 if (raw
&& (p
== NULL
|| *p
== '\0'))
132 return offset
+ inodeoffset
;
134 if (S_ISDIR (CRAMFS_16 (inode
->mode
))) {
135 return cramfs_resolve (begin
,
141 } else if (S_ISREG (CRAMFS_16 (inode
->mode
))) {
142 return offset
+ inodeoffset
;
144 printf ("%*.*s: unsupported file type (%x)\n",
145 namelen
, namelen
, name
,
146 CRAMFS_16 (inode
->mode
));
151 inodeoffset
= nextoffset
;
154 printf ("can't find corresponding entry\n");
158 static int cramfs_uncompress (unsigned long begin
, unsigned long offset
,
159 unsigned long loadoffset
)
161 struct cramfs_inode
*inode
= (struct cramfs_inode
*) (begin
+ offset
);
162 unsigned long *block_ptrs
= (unsigned long *)
163 (begin
+ (CRAMFS_GET_OFFSET (inode
) << 2));
164 unsigned long curr_block
= (CRAMFS_GET_OFFSET (inode
) +
165 (((CRAMFS_24 (inode
->size
)) +
167 int size
, total_size
= 0;
170 cramfs_uncompress_init ();
172 for (i
= 0; i
< ((CRAMFS_24 (inode
->size
) + 4095) >> 12); i
++) {
173 size
= cramfs_uncompress_block ((void *) loadoffset
,
174 (void *) (begin
+ curr_block
),
175 (CRAMFS_32 (block_ptrs
[i
]) -
181 curr_block
= CRAMFS_32 (block_ptrs
[i
]);
184 cramfs_uncompress_exit ();
188 int cramfs_load (char *loadoffset
, struct part_info
*info
, char *filename
)
190 unsigned long offset
;
192 if (cramfs_read_super (info
))
195 offset
= cramfs_resolve (PART_OFFSET(info
),
196 CRAMFS_GET_OFFSET (&(super
.root
)) << 2,
197 CRAMFS_24 (super
.root
.size
), 0,
198 strtok (filename
, "/"));
203 return cramfs_uncompress (PART_OFFSET(info
), offset
,
204 (unsigned long) loadoffset
);
207 static int cramfs_list_inode (struct part_info
*info
, unsigned long offset
)
209 struct cramfs_inode
*inode
= (struct cramfs_inode
*)
210 (PART_OFFSET(info
) + offset
);
212 int namelen
, nextoff
;
215 * Namelengths on disk are shifted by two
216 * and the name padded out to 4-byte boundaries
219 namelen
= CRAMFS_GET_NAMELEN (inode
) << 2;
220 name
= (char *) inode
+ sizeof (struct cramfs_inode
);
226 if (name
[namelen
- 1])
231 printf (" %s %8d %*.*s", mkmodestr (CRAMFS_16 (inode
->mode
), str
),
232 CRAMFS_24 (inode
->size
), namelen
, namelen
, name
);
234 if ((CRAMFS_16 (inode
->mode
) & S_IFMT
) == S_IFLNK
) {
236 * Unpack the link target, trusting in the inode's size field.
238 unsigned long size
= CRAMFS_24 (inode
->size
);
239 char *link
= malloc (size
);
241 if (link
!= NULL
&& cramfs_uncompress (PART_OFFSET(info
), offset
,
242 (unsigned long) link
)
244 printf (" -> %*.*s\n", (int) size
, (int) size
, link
);
246 printf (" [Error reading link]\n");
255 int cramfs_ls (struct part_info
*info
, char *filename
)
257 struct cramfs_inode
*inode
;
258 unsigned long inodeoffset
= 0, nextoffset
;
259 unsigned long offset
, size
;
261 if (cramfs_read_super (info
))
264 if (strlen (filename
) == 0 || !strcmp (filename
, "/")) {
265 /* Root directory. Use root inode in super block */
266 offset
= CRAMFS_GET_OFFSET (&(super
.root
)) << 2;
267 size
= CRAMFS_24 (super
.root
.size
);
269 /* Resolve the path */
270 offset
= cramfs_resolve (PART_OFFSET(info
),
271 CRAMFS_GET_OFFSET (&(super
.root
)) <<
272 2, CRAMFS_24 (super
.root
.size
), 1,
273 strtok (filename
, "/"));
278 /* Resolving was successful. Examine the inode */
279 inode
= (struct cramfs_inode
*) (PART_OFFSET(info
) + offset
);
280 if (!S_ISDIR (CRAMFS_16 (inode
->mode
))) {
281 /* It's not a directory - list it, and that's that */
282 return (cramfs_list_inode (info
, offset
) > 0);
285 /* It's a directory. List files within */
286 offset
= CRAMFS_GET_OFFSET (inode
) << 2;
287 size
= CRAMFS_24 (inode
->size
);
290 /* List the given directory */
291 while (inodeoffset
< size
) {
292 inode
= (struct cramfs_inode
*) (PART_OFFSET(info
) + offset
+
295 nextoffset
= cramfs_list_inode (info
, offset
+ inodeoffset
);
298 inodeoffset
+= sizeof (struct cramfs_inode
) + nextoffset
;
304 int cramfs_info (struct part_info
*info
)
306 if (cramfs_read_super (info
))
309 printf ("size: 0x%x (%u)\n", super
.size
, super
.size
);
311 if (super
.flags
!= 0) {
313 if (super
.flags
& CRAMFS_FLAG_FSID_VERSION_2
)
314 printf ("\tFSID version 2\n");
315 if (super
.flags
& CRAMFS_FLAG_SORTED_DIRS
)
316 printf ("\tsorted dirs\n");
317 if (super
.flags
& CRAMFS_FLAG_HOLES
)
318 printf ("\tholes\n");
319 if (super
.flags
& CRAMFS_FLAG_SHIFTED_ROOT_OFFSET
)
320 printf ("\tshifted root offset\n");
323 printf ("fsid:\n\tcrc: 0x%x\n\tedition: 0x%x\n",
324 super
.fsid
.crc
, super
.fsid
.edition
);
325 printf ("name: %16s\n", super
.name
);
330 int cramfs_check (struct part_info
*info
)
332 struct cramfs_super
*sb
;
334 if (info
->dev
->id
->type
!= MTD_DEV_TYPE_NOR
)
337 sb
= (struct cramfs_super
*) PART_OFFSET(info
);
338 if (sb
->magic
!= CRAMFS_32 (CRAMFS_MAGIC
)) {
339 /* check at 512 byte offset */
340 sb
= (struct cramfs_super
*) (PART_OFFSET(info
) + 512);
341 if (sb
->magic
!= CRAMFS_32 (CRAMFS_MAGIC
))
347 #endif /* CFG_FS_CRAMFS */