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. #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. (namelen == strlen(filename))) {
  112. char *p = strtok (NULL, "/");
  113. if (raw && (p == NULL || *p == '\0'))
  114. return offset + inodeoffset;
  115. if (S_ISDIR (CRAMFS_16 (inode->mode))) {
  116. return cramfs_resolve (begin,
  117. CRAMFS_GET_OFFSET
  118. (inode) << 2,
  119. CRAMFS_24 (inode->
  120. size), raw,
  121. p);
  122. } else if (S_ISREG (CRAMFS_16 (inode->mode))) {
  123. return offset + inodeoffset;
  124. } else {
  125. printf ("%*.*s: unsupported file type (%x)\n",
  126. namelen, namelen, name,
  127. CRAMFS_16 (inode->mode));
  128. return 0;
  129. }
  130. }
  131. inodeoffset = nextoffset;
  132. }
  133. printf ("can't find corresponding entry\n");
  134. return 0;
  135. }
  136. static int cramfs_uncompress (unsigned long begin, unsigned long offset,
  137. unsigned long loadoffset)
  138. {
  139. struct cramfs_inode *inode = (struct cramfs_inode *) (begin + offset);
  140. unsigned long *block_ptrs = (unsigned long *)
  141. (begin + (CRAMFS_GET_OFFSET (inode) << 2));
  142. unsigned long curr_block = (CRAMFS_GET_OFFSET (inode) +
  143. (((CRAMFS_24 (inode->size)) +
  144. 4095) >> 12)) << 2;
  145. int size, total_size = 0;
  146. int i;
  147. cramfs_uncompress_init ();
  148. for (i = 0; i < ((CRAMFS_24 (inode->size) + 4095) >> 12); i++) {
  149. size = cramfs_uncompress_block ((void *) loadoffset,
  150. (void *) (begin + curr_block),
  151. (CRAMFS_32 (block_ptrs[i]) -
  152. curr_block));
  153. if (size < 0)
  154. return size;
  155. loadoffset += size;
  156. total_size += size;
  157. curr_block = CRAMFS_32 (block_ptrs[i]);
  158. }
  159. cramfs_uncompress_exit ();
  160. return total_size;
  161. }
  162. int cramfs_load (char *loadoffset, struct part_info *info, char *filename)
  163. {
  164. unsigned long offset;
  165. if (cramfs_read_super (info))
  166. return -1;
  167. offset = cramfs_resolve (PART_OFFSET(info),
  168. CRAMFS_GET_OFFSET (&(super.root)) << 2,
  169. CRAMFS_24 (super.root.size), 0,
  170. strtok (filename, "/"));
  171. if (offset <= 0)
  172. return offset;
  173. return cramfs_uncompress (PART_OFFSET(info), offset,
  174. (unsigned long) loadoffset);
  175. }
  176. static int cramfs_list_inode (struct part_info *info, unsigned long offset)
  177. {
  178. struct cramfs_inode *inode = (struct cramfs_inode *)
  179. (PART_OFFSET(info) + offset);
  180. char *name, str[20];
  181. int namelen, nextoff;
  182. /*
  183. * Namelengths on disk are shifted by two
  184. * and the name padded out to 4-byte boundaries
  185. * with zeroes.
  186. */
  187. namelen = CRAMFS_GET_NAMELEN (inode) << 2;
  188. name = (char *) inode + sizeof (struct cramfs_inode);
  189. nextoff = namelen;
  190. for (;;) {
  191. if (!namelen)
  192. return namelen;
  193. if (name[namelen - 1])
  194. break;
  195. namelen--;
  196. }
  197. printf (" %s %8d %*.*s", mkmodestr (CRAMFS_16 (inode->mode), str),
  198. CRAMFS_24 (inode->size), namelen, namelen, name);
  199. if ((CRAMFS_16 (inode->mode) & S_IFMT) == S_IFLNK) {
  200. /* symbolic link.
  201. * Unpack the link target, trusting in the inode's size field.
  202. */
  203. unsigned long size = CRAMFS_24 (inode->size);
  204. char *link = malloc (size);
  205. if (link != NULL && cramfs_uncompress (PART_OFFSET(info), offset,
  206. (unsigned long) link)
  207. == size)
  208. printf (" -> %*.*s\n", (int) size, (int) size, link);
  209. else
  210. printf (" [Error reading link]\n");
  211. if (link)
  212. free (link);
  213. } else
  214. printf ("\n");
  215. return nextoff;
  216. }
  217. int cramfs_ls (struct part_info *info, char *filename)
  218. {
  219. struct cramfs_inode *inode;
  220. unsigned long inodeoffset = 0, nextoffset;
  221. unsigned long offset, size;
  222. if (cramfs_read_super (info))
  223. return -1;
  224. if (strlen (filename) == 0 || !strcmp (filename, "/")) {
  225. /* Root directory. Use root inode in super block */
  226. offset = CRAMFS_GET_OFFSET (&(super.root)) << 2;
  227. size = CRAMFS_24 (super.root.size);
  228. } else {
  229. /* Resolve the path */
  230. offset = cramfs_resolve (PART_OFFSET(info),
  231. CRAMFS_GET_OFFSET (&(super.root)) <<
  232. 2, CRAMFS_24 (super.root.size), 1,
  233. strtok (filename, "/"));
  234. if (offset <= 0)
  235. return offset;
  236. /* Resolving was successful. Examine the inode */
  237. inode = (struct cramfs_inode *) (PART_OFFSET(info) + offset);
  238. if (!S_ISDIR (CRAMFS_16 (inode->mode))) {
  239. /* It's not a directory - list it, and that's that */
  240. return (cramfs_list_inode (info, offset) > 0);
  241. }
  242. /* It's a directory. List files within */
  243. offset = CRAMFS_GET_OFFSET (inode) << 2;
  244. size = CRAMFS_24 (inode->size);
  245. }
  246. /* List the given directory */
  247. while (inodeoffset < size) {
  248. inode = (struct cramfs_inode *) (PART_OFFSET(info) + offset +
  249. inodeoffset);
  250. nextoffset = cramfs_list_inode (info, offset + inodeoffset);
  251. if (nextoffset == 0)
  252. break;
  253. inodeoffset += sizeof (struct cramfs_inode) + nextoffset;
  254. }
  255. return 1;
  256. }
  257. int cramfs_info (struct part_info *info)
  258. {
  259. if (cramfs_read_super (info))
  260. return 0;
  261. printf ("size: 0x%x (%u)\n", super.size, super.size);
  262. if (super.flags != 0) {
  263. printf ("flags:\n");
  264. if (super.flags & CRAMFS_FLAG_FSID_VERSION_2)
  265. printf ("\tFSID version 2\n");
  266. if (super.flags & CRAMFS_FLAG_SORTED_DIRS)
  267. printf ("\tsorted dirs\n");
  268. if (super.flags & CRAMFS_FLAG_HOLES)
  269. printf ("\tholes\n");
  270. if (super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET)
  271. printf ("\tshifted root offset\n");
  272. }
  273. printf ("fsid:\n\tcrc: 0x%x\n\tedition: 0x%x\n",
  274. super.fsid.crc, super.fsid.edition);
  275. printf ("name: %16s\n", super.name);
  276. return 1;
  277. }
  278. int cramfs_check (struct part_info *info)
  279. {
  280. struct cramfs_super *sb;
  281. if (info->dev->id->type != MTD_DEV_TYPE_NOR)
  282. return 0;
  283. sb = (struct cramfs_super *) PART_OFFSET(info);
  284. if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
  285. /* check at 512 byte offset */
  286. sb = (struct cramfs_super *) (PART_OFFSET(info) + 512);
  287. if (sb->magic != CRAMFS_32 (CRAMFS_MAGIC))
  288. return 0;
  289. }
  290. return 1;
  291. }