cramfs.c 8.8 KB

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