cramfs.c 9.9 KB

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