cramfs.c 8.9 KB

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