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.
30 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
32 #include <asm/byteorder.h>
33 #include <linux/stat.h>
34 #include <jffs2/load_kernel.h>
35 #include "cramfs_fs.h"
37 /* These two macros may change in future, to provide better st_ino
39 #define CRAMINO(x) (CRAMFS_GET_OFFSET(x) ? CRAMFS_GET_OFFSET(x)<<2 : 1)
40 #define OFFSET(x) ((x)->i_ino)
42 struct cramfs_super super;
44 static int cramfs_read_super (struct part_info *info)
46 unsigned long root_offset;
48 /* Read the first block and get the superblock from it */
49 memcpy (&super, (void *) info->offset, sizeof (super));
51 /* Do sanity checks on the superblock */
52 if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
53 /* check at 512 byte offset */
54 memcpy (&super, (void *) info->offset + 512, sizeof (super));
55 if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
56 printf ("cramfs: wrong magic\n");
61 /* flags is reused several times, so swab it once */
62 super.flags = CRAMFS_32 (super.flags);
63 super.size = CRAMFS_32 (super.size);
65 /* get feature flags first */
66 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
67 printf ("cramfs: unsupported filesystem features\n");
71 /* Check that the root inode is in a sane state */
72 if (!S_ISDIR (CRAMFS_16 (super.root.mode))) {
73 printf ("cramfs: root is not a directory\n");
76 root_offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
77 if (root_offset == 0) {
78 printf ("cramfs: empty filesystem");
79 } else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
80 ((root_offset != sizeof (struct cramfs_super)) &&
81 (root_offset != 512 + sizeof (struct cramfs_super)))) {
82 printf ("cramfs: bad root offset %lu\n", root_offset);
89 static unsigned long cramfs_resolve (char *begin, unsigned long offset,
90 unsigned long size, int raw,
93 unsigned long inodeoffset = 0, nextoffset;
95 while (inodeoffset < size) {
96 struct cramfs_inode *inode;
100 inode = (struct cramfs_inode *) (begin + offset +
104 * Namelengths on disk are shifted by two
105 * and the name padded out to 4-byte boundaries
108 namelen = CRAMFS_GET_NAMELEN (inode) << 2;
109 name = (char *) inode + sizeof (struct cramfs_inode);
112 inodeoffset + sizeof (struct cramfs_inode) + namelen;
117 if (name[namelen - 1])
122 if (!strncmp (filename, name, namelen)) {
123 char *p = strtok (NULL, "/");
125 if (raw && (p == NULL || *p == '\0'))
126 return offset + inodeoffset;
128 if (S_ISDIR (CRAMFS_16 (inode->mode))) {
129 return cramfs_resolve (begin,
135 } else if (S_ISREG (CRAMFS_16 (inode->mode))) {
136 return offset + inodeoffset;
138 printf ("%*.*s: unsupported file type (%x)\n",
139 namelen, namelen, name,
140 CRAMFS_16 (inode->mode));
145 inodeoffset = nextoffset;
148 printf ("can't find corresponding entry\n");
152 static int cramfs_uncompress (char *begin, unsigned long offset,
153 unsigned long loadoffset)
155 struct cramfs_inode *inode = (struct cramfs_inode *) (begin + offset);
156 unsigned long *block_ptrs = (unsigned long *)
157 (begin + (CRAMFS_GET_OFFSET (inode) << 2));
158 unsigned long curr_block = (CRAMFS_GET_OFFSET (inode) +
159 (((CRAMFS_24 (inode->size)) +
161 int size, total_size = 0;
164 cramfs_uncompress_init ();
166 for (i = 0; i < ((CRAMFS_24 (inode->size) + 4095) >> 12); i++) {
167 size = cramfs_uncompress_block ((void *) loadoffset,
168 (void *) (begin + curr_block),
169 (CRAMFS_32 (block_ptrs[i]) -
175 curr_block = CRAMFS_32 (block_ptrs[i]);
178 cramfs_uncompress_exit ();
182 int cramfs_load (char *loadoffset, struct part_info *info, char *filename)
184 unsigned long offset;
186 if (cramfs_read_super (info))
189 offset = cramfs_resolve (info->offset,
190 CRAMFS_GET_OFFSET (&(super.root)) << 2,
191 CRAMFS_24 (super.root.size), 0,
192 strtok (filename, "/"));
197 return cramfs_uncompress (info->offset, offset,
198 (unsigned long) loadoffset);
201 static char *mkmodestr (unsigned long mode, char *str)
203 static const char *l = "xwr";
207 switch (mode & S_IFMT) {
208 case S_IFDIR: str[0] = 'd'; break;
209 case S_IFBLK: str[0] = 'b'; break;
210 case S_IFCHR: str[0] = 'c'; break;
211 case S_IFIFO: str[0] = 'f'; break;
212 case S_IFLNK: str[0] = 'l'; break;
213 case S_IFSOCK: str[0] = 's'; break;
214 case S_IFREG: str[0] = '-'; break;
215 default: str[0] = '?'; break;
218 for (i = 0; i < 9; i++) {
220 str[9 - i] = (mode & mask) ? c : '-';
224 if (mode & S_ISUID) str[3] = (mode & S_IXUSR) ? 's' : 'S';
225 if (mode & S_ISGID) str[6] = (mode & S_IXGRP) ? 's' : 'S';
226 if (mode & S_ISVTX) str[9] = (mode & S_IXOTH) ? 't' : 'T';
231 static int cramfs_list_inode (struct part_info *info, unsigned long offset)
233 struct cramfs_inode *inode = (struct cramfs_inode *)
234 (info->offset + offset);
236 int namelen, nextoff;
239 * Namelengths on disk are shifted by two
240 * and the name padded out to 4-byte boundaries
243 namelen = CRAMFS_GET_NAMELEN (inode) << 2;
244 name = (char *) inode + sizeof (struct cramfs_inode);
250 if (name[namelen - 1])
255 printf (" %s %8d %*.*s", mkmodestr (CRAMFS_16 (inode->mode), str),
256 CRAMFS_24 (inode->size), namelen, namelen, name);
258 if ((CRAMFS_16 (inode->mode) & S_IFMT) == S_IFLNK) {
260 * Unpack the link target, trusting in the inode's size field.
262 unsigned long size = CRAMFS_24 (inode->size);
263 char *link = malloc (size);
265 if (link != NULL && cramfs_uncompress (info->offset, offset,
266 (unsigned long) link)
268 printf (" -> %*.*s\n", (int) size, (int) size, link);
270 printf (" [Error reading link]\n");
279 int cramfs_ls (struct part_info *info, char *filename)
281 struct cramfs_inode *inode;
282 unsigned long inodeoffset = 0, nextoffset;
283 unsigned long offset, size;
285 if (cramfs_read_super (info))
288 if (strlen (filename) == 0 || !strcmp (filename, "/")) {
289 /* Root directory. Use root inode in super block */
290 offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
291 size = CRAMFS_24 (super.root.size);
293 /* Resolve the path */
294 offset = cramfs_resolve (info->offset,
295 CRAMFS_GET_OFFSET (&(super.root)) <<
296 2, CRAMFS_24 (super.root.size), 1,
297 strtok (filename, "/"));
302 /* Resolving was successful. Examine the inode */
303 inode = (struct cramfs_inode *) (info->offset + offset);
304 if (!S_ISDIR (CRAMFS_16 (inode->mode))) {
305 /* It's not a directory - list it, and that's that */
306 return (cramfs_list_inode (info, offset) > 0);
309 /* It's a directory. List files within */
310 offset = CRAMFS_GET_OFFSET (inode) << 2;
311 size = CRAMFS_24 (inode->size);
314 /* List the given directory */
315 while (inodeoffset < size) {
316 inode = (struct cramfs_inode *) (info->offset + offset +
319 nextoffset = cramfs_list_inode (info, offset + inodeoffset);
322 inodeoffset += sizeof (struct cramfs_inode) + nextoffset;
328 int cramfs_info (struct part_info *info)
330 if (cramfs_read_super (info))
333 printf ("size: 0x%x (%u)\n", super.size, super.size);
335 if (super.flags != 0) {
337 if (super.flags & CRAMFS_FLAG_FSID_VERSION_2)
338 printf ("\tFSID version 2\n");
339 if (super.flags & CRAMFS_FLAG_SORTED_DIRS)
340 printf ("\tsorted dirs\n");
341 if (super.flags & CRAMFS_FLAG_HOLES)
342 printf ("\tholes\n");
343 if (super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET)
344 printf ("\tshifted root offset\n");
347 printf ("fsid:\n\tcrc: 0x%x\n\tedition: 0x%x\n",
348 super.fsid.crc, super.fsid.edition);
349 printf ("name: %16s\n", super.name);
354 int cramfs_check (struct part_info *info)
356 struct cramfs_super *sb = (struct cramfs_super *) info->offset;
358 if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
359 /* check at 512 byte offset */
360 sb = (struct cramfs_super *) (info->offset + 512);
361 if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
368 #endif /* CFG_FS_CRAMFS */