cmd_cramfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #include <flash.h>
  42. #ifdef CONFIG_SYS_NO_FLASH
  43. # define OFFSET_ADJUSTMENT 0
  44. #else
  45. # define OFFSET_ADJUSTMENT (flash_info[id.num].start[0])
  46. #endif
  47. #ifndef CONFIG_CMD_JFFS2
  48. #include <linux/stat.h>
  49. char *mkmodestr(unsigned long mode, char *str)
  50. {
  51. static const char *l = "xwr";
  52. int mask = 1, i;
  53. char c;
  54. switch (mode & S_IFMT) {
  55. case S_IFDIR: str[0] = 'd'; break;
  56. case S_IFBLK: str[0] = 'b'; break;
  57. case S_IFCHR: str[0] = 'c'; break;
  58. case S_IFIFO: str[0] = 'f'; break;
  59. case S_IFLNK: str[0] = 'l'; break;
  60. case S_IFSOCK: str[0] = 's'; break;
  61. case S_IFREG: str[0] = '-'; break;
  62. default: str[0] = '?';
  63. }
  64. for(i = 0; i < 9; i++) {
  65. c = l[i%3];
  66. str[9-i] = (mode & mask)?c:'-';
  67. mask = mask<<1;
  68. }
  69. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  70. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  71. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  72. str[10] = '\0';
  73. return str;
  74. }
  75. #endif /* CONFIG_CMD_JFFS2 */
  76. extern int cramfs_check (struct part_info *info);
  77. extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
  78. extern int cramfs_ls (struct part_info *info, char *filename);
  79. extern int cramfs_info (struct part_info *info);
  80. /***************************************************/
  81. /* U-boot commands */
  82. /***************************************************/
  83. /**
  84. * Routine implementing fsload u-boot command. This routine tries to load
  85. * a requested file from cramfs filesystem at location 'cramfsaddr'.
  86. * cramfsaddr is an evironment variable.
  87. *
  88. * @param cmdtp command internal data
  89. * @param flag command flag
  90. * @param argc number of arguments supplied to the command
  91. * @param argv arguments list
  92. * @return 0 on success, 1 otherwise
  93. */
  94. int do_cramfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  95. {
  96. char *filename;
  97. int size;
  98. ulong offset = load_addr;
  99. struct part_info part;
  100. struct mtd_device dev;
  101. struct mtdids id;
  102. ulong addr;
  103. addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16);
  104. /* hack! */
  105. /* cramfs_* only supports NOR flash chips */
  106. /* fake the device type */
  107. id.type = MTD_DEV_TYPE_NOR;
  108. id.num = 0;
  109. dev.id = &id;
  110. part.dev = &dev;
  111. /* fake the address offset */
  112. part.offset = addr - OFFSET_ADJUSTMENT;
  113. /* pre-set Boot file name */
  114. if ((filename = getenv("bootfile")) == NULL) {
  115. filename = "uImage";
  116. }
  117. if (argc == 2) {
  118. filename = argv[1];
  119. }
  120. if (argc == 3) {
  121. offset = simple_strtoul(argv[1], NULL, 0);
  122. load_addr = offset;
  123. filename = argv[2];
  124. }
  125. size = 0;
  126. if (cramfs_check(&part))
  127. size = cramfs_load ((char *) offset, &part, filename);
  128. if (size > 0) {
  129. printf("### CRAMFS load complete: %d bytes loaded to 0x%lx\n",
  130. size, offset);
  131. setenv_hex("filesize", size);
  132. } else {
  133. printf("### CRAMFS LOAD ERROR<%x> for %s!\n", size, filename);
  134. }
  135. return !(size > 0);
  136. }
  137. /**
  138. * Routine implementing u-boot ls command which lists content of a given
  139. * directory at location 'cramfsaddr'.
  140. * cramfsaddr is an evironment variable.
  141. *
  142. * @param cmdtp command internal data
  143. * @param flag command flag
  144. * @param argc number of arguments supplied to the command
  145. * @param argv arguments list
  146. * @return 0 on success, 1 otherwise
  147. */
  148. int do_cramfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  149. {
  150. char *filename = "/";
  151. int ret;
  152. struct part_info part;
  153. struct mtd_device dev;
  154. struct mtdids id;
  155. ulong addr;
  156. addr = simple_strtoul(getenv("cramfsaddr"), NULL, 16);
  157. /* hack! */
  158. /* cramfs_* only supports NOR flash chips */
  159. /* fake the device type */
  160. id.type = MTD_DEV_TYPE_NOR;
  161. id.num = 0;
  162. dev.id = &id;
  163. part.dev = &dev;
  164. /* fake the address offset */
  165. part.offset = addr - OFFSET_ADJUSTMENT;
  166. if (argc == 2)
  167. filename = argv[1];
  168. ret = 0;
  169. if (cramfs_check(&part))
  170. ret = cramfs_ls (&part, filename);
  171. return ret ? 0 : 1;
  172. }
  173. /* command line only */
  174. /***************************************************/
  175. U_BOOT_CMD(
  176. cramfsload, 3, 0, do_cramfs_load,
  177. "load binary file from a filesystem image",
  178. "[ off ] [ filename ]\n"
  179. " - load binary file from address 'cramfsaddr'\n"
  180. " with offset 'off'\n"
  181. );
  182. U_BOOT_CMD(
  183. cramfsls, 2, 1, do_cramfs_ls,
  184. "list files in a directory (default /)",
  185. "[ directory ]\n"
  186. " - list files in a directory.\n"
  187. );
  188. #endif /* #ifdef CONFIG_CRAMFS_CMDLINE */
  189. /***************************************************/