cramfs.c 8.9 KB

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