Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 1 | /* Ported to busybox from mtd-utils. |
| 2 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 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 Koenig | 1d7266d | 2010-07-19 00:44:56 +0200 | [diff] [blame] | 15 | //config: depends on PLATFORM_LINUX |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 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 |
Jeremie Koenig | 1d7266d | 2010-07-19 00:44:56 +0200 | [diff] [blame] | 22 | //config: depends on PLATFORM_LINUX |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 23 | //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 | |
| 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) |
| 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); |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 66 | //xfstat(fd, &st, ubi_ctrl); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 67 | //if (!S_ISCHR(st.st_mode)) |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 68 | // bb_error_msg_and_die("%s: not a char device", ubi_ctrl); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 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 | } |