[PATCH] miscutils: add ubiattach and ubidetach

Baruch Siach baruch at tkos.co.il
Wed Jun 16 08:28:11 UTC 2010


Ported from the mtd-utils package.

Signed-off-by: Baruch Siach <baruch at tkos.co.il>
---
 include/applets.src.h         |    2 +
 include/usage.src.h           |   15 +++++++++++
 miscutils/Config.src          |   10 +++++++
 miscutils/Kbuild.src          |    2 +
 miscutils/ubi_attach_detach.c |   53 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 82 insertions(+), 0 deletions(-)
 create mode 100644 miscutils/ubi_attach_detach.c

diff --git a/include/applets.src.h b/include/applets.src.h
index 9162b66..048fc81 100644
--- a/include/applets.src.h
+++ b/include/applets.src.h
@@ -400,6 +400,8 @@ IF_TTY(APPLET(tty, _BB_DIR_USR_BIN, _BB_SUID_DROP))
 IF_TTYSIZE(APPLET(ttysize, _BB_DIR_USR_BIN, _BB_SUID_DROP))
 IF_TUNCTL(APPLET(tunctl, _BB_DIR_SBIN, _BB_SUID_DROP))
 IF_TUNE2FS(APPLET(tune2fs, _BB_DIR_SBIN, _BB_SUID_DROP))
+IF_UBIATTACH(APPLET_ODDNAME(ubiattach, ubi_attach_detach, _BB_DIR_USR_SBIN, _BB_SUID_DROP, ubiattach))
+IF_UBIDETACH(APPLET_ODDNAME(ubidetach, ubi_attach_detach, _BB_DIR_USR_SBIN, _BB_SUID_DROP, ubidetach))
 IF_UDHCPC(APPLET(udhcpc, _BB_DIR_SBIN, _BB_SUID_DROP))
 IF_UDHCPD(APPLET(udhcpd, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
 IF_UDPSVD(APPLET_ODDNAME(udpsvd, tcpudpsvd, _BB_DIR_USR_BIN, _BB_SUID_DROP, udpsvd))
diff --git a/include/usage.src.h b/include/usage.src.h
index f30edbc..dfd59bf 100644
--- a/include/usage.src.h
+++ b/include/usage.src.h
@@ -4649,6 +4649,21 @@ INSERT
 #define tune2fs_full_usage "\n\n" \
        "Adjust filesystem options on ext[23] filesystems"
 
+#define ubiattach_trivial_usage \
+	"UBI_CTRL_DEV -m MTD_NUM [-d UBI_NUM]"
+#define ubiattach_full_usage "\n\n" \
+       "Attach MTD device to UBI\n" \
+     "\nOptions:" \
+     "\n	-m MTD_NUM	MTD device number to attach" \
+     "\n	-d UBI_NUM	number to assign to the new UBI device" \
+
+#define ubidetach_trivial_usage \
+	"UBI_CTRL_DEV [-d UBI_NUM]"
+#define ubidetach_full_usage "\n\n" \
+       "Detach MTD device from UBI\n" \
+     "\nOptions:" \
+     "\n	-d UBI_NUM	UBI device number to detach" \
+
 #define udhcpc_trivial_usage \
        "[-fbnqvoCR] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE]\n" \
        "	[-H HOSTNAME] [-c CID] [-V VENDOR] [-O DHCP_OPT]..." IF_FEATURE_UDHCP_PORT(" [-P N]")
diff --git a/miscutils/Config.src b/miscutils/Config.src
index 012132e..1d8a38a 100644
--- a/miscutils/Config.src
+++ b/miscutils/Config.src
@@ -626,6 +626,16 @@ config TTYSIZE
 	  error, but returns default 80x24.
 	  Usage in shell scripts: width=`ttysize w`.
 
+config UBIATTACH
+	bool "ubiattach"
+	help
+	  Attach MTD device to an UBI device.
+
+config UBIDETACH
+	bool "ubidetach"
+	help
+	  Detach MTD device from an UBI device.
+
 config VOLNAME
 	bool "volname"
 	default y
diff --git a/miscutils/Kbuild.src b/miscutils/Kbuild.src
index d9bf143..1d73c1f 100644
--- a/miscutils/Kbuild.src
+++ b/miscutils/Kbuild.src
@@ -45,6 +45,8 @@ lib-$(CONFIG_TASKSET)     += taskset.o
 lib-$(CONFIG_TIME)        += time.o
 lib-$(CONFIG_TIMEOUT)     += timeout.o
 lib-$(CONFIG_TTYSIZE)     += ttysize.o
+lib-$(CONFIG_UBIATTACH)   += ubi_attach_detach.o
+lib-$(CONFIG_UBIDETACH)   += ubi_attach_detach.o
 lib-$(CONFIG_VOLNAME)     += volname.o
 lib-$(CONFIG_WALL)        += wall.o
 lib-$(CONFIG_WATCHDOG)    += watchdog.o
diff --git a/miscutils/ubi_attach_detach.c b/miscutils/ubi_attach_detach.c
new file mode 100644
index 0000000..7e33c3d
--- /dev/null
+++ b/miscutils/ubi_attach_detach.c
@@ -0,0 +1,53 @@
+/* Proted to busybox from mtd-utils.
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+#include <mtd/ubi-user.h>
+
+#define OPTION_M	(1 << 0)
+#define OPTION_D	(1 << 1)
+
+#define do_attach (ENABLE_UBIATTACH && \
+		(!ENABLE_UBIDETACH || (applet_name[3] == 'a')))
+
+int ubi_attach_detach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int ubi_attach_detach_main(int argc UNUSED_PARAM, char **argv)
+{
+	unsigned int flags;
+	char *ubi_ctrl;
+	struct stat st;
+	struct ubi_attach_req req;
+	int fd, mtd_num, dev_num;
+
+	opt_complementary = "=1:m+:d+";
+	flags = getopt32(argv, "m:d:", &mtd_num, &dev_num);
+	ubi_ctrl = argv[optind];
+
+	fd = xopen(ubi_ctrl, O_RDWR);
+	fstat(fd, &st);
+	if (!S_ISCHR(st.st_mode))
+		bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
+
+	if (do_attach) {
+		if (!(flags & OPTION_M))
+			bb_error_msg_and_die("MTD device not specified");
+
+		memset(&req, 0, sizeof(struct ubi_attach_req));
+		req.mtd_num = mtd_num;
+		req.ubi_num = (flags & OPTION_D) ? dev_num : UBI_DEV_NUM_AUTO;
+
+		xioctl(fd, UBI_IOCATT, &req);
+	} else { /* detach */
+		if (!(flags & OPTION_D))
+			bb_error_msg_and_die("UBI device not specified");
+
+		xioctl(fd, UBI_IOCDET, &dev_num);
+	}
+
+	if (ENABLE_FEATURE_CLEAN_UP)
+		close(fd);
+
+	return EXIT_SUCCESS;
+}
-- 
1.7.1



More information about the busybox mailing list