cramfs.c 9.2 KB

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