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