cramfs.c 9.9 KB

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