blob: 7b92a8afcfd8f940f345cf5e41e4abd62643b0a5 [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
15//config: help
16//config: Attach MTD device to an UBI device.
17//config:
18//config:config UBIDETACH
19//config: bool "ubidetach"
20//config: default n
21//config: help
22//config: Detach MTD device from an UBI device.
23
24#include "libbb.h"
25#include <mtd/ubi-user.h>
26
27#define OPTION_M (1 << 0)
28#define OPTION_D (1 << 1)
29
30#define do_attach (ENABLE_UBIATTACH && \
31 (!ENABLE_UBIDETACH || (applet_name[3] == 'a')))
32
33//usage:#define ubiattach_trivial_usage
34//usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
35//usage:#define ubiattach_full_usage "\n\n"
36//usage: "Attach MTD device to UBI\n"
37//usage: "\nOptions:"
38//usage: "\n -m MTD_NUM MTD device number to attach"
39//usage: "\n -d UBI_NUM UBI device number to assign"
40//usage:
41//usage:#define ubidetach_trivial_usage
42//usage: "-d UBI_NUM UBI_CTRL_DEV"
43//usage:#define ubidetach_full_usage "\n\n"
44//usage: "Detach MTD device from UBI\n"
45//usage: "\nOptions:"
46//usage: "\n -d UBI_NUM UBI device number"
47
48int ubi_attach_detach_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49int ubi_attach_detach_main(int argc UNUSED_PARAM, char **argv)
50{
51 unsigned opts;
52 char *ubi_ctrl;
53 //struct stat st;
54 struct ubi_attach_req req;
55 int fd;
56 int mtd_num;
57 int dev_num = UBI_DEV_NUM_AUTO;
58
59 opt_complementary = "=1:m+:d+";
60 opts = getopt32(argv, "m:d:", &mtd_num, &dev_num);
61 ubi_ctrl = argv[optind];
62
63 fd = xopen(ubi_ctrl, O_RDWR);
64 //fstat(fd, &st);
65 //if (!S_ISCHR(st.st_mode))
66 // bb_error_msg_and_die("'%s' is not a char device", ubi_ctrl);
67
68 if (do_attach) {
69 if (!(opts & OPTION_M))
70 bb_error_msg_and_die("%s device not specified", "MTD");
71
72 memset(&req, 0, sizeof(req));
73 req.mtd_num = mtd_num;
74 req.ubi_num = dev_num;
75
76 xioctl(fd, UBI_IOCATT, &req);
77 } else { /* detach */
78 if (!(opts & OPTION_D))
79 bb_error_msg_and_die("%s device not specified", "UBI");
80
81 xioctl(fd, UBI_IOCDET, &dev_num);
82 }
83
84 if (ENABLE_FEATURE_CLEAN_UP)
85 close(fd);
86
87 return EXIT_SUCCESS;
88}