cmd_cramfs.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License as
  4. * published by the Free Software Foundation; either version 2 of
  5. * the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  15. * MA 02111-1307 USA
  16. *
  17. * based on: cmd_jffs2.c
  18. *
  19. * Add support for a CRAMFS located in RAM
  20. */
  21. /*
  22. * CRAMFS support
  23. */
  24. #include <common.h>
  25. #include <command.h>
  26. #include <malloc.h>
  27. #include <linux/list.h>
  28. #include <linux/ctype.h>
  29. #include <jffs2/jffs2.h>
  30. #include <jffs2/load_kernel.h>
  31. #include <cramfs/cramfs_fs.h>
  32. /* enable/disable debugging messages */
  33. #define DEBUG_CRAMFS
  34. #undef DEBUG_CRAMFS
  35. #ifdef DEBUG_CRAMFS
  36. # define DEBUGF(fmt, args...) printf(fmt ,##args)
  37. #else
  38. # define DEBUGF(fmt, args...)
  39. #endif
  40. #ifdef CONFIG_CRAMFS_CMDLINE
  41. flash_info_t flash_info[1];
  42. #ifndef CONFIG_CMD_JFFS2
  43. #include <linux/stat.h>
  44. char *mkmodestr(unsigned long mode, char *str)
  45. {
  46. static const char *l = "xwr";
  47. int mask = 1, i;
  48. char c;
  49. switch (mode & S_IFMT) {
  50. case S_IFDIR: str[0] = 'd'; break;
  51. case S_IFBLK: str[0] = 'b'; break;
  52. case S_IFCHR: str[0] = 'c'; break;
  53. case S_IFIFO: str[0] = 'f'; break;
  54. case S_IFLNK: str[0] = 'l'; break;
  55. case S_IFSOCK: str[0] = 's'; break;
  56. case S_IFREG: str[0] = '-'; break;
  57. default: str[0] = '?';
  58. }
  59. for(i = 0; i < 9; i++) {
  60. c = l[i%3];
  61. str[9-i] = (mode & mask)?c:'-';
  62. mask = mask<<1;
  63. }
  64. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  65. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  66. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  67. str[10] = '\0';
  68. return str;
  69. }
  70. #endif /* CONFIG_CMD_JFFS2 */
  71. extern int cramfs_check (struct part_info *info);
  72. extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
  73. extern int cramfs_ls (struct part_info *info, char *filename);
  74. extern int cramfs_info (struct part_info *info);
  75. /***************************************************/
  76. /* U-boot commands */
  77. /***************************************************/
  78. /**
  79. * Routine implementing fsload u-boot command. This routine tries to load
  80. * a requested file from cramfs filesystem at location 'cramfsaddr'.
  81. * cramfsaddr is an evironment variable.
  82. *
  83. * @param cmdtp command internal data
  84. * @param flag command flag
  85. * @param argc number of arguments supplied to the command
  86. * @param argv arguments list
  87. * @return 0 on success, 1 otherwise
  88. */
  89. int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  90. {
  91. char *filename;
  92. int size;
  93. ulong offset = load_addr;
  94. struct part_info part;
  95. struct mtd_device dev;
  96. struct mtdids id;
  97. ulong addr;
  98. addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16);
  99. /* hack! */
  100. /* cramfs_* only supports NOR flash chips */
  101. /* fake the device type */
  102. id.type = MTD_DEV_TYPE_NOR;
  103. id.num = 0;
  104. dev.id = &id;
  105. part.dev = &dev;
  106. /* fake the address offset */
  107. part.offset = addr - flash_info[id.num].start[0];
  108. /* pre-set Boot file name */
  109. if ((filename = getenv("bootfile")) == NULL) {
  110. filename = "uImage";
  111. }
  112. if (argc == 2) {
  113. filename = argv[1];
  114. }
  115. if (argc == 3) {
  116. offset = simple_strtoul(argv[1], NULL, 0);
  117. load_addr = offset;
  118. filename = argv[2];
  119. }
  120. size = 0;
  121. if (cramfs_check(&part))
  122. size = cramfs_load ((char *) offset, &part, filename);
  123. if (size > 0) {
  124. char buf[10];
  125. printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
  126. size, offset);
  127. sprintf(buf, "%x", size);
  128. setenv("filesize", buf);
  129. } else {
  130. printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
  131. }
  132. return !(size > 0);
  133. }
  134. /**
  135. * Routine implementing u-boot ls command which lists content of a given
  136. * directory at location 'cramfsaddr'.
  137. * cramfsaddr is an evironment variable.
  138. *
  139. * @param cmdtp command internal data
  140. * @param flag command flag
  141. * @param argc number of arguments supplied to the command
  142. * @param argv arguments list
  143. * @return 0 on success, 1 otherwise
  144. */
  145. int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  146. {
  147. char *filename = "/";
  148. int ret;
  149. struct part_info part;
  150. struct mtd_device dev;
  151. struct mtdids id;
  152. ulong addr;
  153. addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16);
  154. /* hack! */
  155. /* cramfs_* only supports NOR flash chips */
  156. /* fake the device type */
  157. id.type = MTD_DEV_TYPE_NOR;
  158. id.num = 0;
  159. dev.id = &id;
  160. part.dev = &dev;
  161. /* fake the address offset */
  162. part.offset = addr - flash_info[id.num].start[0];
  163. if (argc == 2)
  164. filename = argv[1];
  165. ret = 0;
  166. if (cramfs_check(&part))
  167. ret = cramfs_ls (&part, filename);
  168. return ret ? 0 : 1;
  169. }
  170. /* command line only */
  171. /***************************************************/
  172. U_BOOT_CMD(
  173. cramfsload, 3, 0, do_cramfs_load,
  174. "cramfsload\t- load binary file from a filesystem image",
  175. "[ off ] [ filename ]\n"
  176. " - load binary file from address 'cramfsaddr'\n"
  177. " with offset 'off'\n"
  178. );
  179. U_BOOT_CMD(
  180. cramfsls, 2, 1, do_cramfs_ls,
  181. "cramfsls\t- list files in a directory (default /)",
  182. "[ directory ]\n"
  183. " - list files in a directory.\n"
  184. );
  185. #endif /* #ifdef CONFIG_CRAMFS_CMDLINE */
  186. /***************************************************/