4 * Copyright (C) 1999 Linus Torvalds
6 * Copyright (C) 2000-2002 Transmeta Corporation
8 * Copyright (C) 2003 Kai-Uwe Bloem,
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 #if !defined(CONFIG_SYS_NO_FLASH)
45 extern flash_info_t flash_info[];
46 #define PART_OFFSET(x) (x->offset + flash_info[x->dev->id->num].start[0])
48 #define PART_OFFSET(x) (x->offset)
51 static int cramfs_read_super (struct part_info *info)
53 unsigned long root_offset;
55 /* Read the first block and get the superblock from it */
56 memcpy (&super, (void *) PART_OFFSET(info), sizeof (super));
58 /* Do sanity checks on the superblock */
59 if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
60 /* check at 512 byte offset */
61 memcpy (&super, (void *) PART_OFFSET(info) + 512, sizeof (super));
62 if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
63 printf ("cramfs: wrong magic\n");
68 /* flags is reused several times, so swab it once */
69 super.flags = CRAMFS_32 (super.flags);
70 super.size = CRAMFS_32 (super.size);
72 /* get feature flags first */
73 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
74 printf ("cramfs: unsupported filesystem features\n");
78 /* Check that the root inode is in a sane state */
79 if (!S_ISDIR (CRAMFS_16 (super.root.mode))) {
80 printf ("cramfs: root is not a directory\n");
83 root_offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
84 if (root_offset == 0) {
85 printf ("cramfs: empty filesystem");
86 } else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
87 ((root_offset != sizeof (struct cramfs_super)) &&
88 (root_offset != 512 + sizeof (struct cramfs_super)))) {
89 printf ("cramfs: bad root offset %lu\n", root_offset);
96 static unsigned long cramfs_resolve (unsigned long begin, unsigned long offset,
97 unsigned long size, int raw,
100 unsigned long inodeoffset = 0, nextoffset;
102 while (inodeoffset < size) {
103 struct cramfs_inode *inode;
107 inode = (struct cramfs_inode *) (begin + offset +
111 * Namelengths on disk are shifted by two
112 * and the name padded out to 4-byte boundaries
115 namelen = CRAMFS_GET_NAMELEN (inode) << 2;
116 name = (char *) inode + sizeof (struct cramfs_inode);
119 inodeoffset + sizeof (struct cramfs_inode) + namelen;
124 if (name[namelen - 1])
129 if (!strncmp(filename, name, namelen) &&
130 (namelen == strlen(filename))) {
131 char *p = strtok (NULL, "/");
133 if (raw && (p == NULL || *p == '\0'))
134 return offset + inodeoffset;
136 if (S_ISDIR (CRAMFS_16 (inode->mode))) {
137 return cramfs_resolve (begin,
143 } else if (S_ISREG (CRAMFS_16 (inode->mode))) {
144 return offset + inodeoffset;
146 printf ("%*.*s: unsupported file type (%x)\n",
147 namelen, namelen, name,
148 CRAMFS_16 (inode->mode));
153 inodeoffset = nextoffset;
156 printf ("can't find corresponding entry\n");
160 static int cramfs_uncompress (unsigned long begin, unsigned long offset,
161 unsigned long loadoffset)
163 struct cramfs_inode *inode = (struct cramfs_inode *) (begin + offset);
164 unsigned long *block_ptrs = (unsigned long *)
165 (begin + (CRAMFS_GET_OFFSET (inode) << 2));
166 unsigned long curr_block = (CRAMFS_GET_OFFSET (inode) +
167 (((CRAMFS_24 (inode->size)) +
169 int size, total_size = 0;
172 cramfs_uncompress_init ();
174 for (i = 0; i < ((CRAMFS_24 (inode->size) + 4095) >> 12); i++) {
175 size = cramfs_uncompress_block ((void *) loadoffset,
176 (void *) (begin + curr_block),
177 (CRAMFS_32 (block_ptrs[i]) -
183 curr_block = CRAMFS_32 (block_ptrs[i]);
186 cramfs_uncompress_exit ();
190 int cramfs_load (char *loadoffset, struct part_info *info, char *filename)
192 unsigned long offset;
194 if (cramfs_read_super (info))
197 offset = cramfs_resolve (PART_OFFSET(info),
198 CRAMFS_GET_OFFSET (&(super.root)) << 2,
199 CRAMFS_24 (super.root.size), 0,
200 strtok (filename, "/"));
205 return cramfs_uncompress (PART_OFFSET(info), offset,
206 (unsigned long) loadoffset);
209 static int cramfs_list_inode (struct part_info *info, unsigned long offset)
211 struct cramfs_inode *inode = (struct cramfs_inode *)
212 (PART_OFFSET(info) + offset);
214 int namelen, nextoff;
217 * Namelengths on disk are shifted by two
218 * and the name padded out to 4-byte boundaries
221 namelen = CRAMFS_GET_NAMELEN (inode) << 2;
222 name = (char *) inode + sizeof (struct cramfs_inode);
228 if (name[namelen - 1])
233 printf (" %s %8d %*.*s", mkmodestr (CRAMFS_16 (inode->mode), str),
234 CRAMFS_24 (inode->size), namelen, namelen, name);
236 if ((CRAMFS_16 (inode->mode) & S_IFMT) == S_IFLNK) {
238 * Unpack the link target, trusting in the inode's size field.
240 unsigned long size = CRAMFS_24 (inode->size);
241 char *link = malloc (size);
243 if (link != NULL && cramfs_uncompress (PART_OFFSET(info), offset,
244 (unsigned long) link)
246 printf (" -> %*.*s\n", (int) size, (int) size, link);
248 printf (" [Error reading link]\n");
257 int cramfs_ls (struct part_info *info, char *filename)
259 struct cramfs_inode *inode;
260 unsigned long inodeoffset = 0, nextoffset;
261 unsigned long offset, size;
263 if (cramfs_read_super (info))
266 if (strlen (filename) == 0 || !strcmp (filename, "/")) {
267 /* Root directory. Use root inode in super block */
268 offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
269 size = CRAMFS_24 (super.root.size);
271 /* Resolve the path */
272 offset = cramfs_resolve (PART_OFFSET(info),
273 CRAMFS_GET_OFFSET (&(super.root)) <<
274 2, CRAMFS_24 (super.root.size), 1,
275 strtok (filename, "/"));
280 /* Resolving was successful. Examine the inode */
281 inode = (struct cramfs_inode *) (PART_OFFSET(info) + offset);
282 if (!S_ISDIR (CRAMFS_16 (inode->mode))) {
283 /* It's not a directory - list it, and that's that */
284 return (cramfs_list_inode (info, offset) > 0);
287 /* It's a directory. List files within */
288 offset = CRAMFS_GET_OFFSET (inode) << 2;
289 size = CRAMFS_24 (inode->size);
292 /* List the given directory */
293 while (inodeoffset < size) {
294 inode = (struct cramfs_inode *) (PART_OFFSET(info) + offset +
297 nextoffset = cramfs_list_inode (info, offset + inodeoffset);
300 inodeoffset += sizeof (struct cramfs_inode) + nextoffset;
306 int cramfs_info (struct part_info *info)
308 if (cramfs_read_super (info))
311 printf ("size: 0x%x (%u)\n", super.size, super.size);
313 if (super.flags != 0) {
315 if (super.flags & CRAMFS_FLAG_FSID_VERSION_2)
316 printf ("\tFSID version 2\n");
317 if (super.flags & CRAMFS_FLAG_SORTED_DIRS)
318 printf ("\tsorted dirs\n");
319 if (super.flags & CRAMFS_FLAG_HOLES)
320 printf ("\tholes\n");
321 if (super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET)
322 printf ("\tshifted root offset\n");
325 printf ("fsid:\n\tcrc: 0x%x\n\tedition: 0x%x\n",
326 super.fsid.crc, super.fsid.edition);
327 printf ("name: %16s\n", super.name);
332 int cramfs_check (struct part_info *info)
334 struct cramfs_super *sb;
336 if (info->dev->id->type != MTD_DEV_TYPE_NOR)
339 sb = (struct cramfs_super *) PART_OFFSET(info);
340 if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
341 /* check at 512 byte offset */
342 sb = (struct cramfs_super *) (PART_OFFSET(info) + 512);
343 if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC))