ubi_attach_detach.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Ported to busybox from mtd-utils.
  2. *
  3. * Licensed under GPLv2, see file LICENSE in this source tree.
  4. */
  5. //applet:IF_UBIATTACH(APPLET_ODDNAME(ubiattach, ubi_attach_detach, _BB_DIR_USR_SBIN, _BB_SUID_DROP, ubiattach))
  6. //applet:IF_UBIDETACH(APPLET_ODDNAME(ubidetach, ubi_attach_detach, _BB_DIR_USR_SBIN, _BB_SUID_DROP, ubidetach))
  7. //kbuild:lib-$(CONFIG_UBIATTACH) += ubi_attach_detach.o
  8. //kbuild:lib-$(CONFIG_UBIDETACH) += ubi_attach_detach.o
  9. //config:config UBIATTACH
  10. //config: bool "ubiattach"
  11. //config: default n
  12. //config: depends on PLATFORM_LINUX
  13. //config: help
  14. //config: Attach MTD device to an UBI device.
  15. //config:
  16. //config:config UBIDETACH
  17. //config: bool "ubidetach"
  18. //config: default n
  19. //config: depends on PLATFORM_LINUX
  20. //config: help
  21. //config: Detach MTD device from an UBI device.
  22. #include "libbb.h"
  23. #include <mtd/ubi-user.h>
  24. #define OPTION_M (1 << 0)
  25. #define OPTION_D (1 << 1)
  26. #define do_attach (ENABLE_UBIATTACH && \
  27. (!ENABLE_UBIDETACH || (applet_name[3] == 'a')))
  28. //usage:#define ubiattach_trivial_usage
  29. //usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
  30. //usage:#define ubiattach_full_usage "\n\n"
  31. //usage: "Attach MTD device to UBI\n"
  32. //usage: "\nOptions:"
  33. //usage: "\n -m MTD_NUM MTD device number to attach"
  34. //usage: "\n -d UBI_NUM UBI device number to assign"
  35. //usage:
  36. //usage:#define ubidetach_trivial_usage
  37. //usage: "-d UBI_NUM UBI_CTRL_DEV"
  38. //usage:#define ubidetach_full_usage "\n\n"
  39. //usage: "Detach MTD device from UBI\n"
  40. //usage: "\nOptions:"
  41. //usage: "\n -d UBI_NUM UBI device number"
  42. int ubi_attach_detach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  43. int ubi_attach_detach_main(int argc UNUSED_PARAM, char **argv)
  44. {
  45. unsigned opts;
  46. char *ubi_ctrl;
  47. //struct stat st;
  48. struct ubi_attach_req req;
  49. int fd;
  50. int mtd_num;
  51. int dev_num = UBI_DEV_NUM_AUTO;
  52. opt_complementary = "=1:m+:d+";
  53. opts = getopt32(argv, "m:d:", &mtd_num, &dev_num);
  54. ubi_ctrl = argv[optind];
  55. fd = xopen(ubi_ctrl, O_RDWR);
  56. //xfstat(fd, &st, ubi_ctrl);
  57. //if (!S_ISCHR(st.st_mode))
  58. // bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
  59. if (do_attach) {
  60. if (!(opts & OPTION_M))
  61. bb_error_msg_and_die("%s device not specified", "MTD");
  62. memset(&req, 0, sizeof(req));
  63. req.mtd_num = mtd_num;
  64. req.ubi_num = dev_num;
  65. xioctl(fd, UBI_IOCATT, &req);
  66. } else { /* detach */
  67. if (!(opts & OPTION_D))
  68. bb_error_msg_and_die("%s device not specified", "UBI");
  69. xioctl(fd, UBI_IOCDET, &dev_num);
  70. }
  71. if (ENABLE_FEATURE_CLEAN_UP)
  72. close(fd);
  73. return EXIT_SUCCESS;
  74. }