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.
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>
35 /* These two macros may change in future, to provide better st_ino
37 #define CRAMINO(x) (CRAMFS_GET_OFFSET(x) ? CRAMFS_GET_OFFSET(x)<<2 : 1)
38 #define OFFSET(x) ((x)->i_ino)
40 struct cramfs_super super
;
42 /* CPU address space offset calculation macro, struct part_info offset is
43 * device address space offset, so we need to shift it by a device start address. */
44 extern flash_info_t flash_info
[];
45 #define PART_OFFSET(x) (x->offset + flash_info[x->dev->id->num].start[0])
47 static int cramfs_read_super (struct part_info
*info
)
49 unsigned long root_offset
;
51 /* Read the first block and get the superblock from it */
52 memcpy (&super
, (void *) PART_OFFSET(info
), sizeof (super
));
54 /* Do sanity checks on the superblock */
55 if (super
.magic
!= CRAMFS_32 (CRAMFS_MAGIC
)) {
56 /* check at 512 byte offset */
57 memcpy (&super
, (void *) PART_OFFSET(info
) + 512, sizeof (super
));
58 if (super
.magic
!= CRAMFS_32 (CRAMFS_MAGIC
)) {
59 printf ("cramfs: wrong magic\n");
64 /* flags is reused several times, so swab it once */
65 super
.flags
= CRAMFS_32 (super
.flags
);
66 super
.size
= CRAMFS_32 (super
.size
);
68 /* get feature flags first */
69 if (super
.flags
& ~CRAMFS_SUPPORTED_FLAGS
) {
70 printf ("cramfs: unsupported filesystem features\n");
74 /* Check that the root inode is in a sane state */
75 if (!S_ISDIR (CRAMFS_16 (super
.root
.mode
))) {
76 printf ("cramfs: root is not a directory\n");
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
);
92 static unsigned long cramfs_resolve (unsigned long begin
, unsigned long offset
,
93 unsigned long size
, int raw
,
96 unsigned long inodeoffset
= 0, nextoffset
;
98 while (inodeoffset
< size
) {
99 struct cramfs_inode
*inode
;
103 inode
= (struct cramfs_inode
*) (begin
+ offset
+
107 * Namelengths on disk are shifted by two
108 * and the name padded out to 4-byte boundaries
111 namelen
= CRAMFS_GET_NAMELEN (inode
) << 2;
112 name
= (char *) inode
+ sizeof (struct cramfs_inode
);
115 inodeoffset
+ sizeof (struct cramfs_inode
) + namelen
;
120 if (name
[namelen
- 1])
125 if (!strncmp (filename
, name
, namelen
)) {
126 char *p
= strtok (NULL
, "/");
128 if (raw
&& (p
== NULL
|| *p
== '\0'))
129 return offset
+ inodeoffset
;
131 if (S_ISDIR (CRAMFS_16 (inode
->mode
))) {
132 return cramfs_resolve (begin
,
138 } else if (S_ISREG (CRAMFS_16 (inode
->mode
))) {
139 return offset
+ inodeoffset
;
141 printf ("%*.*s: unsupported file type (%x)\n",
142 namelen
, namelen
, name
,
143 CRAMFS_16 (inode
->mode
));
148 inodeoffset
= nextoffset
;
151 printf ("can't find corresponding entry\n");
155 static int cramfs_uncompress (unsigned long begin
, unsigned long offset
,
156 unsigned long loadoffset
)
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
)) +
164 int size
, total_size
= 0;
167 cramfs_uncompress_init ();
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
]) -
178 curr_block
= CRAMFS_32 (block_ptrs
[i
]);
181 cramfs_uncompress_exit ();
185 int cramfs_load (char *loadoffset
, struct part_info
*info
, char *filename
)
187 unsigned long offset
;
189 if (cramfs_read_super (info
))
192 offset
= cramfs_resolve (PART_OFFSET(info
),
193 CRAMFS_GET_OFFSET (&(super
.root
)) << 2,
194 CRAMFS_24 (super
.root
.size
), 0,
195 strtok (filename
, "/"));
200 return cramfs_uncompress (PART_OFFSET(info
), offset
,
201 (unsigned long) loadoffset
);
204 static int cramfs_list_inode (struct part_info
*info
, unsigned long offset
)
206 struct cramfs_inode
*inode
= (struct cramfs_inode
*)
207 (PART_OFFSET(info
) + offset
);
209 int namelen
, nextoff
;
212 * Namelengths on disk are shifted by two
213 * and the name padded out to 4-byte boundaries
216 namelen
= CRAMFS_GET_NAMELEN (inode
) << 2;
217 name
= (char *) inode
+ sizeof (struct cramfs_inode
);
223 if (name
[namelen
- 1])
228 printf (" %s %8d %*.*s", mkmodestr (CRAMFS_16 (inode
->mode
), str
),
229 CRAMFS_24 (inode
->size
), namelen
, namelen
, name
);
231 if ((CRAMFS_16 (inode
->mode
) & S_IFMT
) == S_IFLNK
) {
233 * Unpack the link target, trusting in the inode's size field.
235 unsigned long size
= CRAMFS_24 (inode
->size
);
236 char *link
= malloc (size
);
238 if (link
!= NULL
&& cramfs_uncompress (PART_OFFSET(info
), offset
,
239 (unsigned long) link
)
241 printf (" -> %*.*s\n", (int) size
, (int) size
, link
);
243 printf (" [Error reading link]\n");
252 int cramfs_ls (struct part_info
*info
, char *filename
)
254 struct cramfs_inode
*inode
;
255 unsigned long inodeoffset
= 0, nextoffset
;
256 unsigned long offset
, size
;
258 if (cramfs_read_super (info
))
261 if (strlen (filename
) == 0 || !strcmp (filename
, "/")) {
262 /* Root directory. Use root inode in super block */
263 offset
= CRAMFS_GET_OFFSET (&(super
.root
)) << 2;
264 size
= CRAMFS_24 (super
.root
.size
);
266 /* Resolve the path */
267 offset
= cramfs_resolve (PART_OFFSET(info
),
268 CRAMFS_GET_OFFSET (&(super
.root
)) <<
269 2, CRAMFS_24 (super
.root
.size
), 1,
270 strtok (filename
, "/"));
275 /* Resolving was successful. Examine the inode */
276 inode
= (struct cramfs_inode
*) (PART_OFFSET(info
) + offset
);
277 if (!S_ISDIR (CRAMFS_16 (inode
->mode
))) {
278 /* It's not a directory - list it, and that's that */
279 return (cramfs_list_inode (info
, offset
) > 0);
282 /* It's a directory. List files within */
283 offset
= CRAMFS_GET_OFFSET (inode
) << 2;
284 size
= CRAMFS_24 (inode
->size
);
287 /* List the given directory */
288 while (inodeoffset
< size
) {
289 inode
= (struct cramfs_inode
*) (PART_OFFSET(info
) + offset
+
292 nextoffset
= cramfs_list_inode (info
, offset
+ inodeoffset
);
295 inodeoffset
+= sizeof (struct cramfs_inode
) + nextoffset
;
301 int cramfs_info (struct part_info
*info
)
303 if (cramfs_read_super (info
))
306 printf ("size: 0x%x (%u)\n", super
.size
, super
.size
);
308 if (super
.flags
!= 0) {
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");
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
);
327 int cramfs_check (struct part_info
*info
)
329 struct cramfs_super
*sb
;
331 if (info
->dev
->id
->type
!= MTD_DEV_TYPE_NOR
)
334 sb
= (struct cramfs_super
*) PART_OFFSET(info
);
335 if (sb
->magic
!= CRAMFS_32 (CRAMFS_MAGIC
)) {
336 /* check at 512 byte offset */
337 sb
= (struct cramfs_super
*) (PART_OFFSET(info
) + 512);
338 if (sb
->magic
!= CRAMFS_32 (CRAMFS_MAGIC
))