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 #include <asm/byteorder.h>
31 #include <linux/stat.h>
32 #include <jffs2/jffs2.h>
33 #include <jffs2/load_kernel.h>
34 #include <cramfs/cramfs_fs.h>
36 /* These two macros may change in future, to provide better st_ino
38 #define CRAMINO(x) (CRAMFS_GET_OFFSET(x) ? CRAMFS_GET_OFFSET(x)<<2 : 1)
39 #define OFFSET(x) ((x)->i_ino)
41 struct cramfs_super super;
43 /* CPU address space offset calculation macro, struct part_info offset is
44 * device address space offset, so we need to shift it by a device start address. */
45 #if defined(CONFIG_MTD_NOR_FLASH)
46 extern flash_info_t flash_info[];
47 #define PART_OFFSET(x) ((ulong)x->offset + \
48 flash_info[x->dev->id->num].start[0])
50 #define PART_OFFSET(x) ((ulong)x->offset)
53 static int cramfs_uncompress (unsigned long begin, unsigned long offset,
54 unsigned long loadoffset);
56 static int cramfs_read_super (struct part_info *info)
58 unsigned long root_offset;
60 /* Read the first block and get the superblock from it */
61 memcpy (&super, (void *) PART_OFFSET(info), sizeof (super));
63 /* Do sanity checks on the superblock */
64 if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
65 /* check at 512 byte offset */
66 memcpy (&super, (void *) PART_OFFSET(info) + 512, sizeof (super));
67 if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
68 printf ("cramfs: wrong magic\n");
73 /* flags is reused several times, so swab it once */
74 super.flags = CRAMFS_32 (super.flags);
75 super.size = CRAMFS_32 (super.size);
77 /* get feature flags first */
78 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
79 printf ("cramfs: unsupported filesystem features\n");
83 /* Check that the root inode is in a sane state */
84 if (!S_ISDIR (CRAMFS_16 (super.root.mode))) {
85 printf ("cramfs: root is not a directory\n");
88 root_offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
89 if (root_offset == 0) {
90 printf ("cramfs: empty filesystem");
91 } else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
92 ((root_offset != sizeof (struct cramfs_super)) &&
93 (root_offset != 512 + sizeof (struct cramfs_super)))) {
94 printf ("cramfs: bad root offset %lu\n", root_offset);
101 /* Unpack to an allocated buffer, trusting in the inode's size field. */
102 static char *cramfs_uncompress_link (unsigned long begin, unsigned long offset)
104 struct cramfs_inode *inode = (struct cramfs_inode *)(begin + offset);
105 unsigned long size = CRAMFS_24 (inode->size);
106 char *link = malloc (size + 1);
108 if (!link || cramfs_uncompress (begin, offset, (unsigned long)link) != size) {
117 static unsigned long cramfs_resolve (unsigned long begin, unsigned long offset,
118 unsigned long size, int raw,
121 unsigned long inodeoffset = 0, nextoffset;
123 while (inodeoffset < size) {
124 struct cramfs_inode *inode;
128 inode = (struct cramfs_inode *) (begin + offset +
132 * Namelengths on disk are shifted by two
133 * and the name padded out to 4-byte boundaries
136 namelen = CRAMFS_GET_NAMELEN (inode) << 2;
137 name = (char *) inode + sizeof (struct cramfs_inode);
140 inodeoffset + sizeof (struct cramfs_inode) + namelen;
145 if (name[namelen - 1])
150 if (!strncmp(filename, name, namelen) &&
151 (namelen == strlen(filename))) {
152 char *p = strtok (NULL, "/");
154 if (raw && (p == NULL || *p == '\0'))
155 return offset + inodeoffset;
157 if (S_ISDIR (CRAMFS_16 (inode->mode))) {
158 return cramfs_resolve (begin,
164 } else if (S_ISREG (CRAMFS_16 (inode->mode))) {
165 return offset + inodeoffset;
166 } else if (S_ISLNK (CRAMFS_16 (inode->mode))) {
169 if (p && strlen(p)) {
170 printf ("unsupported symlink to \
171 non-terminal path\n");
174 link = cramfs_uncompress_link (begin,
175 offset + inodeoffset);
177 printf ("%*.*s: Error reading link\n",
178 namelen, namelen, name);
180 } else if (link[0] == '/') {
181 printf ("unsupported symlink to \
186 ret = cramfs_resolve (begin,
194 printf ("%*.*s: unsupported file type (%x)\n",
195 namelen, namelen, name,
196 CRAMFS_16 (inode->mode));
201 inodeoffset = nextoffset;
204 printf ("can't find corresponding entry\n");
208 static int cramfs_uncompress (unsigned long begin, unsigned long offset,
209 unsigned long loadoffset)
211 struct cramfs_inode *inode = (struct cramfs_inode *) (begin + offset);
212 u32 *block_ptrs = (u32 *)
213 (begin + (CRAMFS_GET_OFFSET (inode) << 2));
214 unsigned long curr_block = (CRAMFS_GET_OFFSET (inode) +
215 (((CRAMFS_24 (inode->size)) +
217 int size, total_size = 0;
220 cramfs_uncompress_init ();
222 for (i = 0; i < ((CRAMFS_24 (inode->size) + 4095) >> 12); i++) {
223 size = cramfs_uncompress_block ((void *) loadoffset,
224 (void *) (begin + curr_block),
225 (CRAMFS_32 (block_ptrs[i]) -
231 curr_block = CRAMFS_32 (block_ptrs[i]);
234 cramfs_uncompress_exit ();
238 int cramfs_load (char *loadoffset, struct part_info *info, char *filename)
240 unsigned long offset;
242 if (cramfs_read_super (info))
245 offset = cramfs_resolve (PART_OFFSET(info),
246 CRAMFS_GET_OFFSET (&(super.root)) << 2,
247 CRAMFS_24 (super.root.size), 0,
248 strtok (filename, "/"));
253 return cramfs_uncompress (PART_OFFSET(info), offset,
254 (unsigned long) loadoffset);
257 static int cramfs_list_inode (struct part_info *info, unsigned long offset)
259 struct cramfs_inode *inode = (struct cramfs_inode *)
260 (PART_OFFSET(info) + offset);
262 int namelen, nextoff;
265 * Namelengths on disk are shifted by two
266 * and the name padded out to 4-byte boundaries
269 namelen = CRAMFS_GET_NAMELEN (inode) << 2;
270 name = (char *) inode + sizeof (struct cramfs_inode);
276 if (name[namelen - 1])
281 printf (" %s %8d %*.*s", mkmodestr (CRAMFS_16 (inode->mode), str),
282 CRAMFS_24 (inode->size), namelen, namelen, name);
284 if ((CRAMFS_16 (inode->mode) & S_IFMT) == S_IFLNK) {
285 char *link = cramfs_uncompress_link (PART_OFFSET(info), offset);
287 printf (" -> %s\n", link);
289 printf (" [Error reading link]\n");
297 int cramfs_ls (struct part_info *info, char *filename)
299 struct cramfs_inode *inode;
300 unsigned long inodeoffset = 0, nextoffset;
301 unsigned long offset, size;
303 if (cramfs_read_super (info))
306 if (strlen (filename) == 0 || !strcmp (filename, "/")) {
307 /* Root directory. Use root inode in super block */
308 offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
309 size = CRAMFS_24 (super.root.size);
311 /* Resolve the path */
312 offset = cramfs_resolve (PART_OFFSET(info),
313 CRAMFS_GET_OFFSET (&(super.root)) <<
314 2, CRAMFS_24 (super.root.size), 1,
315 strtok (filename, "/"));
320 /* Resolving was successful. Examine the inode */
321 inode = (struct cramfs_inode *) (PART_OFFSET(info) + offset);
322 if (!S_ISDIR (CRAMFS_16 (inode->mode))) {
323 /* It's not a directory - list it, and that's that */
324 return (cramfs_list_inode (info, offset) > 0);
327 /* It's a directory. List files within */
328 offset = CRAMFS_GET_OFFSET (inode) << 2;
329 size = CRAMFS_24 (inode->size);
332 /* List the given directory */
333 while (inodeoffset < size) {
334 inode = (struct cramfs_inode *) (PART_OFFSET(info) + offset +
337 nextoffset = cramfs_list_inode (info, offset + inodeoffset);
340 inodeoffset += sizeof (struct cramfs_inode) + nextoffset;
346 int cramfs_info (struct part_info *info)
348 if (cramfs_read_super (info))
351 printf ("size: 0x%x (%u)\n", super.size, super.size);
353 if (super.flags != 0) {
355 if (super.flags & CRAMFS_FLAG_FSID_VERSION_2)
356 printf ("\tFSID version 2\n");
357 if (super.flags & CRAMFS_FLAG_SORTED_DIRS)
358 printf ("\tsorted dirs\n");
359 if (super.flags & CRAMFS_FLAG_HOLES)
360 printf ("\tholes\n");
361 if (super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET)
362 printf ("\tshifted root offset\n");
365 printf ("fsid:\n\tcrc: 0x%x\n\tedition: 0x%x\n",
366 super.fsid.crc, super.fsid.edition);
367 printf ("name: %16s\n", super.name);
372 int cramfs_check (struct part_info *info)
374 struct cramfs_super *sb;
376 if (info->dev->id->type != MTD_DEV_TYPE_NOR)
379 sb = (struct cramfs_super *) PART_OFFSET(info);
380 if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
381 /* check at 512 byte offset */
382 sb = (struct cramfs_super *) (PART_OFFSET(info) + 512);
383 if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC))