blob: 0d63a10d54488d6e3b9aacbfddcbc56c44d55046 [file] [log] [blame]
Baruch Siach3324c962010-06-16 12:22:58 +02001/* Ported to busybox from mtd-utils.
2 *
3 * Licensed under GPLv2, see file LICENSE in this tarball for details.
4 */
5
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))
8
9//kbuild:lib-$(CONFIG_UBIATTACH) += ubi_attach_detach.o
10//kbuild:lib-$(CONFIG_UBIDETACH) += ubi_attach_detach.o
11
12//config:config UBIATTACH
13//config: bool "ubiattach"
14//config: default n
Jeremie Koenig1d7266d2010-07-19 00:44:56 +020015//config: depends on PLATFORM_LINUX
Baruch Siach3324c962010-06-16 12:22:58 +020016//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
Jeremie Koenig1d7266d2010-07-19 00:44:56 +020022//config: depends on PLATFORM_LINUX
Baruch Siach3324c962010-06-16 12:22:58 +020023//config: help
24//config: Detach MTD device from an UBI device.
25
26#include "libbb.h"
27#include <mtd/ubi-user.h>
28
29#define OPTION_M (1 << 0)
30#define OPTION_D (1 << 1)
31
32#define do_attach (ENABLE_UBIATTACH && \
33 (!ENABLE_UBIDETACH || (applet_name[3] == 'a')))
34
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"
49
50int ubi_attach_detach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
51int ubi_attach_detach_main(int argc UNUSED_PARAM, char **argv)
52{
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;
60
61 opt_complementary = "=1:m+:d+";
62 opts = getopt32(argv, "m:d:", &mtd_num, &dev_num);
63 ubi_ctrl = argv[optind];
64
65 fd = xopen(ubi_ctrl, O_RDWR);
66 //fstat(fd, &st);
67 //if (!S_ISCHR(st.st_mode))
68 // bb_error_msg_and_die("'%s' is not a char device", ubi_ctrl);
69
70 if (do_attach) {
71 if (!(opts & OPTION_M))
72 bb_error_msg_and_die("%s device not specified", "MTD");
73
74 memset(&req, 0, sizeof(req));
75 req.mtd_num = mtd_num;
76 req.ubi_num = dev_num;
77
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");
82
83 xioctl(fd, UBI_IOCDET, &dev_num);
84 }
85
86 if (ENABLE_FEATURE_CLEAN_UP)
87 close(fd);
88
89 return EXIT_SUCCESS;
90}