cmd_jffs2.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. * (C) Copyright 2002
  5. * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
  6. * (C) Copyright 2003
  7. * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /*
  28. * Boot support
  29. */
  30. #include <common.h>
  31. #include <command.h>
  32. #include <s_record.h>
  33. #include <jffs2/load_kernel.h>
  34. #include <net.h>
  35. #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
  36. #include <cramfs/cramfs_fs.h>
  37. extern int cramfs_check (struct part_info *info);
  38. extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
  39. extern int cramfs_ls (struct part_info *info, char *filename);
  40. extern int cramfs_info (struct part_info *info);
  41. static int part_num=0;
  42. #ifndef CFG_JFFS_CUSTOM_PART
  43. static struct part_info part;
  44. #ifndef CONFIG_JFFS2_NAND
  45. struct part_info*
  46. jffs2_part_info(int part_num)
  47. {
  48. extern flash_info_t flash_info[]; /* info for FLASH chips */
  49. int i;
  50. if(part_num==0){
  51. if(part.usr_priv==(void*)1)
  52. return &part;
  53. memset(&part, 0, sizeof(part));
  54. #if defined(CFG_JFFS2_FIRST_SECTOR)
  55. part.offset = (unsigned char *) flash_info[CFG_JFFS2_FIRST_BANK].start[CFG_JFFS2_FIRST_SECTOR];
  56. #else
  57. part.offset = (unsigned char *) flash_info[CFG_JFFS2_FIRST_BANK].start[0];
  58. #endif
  59. /* Figure out flash partition size */
  60. for (i = CFG_JFFS2_FIRST_BANK; i < CFG_JFFS2_NUM_BANKS+CFG_JFFS2_FIRST_BANK; i++)
  61. part.size += flash_info[i].size;
  62. #if defined(CFG_JFFS2_FIRST_SECTOR) && (CFG_JFFS2_FIRST_SECTOR > 0)
  63. part.size -=
  64. flash_info[CFG_JFFS2_FIRST_BANK].start[CFG_JFFS2_FIRST_SECTOR] -
  65. flash_info[CFG_JFFS2_FIRST_BANK].start[0];
  66. #endif
  67. /* unused in current jffs2 loader */
  68. part.erasesize = 0;
  69. /* Mark the struct as ready */
  70. part.usr_priv=(void*)1;
  71. return &part;
  72. }
  73. return 0;
  74. }
  75. #else /* CONFIG_JFFS2_NAND */
  76. struct part_info*
  77. jffs2_part_info(int part_num)
  78. {
  79. if(part_num==0){
  80. if(part.usr_priv==(void*)1)
  81. return &part;
  82. memset(&part, 0, sizeof(part));
  83. part.offset = CONFIG_JFFS2_NAND_OFF;
  84. part.size = CONFIG_JFFS2_NAND_SIZE; /* the bigger size the slower jffs2 */
  85. #ifndef CONFIG_JFFS2_NAND_DEV
  86. #define CONFIG_JFFS2_NAND_DEV 0
  87. #endif
  88. /* nand device with the JFFS2 parition plus 1 */
  89. part.usr_priv = (void*)(CONFIG_JFFS2_NAND_DEV+1);
  90. return &part;
  91. }
  92. return 0;
  93. }
  94. #endif /* CONFIG_JFFS2_NAND */
  95. #endif /* ifndef CFG_JFFS_CUSTOM_PART */
  96. int
  97. do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  98. {
  99. struct part_info* jffs2_part_info(int);
  100. int jffs2_1pass_load(char *, struct part_info *,const char *);
  101. char *fsname;
  102. char *filename = "uImage";
  103. ulong offset = load_addr;
  104. int size;
  105. struct part_info *part;
  106. if (argc == 2) {
  107. filename = argv[1];
  108. }
  109. if (argc == 3) {
  110. offset = simple_strtoul(argv[1], NULL, 16);
  111. load_addr = offset;
  112. filename = argv[2];
  113. }
  114. if (0 != (part=jffs2_part_info(part_num))){
  115. /* check partition type for cramfs */
  116. fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
  117. printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
  118. if (cramfs_check(part)) {
  119. size = cramfs_load ((char *) offset, part, filename);
  120. } else {
  121. /* if this is not cramfs assume jffs2 */
  122. size = jffs2_1pass_load((char *)offset, part, filename);
  123. }
  124. if (size > 0) {
  125. char buf[10];
  126. printf("### %s load complete: %d bytes loaded to 0x%lx\n",
  127. fsname, size, offset);
  128. sprintf(buf, "%x", size);
  129. setenv("filesize", buf);
  130. } else {
  131. printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
  132. }
  133. return !(size > 0);
  134. }
  135. puts ("Active partition not valid\n");
  136. return 0;
  137. }
  138. int
  139. do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  140. {
  141. struct part_info* jffs2_part_info(int);
  142. int jffs2_1pass_ls(struct part_info *,char *);
  143. char *filename = "/";
  144. int ret;
  145. struct part_info *part;
  146. if (argc == 2)
  147. filename = argv[1];
  148. if (0 != (part=jffs2_part_info(part_num))){
  149. /* check partition type for cramfs */
  150. if (cramfs_check(part)) {
  151. ret = cramfs_ls (part, filename);
  152. } else {
  153. /* if this is not cramfs assume jffs2 */
  154. ret = jffs2_1pass_ls(part, filename);
  155. }
  156. return (ret == 1);
  157. }
  158. puts ("Active partition not valid\n");
  159. return 0;
  160. }
  161. int
  162. do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  163. {
  164. struct part_info* jffs2_part_info(int);
  165. int jffs2_1pass_info(struct part_info *);
  166. struct part_info *part;
  167. char *fsname;
  168. int ret;
  169. if ((part=jffs2_part_info(part_num))){
  170. /* check partition type for cramfs */
  171. fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
  172. printf("### filesystem type is %s\n", fsname);
  173. if (cramfs_check(part)) {
  174. ret = cramfs_info (part);
  175. } else {
  176. /* if this is not cramfs assume jffs2 */
  177. ret = jffs2_1pass_info(part);
  178. }
  179. return (ret == 1);
  180. }
  181. puts ("Active partition not valid\n");
  182. return 0;
  183. }
  184. int
  185. do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  186. {
  187. int tmp_part;
  188. char str_part_num[3];
  189. struct part_info* jffs2_part_info(int);
  190. if (argc >= 2) {
  191. tmp_part = simple_strtoul(argv[1], NULL, 16);
  192. }else{
  193. puts ("Need partition number in argument list\n");
  194. return 0;
  195. }
  196. if (jffs2_part_info(tmp_part)){
  197. printf("Partition changed to %d\n",tmp_part);
  198. part_num=tmp_part;
  199. sprintf(str_part_num, "%d", part_num);
  200. setenv("partition", str_part_num);
  201. return 0;
  202. }
  203. printf("Partition %d is not valid partiton\n",tmp_part);
  204. return 0;
  205. }
  206. /***************************************************/
  207. U_BOOT_CMD(
  208. fsload, 3, 0, do_jffs2_fsload,
  209. "fsload\t- load binary file from a filesystem image\n",
  210. "[ off ] [ filename ]\n"
  211. " - load binary file from flash bank\n"
  212. " with offset 'off'\n"
  213. );
  214. U_BOOT_CMD(
  215. fsinfo, 1, 1, do_jffs2_fsinfo,
  216. "fsinfo\t- print information about filesystems\n",
  217. " - print information about filesystems\n"
  218. );
  219. U_BOOT_CMD(
  220. ls, 2, 1, do_jffs2_ls,
  221. "ls\t- list files in a directory (default /)\n",
  222. "[ directory ]\n"
  223. " - list files in a directory.\n"
  224. );
  225. U_BOOT_CMD(
  226. chpart, 2, 0, do_jffs2_chpart,
  227. "chpart\t- change active partition\n",
  228. " - change active partition\n"
  229. );
  230. #endif /* CFG_CMD_JFFS2 */