cramfs.c 8.9 KB

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