cmd_ubifs.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * (C) Copyright 2008
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * UBIFS command support
  9. */
  10. #undef DEBUG
  11. #include <common.h>
  12. #include <config.h>
  13. #include <command.h>
  14. #include "../fs/ubifs/ubifs.h"
  15. static int ubifs_initialized;
  16. static int ubifs_mounted;
  17. static int do_ubifs_mount(cmd_tbl_t *cmdtp, int flag, int argc,
  18. char * const argv[])
  19. {
  20. char *vol_name;
  21. int ret;
  22. if (argc != 2)
  23. return CMD_RET_USAGE;
  24. vol_name = argv[1];
  25. debug("Using volume %s\n", vol_name);
  26. if (ubifs_initialized == 0) {
  27. ubifs_init();
  28. ubifs_initialized = 1;
  29. }
  30. ret = uboot_ubifs_mount(vol_name);
  31. if (ret)
  32. return -1;
  33. ubifs_mounted = 1;
  34. return 0;
  35. }
  36. int ubifs_is_mounted(void)
  37. {
  38. return ubifs_mounted;
  39. }
  40. void cmd_ubifs_umount(void)
  41. {
  42. if (ubifs_sb) {
  43. printf("Unmounting UBIFS volume %s!\n",
  44. ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
  45. ubifs_umount(ubifs_sb->s_fs_info);
  46. }
  47. ubifs_sb = NULL;
  48. ubifs_mounted = 0;
  49. ubifs_initialized = 0;
  50. }
  51. static int do_ubifs_umount(cmd_tbl_t *cmdtp, int flag, int argc,
  52. char * const argv[])
  53. {
  54. if (argc != 1)
  55. return CMD_RET_USAGE;
  56. if (ubifs_initialized == 0) {
  57. printf("No UBIFS volume mounted!\n");
  58. return -1;
  59. }
  60. cmd_ubifs_umount();
  61. return 0;
  62. }
  63. static int do_ubifs_ls(cmd_tbl_t *cmdtp, int flag, int argc,
  64. char * const argv[])
  65. {
  66. char *filename = "/";
  67. int ret;
  68. if (!ubifs_mounted) {
  69. printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
  70. return -1;
  71. }
  72. if (argc == 2)
  73. filename = argv[1];
  74. debug("Using filename %s\n", filename);
  75. ret = ubifs_ls(filename);
  76. if (ret) {
  77. printf("** File not found %s **\n", filename);
  78. ret = CMD_RET_FAILURE;
  79. }
  80. return ret;
  81. }
  82. static int do_ubifs_load(cmd_tbl_t *cmdtp, int flag, int argc,
  83. char * const argv[])
  84. {
  85. char *filename;
  86. char *endp;
  87. int ret;
  88. u32 addr;
  89. u32 size = 0;
  90. if (!ubifs_mounted) {
  91. printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
  92. return -1;
  93. }
  94. if (argc < 3)
  95. return CMD_RET_USAGE;
  96. addr = simple_strtoul(argv[1], &endp, 16);
  97. if (endp == argv[1])
  98. return CMD_RET_USAGE;
  99. filename = argv[2];
  100. if (argc == 4) {
  101. size = simple_strtoul(argv[3], &endp, 16);
  102. if (endp == argv[3])
  103. return CMD_RET_USAGE;
  104. }
  105. debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size);
  106. ret = ubifs_load(filename, addr, size);
  107. if (ret) {
  108. printf("** File not found %s **\n", filename);
  109. ret = CMD_RET_FAILURE;
  110. }
  111. return ret;
  112. }
  113. U_BOOT_CMD(
  114. ubifsmount, 2, 0, do_ubifs_mount,
  115. "mount UBIFS volume",
  116. "<volume-name>\n"
  117. " - mount 'volume-name' volume"
  118. );
  119. U_BOOT_CMD(
  120. ubifsumount, 1, 0, do_ubifs_umount,
  121. "unmount UBIFS volume",
  122. " - unmount current volume"
  123. );
  124. U_BOOT_CMD(
  125. ubifsls, 2, 0, do_ubifs_ls,
  126. "list files in a directory",
  127. "[directory]\n"
  128. " - list files in a 'directory' (default '/')"
  129. );
  130. U_BOOT_CMD(
  131. ubifsload, 4, 0, do_ubifs_load,
  132. "load file from an UBIFS filesystem",
  133. "<addr> <filename> [bytes]\n"
  134. " - load file 'filename' to address 'addr'"
  135. );