cramfs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * cramfs.c
  3. *
  4. * Copyright (C) 1999 Linus Torvalds
  5. *
  6. * Copyright (C) 2000-2002 Transmeta Corporation
  7. *
  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
  11. *
  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.
  15. *
  16. * Compressed ROM filesystem for Linux.
  17. *
  18. * TODO:
  19. * add support for resolving symbolic links
  20. */
  21. /*
  22. * These are the VFS interfaces to the compressed ROM filesystem.
  23. * The actual compression is based on zlib, see the other files.
  24. */
  25. #include <common.h>
  26. #include <malloc.h>
  27. #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
  28. #include <asm/byteorder.h>
  29. #include <linux/stat.h>
  30. #include <jffs2/jffs2.h>
  31. #include <jffs2/load_kernel.h>
  32. #include <cramfs/cramfs_fs.h>
  33. /* These two macros may change in future, to provide better st_ino
  34. semantics. */
  35. #define CRAMINO(x) (CRAMFS_GET_OFFSET(x) ? CRAMFS_GET_OFFSET(x)<<2 : 1)
  36. #define OFFSET(x) ((x)->i_ino)
  37. struct cramfs_super super;
  38. static int cramfs_read_super (struct part_info *info)
  39. {
  40. unsigned long root_offset;
  41. /* Read the first block and get the superblock from it */
  42. memcpy (&super, (void *) info->offset, sizeof (super));
  43. /* Do sanity checks on the superblock */
  44. if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
  45. /* check at 512 byte offset */
  46. memcpy (&super, (void *) info->offset + 512, sizeof (super));
  47. if (super.magic != CRAMFS_32 (CRAMFS_MAGIC)) {
  48. printf ("cramfs: wrong magic\n");
  49. return -1;
  50. }
  51. }
  52. /* flags is reused several times, so swab it once */
  53. super.flags = CRAMFS_32 (super.flags);
  54. super.size = CRAMFS_32 (super.size);
  55. /* get feature flags first */
  56. if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
  57. printf ("cramfs: unsupported filesystem features\n");
  58. return -1;
  59. }
  60. /* Check that the root inode is in a sane state */
  61. if (!S_ISDIR (CRAMFS_16 (super.root.mode))) {
  62. printf ("cramfs: root is not a directory\n");
  63. return -1;
  64. }
  65. root_offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
  66. if (root_offset == 0) {
  67. printf ("cramfs: empty filesystem");
  68. } else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
  69. ((root_offset != sizeof (struct cramfs_super)) &&
  70. (root_offset != 512 + sizeof (struct cramfs_super)))) {
  71. printf ("cramfs: bad root offset %lu\n", root_offset);
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static unsigned long cramfs_resolve (char *begin, unsigned long offset,
  77. unsigned long size, int raw,
  78. char *filename)
  79. {
  80. unsigned long inodeoffset = 0, nextoffset;
  81. while (inodeoffset < size) {
  82. struct cramfs_inode *inode;
  83. char *name;
  84. int namelen;
  85. inode = (struct cramfs_inode *) (begin + offset +
  86. inodeoffset);
  87. /*
  88. * Namelengths on disk are shifted by two
  89. * and the name padded out to 4-byte boundaries
  90. * with zeroes.
  91. */
  92. namelen = CRAMFS_GET_NAMELEN (inode) << 2;
  93. name = (char *) inode + sizeof (struct cramfs_inode);
  94. nextoffset =
  95. inodeoffset + sizeof (struct cramfs_inode) + namelen;
  96. for (;;) {
  97. if (!namelen)
  98. return -1;
  99. if (name[namelen - 1])
  100. break;
  101. namelen--;
  102. }
  103. if (!strncmp (filename, name, namelen)) {
  104. char *p = strtok (NULL, "/");
  105. if (raw && (p == NULL || *p == '\0'))
  106. return offset + inodeoffset;
  107. if (S_ISDIR (CRAMFS_16 (inode->mode))) {
  108. return cramfs_resolve (begin,
  109. CRAMFS_GET_OFFSET
  110. (inode) << 2,
  111. CRAMFS_24 (inode->
  112. size), raw,
  113. p);
  114. } else if (S_ISREG (CRAMFS_16 (inode->mode))) {
  115. return offset + inodeoffset;
  116. } else {
  117. printf ("%*.*s: unsupported file type (%x)\n",
  118. namelen, namelen, name,
  119. CRAMFS_16 (inode->mode));
  120. return 0;
  121. }
  122. }
  123. inodeoffset = nextoffset;
  124. }
  125. printf ("can't find corresponding entry\n");
  126. return 0;
  127. }
  128. static int cramfs_uncompress (char *begin, unsigned long offset,
  129. unsigned long loadoffset)
  130. {
  131. struct cramfs_inode *inode = (struct cramfs_inode *) (begin + offset);
  132. unsigned long *block_ptrs = (unsigned long *)
  133. (begin + (CRAMFS_GET_OFFSET (inode) << 2));
  134. unsigned long curr_block = (CRAMFS_GET_OFFSET (inode) +
  135. (((CRAMFS_24 (inode->size)) +
  136. 4095) >> 12)) << 2;
  137. int size, total_size = 0;
  138. int i;
  139. cramfs_uncompress_init ();
  140. for (i = 0; i < ((CRAMFS_24 (inode->size) + 4095) >> 12); i++) {
  141. size = cramfs_uncompress_block ((void *) loadoffset,
  142. (void *) (begin + curr_block),
  143. (CRAMFS_32 (block_ptrs[i]) -
  144. curr_block));
  145. if (size < 0)
  146. return size;
  147. loadoffset += size;
  148. total_size += size;
  149. curr_block = CRAMFS_32 (block_ptrs[i]);
  150. }
  151. cramfs_uncompress_exit ();
  152. return total_size;
  153. }
  154. int cramfs_load (char *loadoffset, struct part_info *info, char *filename)
  155. {
  156. unsigned long offset;
  157. if (cramfs_read_super (info))
  158. return -1;
  159. offset = cramfs_resolve (info->offset,
  160. CRAMFS_GET_OFFSET (&(super.root)) << 2,
  161. CRAMFS_24 (super.root.size), 0,
  162. strtok (filename, "/"));
  163. if (offset <= 0)
  164. return offset;
  165. return cramfs_uncompress (info->offset, offset,
  166. (unsigned long) loadoffset);
  167. }
  168. static int cramfs_list_inode (struct part_info *info, unsigned long offset)
  169. {
  170. struct cramfs_inode *inode = (struct cramfs_inode *)
  171. (info->offset + offset);
  172. char *name, str[20];
  173. int namelen, nextoff;
  174. /*
  175. * Namelengths on disk are shifted by two
  176. * and the name padded out to 4-byte boundaries
  177. * with zeroes.
  178. */
  179. namelen = CRAMFS_GET_NAMELEN (inode) << 2;
  180. name = (char *) inode + sizeof (struct cramfs_inode);
  181. nextoff = namelen;
  182. for (;;) {
  183. if (!namelen)
  184. return namelen;
  185. if (name[namelen - 1])
  186. break;
  187. namelen--;
  188. }
  189. printf (" %s %8d %*.*s", mkmodestr (CRAMFS_16 (inode->mode), str),
  190. CRAMFS_24 (inode->size), namelen, namelen, name);
  191. if ((CRAMFS_16 (inode->mode) & S_IFMT) == S_IFLNK) {
  192. /* symbolic link.
  193. * Unpack the link target, trusting in the inode's size field.
  194. */
  195. unsigned long size = CRAMFS_24 (inode->size);
  196. char *link = malloc (size);
  197. if (link != NULL && cramfs_uncompress (info->offset, offset,
  198. (unsigned long) link)
  199. == size)
  200. printf (" -> %*.*s\n", (int) size, (int) size, link);
  201. else
  202. printf (" [Error reading link]\n");
  203. if (link)
  204. free (link);
  205. } else
  206. printf ("\n");
  207. return nextoff;
  208. }
  209. int cramfs_ls (struct part_info *info, char *filename)
  210. {
  211. struct cramfs_inode *inode;
  212. unsigned long inodeoffset = 0, nextoffset;
  213. unsigned long offset, size;
  214. if (cramfs_read_super (info))
  215. return -1;
  216. if (strlen (filename) == 0 || !strcmp (filename, "/")) {
  217. /* Root directory. Use root inode in super block */
  218. offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
  219. size = CRAMFS_24 (super.root.size);
  220. } else {
  221. /* Resolve the path */
  222. offset = cramfs_resolve (info->offset,
  223. CRAMFS_GET_OFFSET (&(super.root)) <<
  224. 2, CRAMFS_24 (super.root.size), 1,
  225. strtok (filename, "/"));
  226. if (offset <= 0)
  227. return offset;
  228. /* Resolving was successful. Examine the inode */
  229. inode = (struct cramfs_inode *) (info->offset + offset);
  230. if (!S_ISDIR (CRAMFS_16 (inode->mode))) {
  231. /* It's not a directory - list it, and that's that */
  232. return (cramfs_list_inode (info, offset) > 0);
  233. }
  234. /* It's a directory. List files within */
  235. offset = CRAMFS_GET_OFFSET (inode) << 2;
  236. size = CRAMFS_24 (inode->size);
  237. }
  238. /* List the given directory */
  239. while (inodeoffset < size) {
  240. inode = (struct cramfs_inode *) (info->offset + offset +
  241. inodeoffset);
  242. nextoffset = cramfs_list_inode (info, offset + inodeoffset);
  243. if (nextoffset == 0)
  244. break;
  245. inodeoffset += sizeof (struct cramfs_inode) + nextoffset;
  246. }
  247. return 1;
  248. }
  249. int cramfs_info (struct part_info *info)
  250. {
  251. if (cramfs_read_super (info))
  252. return 0;
  253. printf ("size: 0x%x (%u)\n", super.size, super.size);
  254. if (super.flags != 0) {
  255. printf ("flags:\n");
  256. if (super.flags & CRAMFS_FLAG_FSID_VERSION_2)
  257. printf ("\tFSID version 2\n");
  258. if (super.flags & CRAMFS_FLAG_SORTED_DIRS)
  259. printf ("\tsorted dirs\n");
  260. if (super.flags & CRAMFS_FLAG_HOLES)
  261. printf ("\tholes\n");
  262. if (super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET)
  263. printf ("\tshifted root offset\n");
  264. }
  265. printf ("fsid:\n\tcrc: 0x%x\n\tedition: 0x%x\n",
  266. super.fsid.crc, super.fsid.edition);
  267. printf ("name: %16s\n", super.name);
  268. return 1;
  269. }
  270. int cramfs_check (struct part_info *info)
  271. {
  272. struct cramfs_super *sb = (struct cramfs_super *) info->offset;
  273. if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
  274. /* check at 512 byte offset */
  275. sb = (struct cramfs_super *) (info->offset + 512);
  276. if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
  277. return 0;
  278. }
  279. }
  280. return 1;
  281. }
  282. #endif /* CFG_FS_CRAMFS */