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 | |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 6 | //config:config UBIATTACH |
| 7 | //config: bool "ubiattach" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 8 | //config: default y |
Denys Vlasenko | e3b1a1f | 2011-02-26 22:24:08 +0100 | [diff] [blame] | 9 | //config: select PLATFORM_LINUX |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 10 | //config: help |
| 11 | //config: Attach MTD device to an UBI device. |
| 12 | //config: |
| 13 | //config:config UBIDETACH |
| 14 | //config: bool "ubidetach" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 15 | //config: default y |
Denys Vlasenko | e3b1a1f | 2011-02-26 22:24:08 +0100 | [diff] [blame] | 16 | //config: select PLATFORM_LINUX |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 17 | //config: help |
| 18 | //config: Detach MTD device from an UBI device. |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 19 | //config: |
| 20 | //config:config UBIMKVOL |
| 21 | //config: bool "ubimkvol" |
| 22 | //config: default y |
Denys Vlasenko | e3b1a1f | 2011-02-26 22:24:08 +0100 | [diff] [blame] | 23 | //config: select PLATFORM_LINUX |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 24 | //config: help |
| 25 | //config: Create a UBI volume. |
| 26 | //config: |
| 27 | //config:config UBIRMVOL |
| 28 | //config: bool "ubirmvol" |
| 29 | //config: default y |
Denys Vlasenko | e3b1a1f | 2011-02-26 22:24:08 +0100 | [diff] [blame] | 30 | //config: select PLATFORM_LINUX |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 31 | //config: help |
| 32 | //config: Delete a UBI volume. |
| 33 | //config: |
| 34 | //config:config UBIRSVOL |
| 35 | //config: bool "ubirsvol" |
| 36 | //config: default y |
Denys Vlasenko | e3b1a1f | 2011-02-26 22:24:08 +0100 | [diff] [blame] | 37 | //config: select PLATFORM_LINUX |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 38 | //config: help |
| 39 | //config: Resize a UBI volume. |
| 40 | |
| 41 | //applet:IF_UBIATTACH(APPLET_ODDNAME(ubiattach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiattach)) |
| 42 | //applet:IF_UBIDETACH(APPLET_ODDNAME(ubidetach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubidetach)) |
| 43 | //applet:IF_UBIMKVOL(APPLET_ODDNAME(ubimkvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubimkvol)) |
| 44 | //applet:IF_UBIRMVOL(APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol)) |
| 45 | //applet:IF_UBIRSVOL(APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol)) |
| 46 | |
| 47 | //kbuild:lib-$(CONFIG_UBIATTACH) += ubi_attach_detach.o |
| 48 | //kbuild:lib-$(CONFIG_UBIDETACH) += ubi_attach_detach.o |
| 49 | //kbuild:lib-$(CONFIG_UBIMKVOL) += ubi_attach_detach.o |
| 50 | //kbuild:lib-$(CONFIG_UBIRMVOL) += ubi_attach_detach.o |
| 51 | //kbuild:lib-$(CONFIG_UBIRSVOL) += ubi_attach_detach.o |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 52 | |
| 53 | #include "libbb.h" |
| 54 | #include <mtd/ubi-user.h> |
| 55 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 56 | #define OPTION_M (1 << 0) |
| 57 | #define OPTION_D (1 << 1) |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 58 | #define OPTION_n (1 << 2) |
| 59 | #define OPTION_N (1 << 3) |
| 60 | #define OPTION_s (1 << 4) |
| 61 | #define OPTION_a (1 << 5) |
| 62 | #define OPTION_t (1 << 6) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 63 | |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 64 | #define do_attach (ENABLE_UBIATTACH && applet_name[3] == 'a') |
| 65 | #define do_detach (ENABLE_UBIDETACH && applet_name[3] == 'd') |
| 66 | #define do_mkvol (ENABLE_UBIMKVOL && applet_name[3] == 'm') |
| 67 | #define do_rmvol (ENABLE_UBIRMVOL && applet_name[4] == 'm') |
| 68 | #define do_rsvol (ENABLE_UBIRSVOL && applet_name[4] == 's') |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 69 | |
| 70 | //usage:#define ubiattach_trivial_usage |
| 71 | //usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV" |
| 72 | //usage:#define ubiattach_full_usage "\n\n" |
| 73 | //usage: "Attach MTD device to UBI\n" |
| 74 | //usage: "\nOptions:" |
| 75 | //usage: "\n -m MTD_NUM MTD device number to attach" |
| 76 | //usage: "\n -d UBI_NUM UBI device number to assign" |
| 77 | //usage: |
| 78 | //usage:#define ubidetach_trivial_usage |
| 79 | //usage: "-d UBI_NUM UBI_CTRL_DEV" |
| 80 | //usage:#define ubidetach_full_usage "\n\n" |
| 81 | //usage: "Detach MTD device from UBI\n" |
| 82 | //usage: "\nOptions:" |
| 83 | //usage: "\n -d UBI_NUM UBI device number" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 84 | //usage: |
| 85 | //usage:#define ubimkvol_trivial_usage |
| 86 | //usage: "UBI_DEVICE -N NAME -s SIZE" |
| 87 | //usage:#define ubimkvol_full_usage "\n\n" |
| 88 | //usage: "Create UBI Volume\n" |
| 89 | //usage: "\nOptions:" |
| 90 | //usage: "\n -a ALIGNMENT Volume alignment (default 1)" |
| 91 | //usage: "\n -n VOLID Volume ID, if not specified, it" |
| 92 | //usage: "\n will be assigned automatically" |
| 93 | //usage: "\n -N NAME Volume name" |
| 94 | //usage: "\n -s SIZE Size in bytes" |
| 95 | //usage: "\n -t TYPE Volume type (static|dynamic)" |
| 96 | //usage: |
| 97 | //usage:#define ubirmvol_trivial_usage |
| 98 | //usage: "UBI_DEVICE -n VOLID" |
| 99 | //usage:#define ubirmvol_full_usage "\n\n" |
| 100 | //usage: "Remove UBI Volume\n" |
| 101 | //usage: "\nOptions:" |
| 102 | //usage: "\n -n VOLID Volume ID" |
| 103 | //usage: |
| 104 | //usage:#define ubirsvol_trivial_usage |
| 105 | //usage: "UBI_DEVICE -N NAME -s SIZE" |
| 106 | //usage:#define ubirsvol_full_usage "\n\n" |
| 107 | //usage: "Resize UBI Volume\n" |
| 108 | //usage: "\nOptions:" |
| 109 | //usage: "\n -N NAME Volume name" |
| 110 | //usage: "\n -s SIZE Size in bytes" |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 111 | |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 112 | int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 113 | int ubi_tools_main(int argc UNUSED_PARAM, char **argv) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 114 | { |
| 115 | unsigned opts; |
| 116 | char *ubi_ctrl; |
| 117 | //struct stat st; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 118 | int fd; |
| 119 | int mtd_num; |
| 120 | int dev_num = UBI_DEV_NUM_AUTO; |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 121 | int vol_id = UBI_VOL_NUM_AUTO; |
| 122 | char *vol_name = NULL; |
| 123 | int size_bytes; |
| 124 | int alignment = 1; |
| 125 | char *type = NULL; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 126 | |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 127 | opt_complementary = "=1:m+:d+:n+:s+:a+"; |
| 128 | opts = getopt32(argv, "m:d:n:N:s:a:t:", |
| 129 | &mtd_num, &dev_num, &vol_id, |
| 130 | &vol_name, &size_bytes, &alignment, &type |
| 131 | ); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 132 | ubi_ctrl = argv[optind]; |
| 133 | |
| 134 | fd = xopen(ubi_ctrl, O_RDWR); |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 135 | //xfstat(fd, &st, ubi_ctrl); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 136 | //if (!S_ISCHR(st.st_mode)) |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 137 | // bb_error_msg_and_die("%s: not a char device", ubi_ctrl); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 138 | |
| 139 | if (do_attach) { |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 140 | struct ubi_attach_req req; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 141 | if (!(opts & OPTION_M)) |
| 142 | bb_error_msg_and_die("%s device not specified", "MTD"); |
| 143 | |
| 144 | memset(&req, 0, sizeof(req)); |
| 145 | req.mtd_num = mtd_num; |
| 146 | req.ubi_num = dev_num; |
| 147 | |
| 148 | xioctl(fd, UBI_IOCATT, &req); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 149 | } else |
| 150 | if (do_detach) { |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 151 | if (!(opts & OPTION_D)) |
| 152 | bb_error_msg_and_die("%s device not specified", "UBI"); |
| 153 | |
| 154 | xioctl(fd, UBI_IOCDET, &dev_num); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 155 | } else |
| 156 | if (do_mkvol) { |
| 157 | struct ubi_mkvol_req req; |
| 158 | int vol_name_len; |
| 159 | if (!(opts & OPTION_s)) |
| 160 | bb_error_msg_and_die("%s size not specified", "UBI"); |
| 161 | if (!(opts & OPTION_N)) |
| 162 | bb_error_msg_and_die("%s name not specified", "UBI"); |
| 163 | vol_name_len = strlen(vol_name); |
| 164 | if (vol_name_len > UBI_MAX_VOLUME_NAME) |
| 165 | bb_error_msg_and_die("%s volume name too long", "UBI"); |
| 166 | |
| 167 | memset(&req, 0, sizeof(req)); |
| 168 | req.vol_id = vol_id; |
| 169 | if (opts & OPTION_t) { |
| 170 | if (type[0] == 's') |
| 171 | req.vol_type = UBI_STATIC_VOLUME; |
| 172 | else |
| 173 | req.vol_type = UBI_DYNAMIC_VOLUME; |
| 174 | } else { |
| 175 | req.vol_type = UBI_DYNAMIC_VOLUME; |
| 176 | } |
| 177 | req.alignment = alignment; |
| 178 | req.bytes = size_bytes; |
| 179 | strncpy(req.name, vol_name, UBI_MAX_VOLUME_NAME); |
| 180 | req.name_len = vol_name_len; |
| 181 | |
| 182 | xioctl(fd, UBI_IOCMKVOL, &req); |
| 183 | } else |
| 184 | if (do_rmvol) { |
| 185 | if (!(opts & OPTION_n)) |
| 186 | bb_error_msg_and_die("%s volume id not specified", "UBI"); |
| 187 | |
| 188 | xioctl(fd, UBI_IOCRMVOL, &vol_id); |
| 189 | } else |
| 190 | if (do_rsvol) { |
| 191 | struct ubi_rsvol_req req; |
| 192 | if (!(opts & OPTION_s)) |
| 193 | bb_error_msg_and_die("%s size not specified", "UBI"); |
| 194 | if (!(opts & OPTION_n)) |
| 195 | bb_error_msg_and_die("%s volume id not specified", "UBI"); |
| 196 | |
| 197 | memset(&req, 0, sizeof(req)); |
| 198 | req.bytes = size_bytes; |
| 199 | req.vol_id = vol_id; |
| 200 | |
| 201 | xioctl(fd, UBI_IOCRSVOL, &req); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (ENABLE_FEATURE_CLEAN_UP) |
| 205 | close(fd); |
| 206 | |
| 207 | return EXIT_SUCCESS; |
| 208 | } |