ubifs.c 2.9 KB

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