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. |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 40 | //config: |
| 41 | //config:config UBIUPDATEVOL |
| 42 | //config: bool "ubiupdatevol" |
| 43 | //config: default y |
| 44 | //config: select PLATFORM_LINUX |
| 45 | //config: help |
| 46 | //config: Update a UBI volume. |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 47 | |
| 48 | //applet:IF_UBIATTACH(APPLET_ODDNAME(ubiattach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiattach)) |
| 49 | //applet:IF_UBIDETACH(APPLET_ODDNAME(ubidetach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubidetach)) |
| 50 | //applet:IF_UBIMKVOL(APPLET_ODDNAME(ubimkvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubimkvol)) |
| 51 | //applet:IF_UBIRMVOL(APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol)) |
| 52 | //applet:IF_UBIRSVOL(APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol)) |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 53 | //applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol)) |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 54 | |
Denys Vlasenko | d1993f1 | 2011-04-26 04:32:05 +0200 | [diff] [blame] | 55 | //kbuild:lib-$(CONFIG_UBIATTACH) += ubi_tools.o |
| 56 | //kbuild:lib-$(CONFIG_UBIDETACH) += ubi_tools.o |
| 57 | //kbuild:lib-$(CONFIG_UBIMKVOL) += ubi_tools.o |
| 58 | //kbuild:lib-$(CONFIG_UBIRMVOL) += ubi_tools.o |
| 59 | //kbuild:lib-$(CONFIG_UBIRSVOL) += ubi_tools.o |
| 60 | //kbuild:lib-$(CONFIG_UBIUPDATEVOL) += ubi_tools.o |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 61 | |
| 62 | #include "libbb.h" |
| 63 | #include <mtd/ubi-user.h> |
| 64 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 65 | #define OPTION_M (1 << 0) |
| 66 | #define OPTION_D (1 << 1) |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 67 | #define OPTION_n (1 << 2) |
| 68 | #define OPTION_N (1 << 3) |
| 69 | #define OPTION_s (1 << 4) |
| 70 | #define OPTION_a (1 << 5) |
| 71 | #define OPTION_t (1 << 6) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 72 | |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 73 | #define do_attach (ENABLE_UBIATTACH && applet_name[3] == 'a') |
| 74 | #define do_detach (ENABLE_UBIDETACH && applet_name[3] == 'd') |
| 75 | #define do_mkvol (ENABLE_UBIMKVOL && applet_name[3] == 'm') |
| 76 | #define do_rmvol (ENABLE_UBIRMVOL && applet_name[4] == 'm') |
| 77 | #define do_rsvol (ENABLE_UBIRSVOL && applet_name[4] == 's') |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 78 | #define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u') |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 79 | |
| 80 | //usage:#define ubiattach_trivial_usage |
| 81 | //usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV" |
| 82 | //usage:#define ubiattach_full_usage "\n\n" |
| 83 | //usage: "Attach MTD device to UBI\n" |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 84 | //usage: "\n -m MTD_NUM MTD device number to attach" |
| 85 | //usage: "\n -d UBI_NUM UBI device number to assign" |
| 86 | //usage: |
| 87 | //usage:#define ubidetach_trivial_usage |
| 88 | //usage: "-d UBI_NUM UBI_CTRL_DEV" |
| 89 | //usage:#define ubidetach_full_usage "\n\n" |
| 90 | //usage: "Detach MTD device from UBI\n" |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 91 | //usage: "\n -d UBI_NUM UBI device number" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 92 | //usage: |
| 93 | //usage:#define ubimkvol_trivial_usage |
| 94 | //usage: "UBI_DEVICE -N NAME -s SIZE" |
| 95 | //usage:#define ubimkvol_full_usage "\n\n" |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 96 | //usage: "Create UBI volume\n" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 97 | //usage: "\n -a ALIGNMENT Volume alignment (default 1)" |
| 98 | //usage: "\n -n VOLID Volume ID, if not specified, it" |
| 99 | //usage: "\n will be assigned automatically" |
| 100 | //usage: "\n -N NAME Volume name" |
| 101 | //usage: "\n -s SIZE Size in bytes" |
| 102 | //usage: "\n -t TYPE Volume type (static|dynamic)" |
| 103 | //usage: |
| 104 | //usage:#define ubirmvol_trivial_usage |
| 105 | //usage: "UBI_DEVICE -n VOLID" |
| 106 | //usage:#define ubirmvol_full_usage "\n\n" |
Denys Vlasenko | 0bfb9c2 | 2011-04-26 04:31:03 +0200 | [diff] [blame] | 107 | //usage: "Remove UBI volume\n" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 108 | //usage: "\n -n VOLID Volume ID" |
| 109 | //usage: |
| 110 | //usage:#define ubirsvol_trivial_usage |
Reuben Dowle | ab940af | 2011-04-04 10:55:55 +1200 | [diff] [blame] | 111 | //usage: "UBI_DEVICE -n VOLID -s SIZE" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 112 | //usage:#define ubirsvol_full_usage "\n\n" |
Denys Vlasenko | 0bfb9c2 | 2011-04-26 04:31:03 +0200 | [diff] [blame] | 113 | //usage: "Resize UBI volume\n" |
Reuben Dowle | ab940af | 2011-04-04 10:55:55 +1200 | [diff] [blame] | 114 | //usage: "\n -n VOLID Volume ID to resize" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 115 | //usage: "\n -s SIZE Size in bytes" |
Denys Vlasenko | 0bfb9c2 | 2011-04-26 04:31:03 +0200 | [diff] [blame] | 116 | //usage: |
| 117 | //usage:#define ubiupdatevol_trivial_usage |
| 118 | //usage: "UBI_DEVICE [IMG_FILE]" |
| 119 | //usage:#define ubiupdatevol_full_usage "\n\n" |
| 120 | //usage: "Update UBI volume\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 121 | //usage: "\n -t Truncate UBI volume" |
| 122 | //usage: "\n -s SIZE Bytes in input (if reading stdin)" |
Denys Vlasenko | 0bfb9c2 | 2011-04-26 04:31:03 +0200 | [diff] [blame] | 123 | |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 124 | |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 125 | int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 126 | int ubi_tools_main(int argc UNUSED_PARAM, char **argv) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 127 | { |
| 128 | unsigned opts; |
| 129 | char *ubi_ctrl; |
| 130 | //struct stat st; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 131 | int fd; |
| 132 | int mtd_num; |
| 133 | int dev_num = UBI_DEV_NUM_AUTO; |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 134 | int vol_id = UBI_VOL_NUM_AUTO; |
| 135 | char *vol_name = NULL; |
| 136 | int size_bytes; |
| 137 | int alignment = 1; |
| 138 | char *type = NULL; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 139 | |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 140 | opt_complementary = "-1:m+:d+:n+:s+:a+"; |
| 141 | opts = getopt32(argv, "m:d:n:N:s:a:t::", |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 142 | &mtd_num, &dev_num, &vol_id, |
| 143 | &vol_name, &size_bytes, &alignment, &type |
| 144 | ); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 145 | ubi_ctrl = argv[optind]; |
| 146 | |
| 147 | fd = xopen(ubi_ctrl, O_RDWR); |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 148 | //xfstat(fd, &st, ubi_ctrl); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 149 | //if (!S_ISCHR(st.st_mode)) |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 150 | // bb_error_msg_and_die("%s: not a char device", ubi_ctrl); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 151 | |
| 152 | if (do_attach) { |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 153 | struct ubi_attach_req req; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 154 | if (!(opts & OPTION_M)) |
| 155 | bb_error_msg_and_die("%s device not specified", "MTD"); |
| 156 | |
| 157 | memset(&req, 0, sizeof(req)); |
| 158 | req.mtd_num = mtd_num; |
| 159 | req.ubi_num = dev_num; |
| 160 | |
| 161 | xioctl(fd, UBI_IOCATT, &req); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 162 | } else |
| 163 | if (do_detach) { |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 164 | if (!(opts & OPTION_D)) |
| 165 | bb_error_msg_and_die("%s device not specified", "UBI"); |
| 166 | |
| 167 | xioctl(fd, UBI_IOCDET, &dev_num); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 168 | } else |
| 169 | if (do_mkvol) { |
| 170 | struct ubi_mkvol_req req; |
| 171 | int vol_name_len; |
| 172 | if (!(opts & OPTION_s)) |
| 173 | bb_error_msg_and_die("%s size not specified", "UBI"); |
| 174 | if (!(opts & OPTION_N)) |
| 175 | bb_error_msg_and_die("%s name not specified", "UBI"); |
| 176 | vol_name_len = strlen(vol_name); |
| 177 | if (vol_name_len > UBI_MAX_VOLUME_NAME) |
| 178 | bb_error_msg_and_die("%s volume name too long", "UBI"); |
| 179 | |
| 180 | memset(&req, 0, sizeof(req)); |
| 181 | req.vol_id = vol_id; |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 182 | if ((opts & OPTION_t) && type) { |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 183 | if (type[0] == 's') |
| 184 | req.vol_type = UBI_STATIC_VOLUME; |
| 185 | else |
| 186 | req.vol_type = UBI_DYNAMIC_VOLUME; |
| 187 | } else { |
| 188 | req.vol_type = UBI_DYNAMIC_VOLUME; |
| 189 | } |
| 190 | req.alignment = alignment; |
| 191 | req.bytes = size_bytes; |
| 192 | strncpy(req.name, vol_name, UBI_MAX_VOLUME_NAME); |
| 193 | req.name_len = vol_name_len; |
| 194 | |
| 195 | xioctl(fd, UBI_IOCMKVOL, &req); |
| 196 | } else |
| 197 | if (do_rmvol) { |
| 198 | if (!(opts & OPTION_n)) |
| 199 | bb_error_msg_and_die("%s volume id not specified", "UBI"); |
| 200 | |
| 201 | xioctl(fd, UBI_IOCRMVOL, &vol_id); |
| 202 | } else |
| 203 | if (do_rsvol) { |
| 204 | struct ubi_rsvol_req req; |
| 205 | if (!(opts & OPTION_s)) |
| 206 | bb_error_msg_and_die("%s size not specified", "UBI"); |
| 207 | if (!(opts & OPTION_n)) |
| 208 | bb_error_msg_and_die("%s volume id not specified", "UBI"); |
| 209 | |
| 210 | memset(&req, 0, sizeof(req)); |
| 211 | req.bytes = size_bytes; |
| 212 | req.vol_id = vol_id; |
| 213 | |
| 214 | xioctl(fd, UBI_IOCRSVOL, &req); |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 215 | } else |
| 216 | if (do_update) { |
| 217 | long long bytes; |
| 218 | |
| 219 | if (opts & OPTION_t) { |
| 220 | // truncate the volume by starting an update for size 0 |
| 221 | bytes = 0; |
| 222 | xioctl(fd, UBI_IOCVOLUP, &bytes); |
| 223 | } |
| 224 | else { |
| 225 | struct stat st; |
| 226 | char buf[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size") + 2 * sizeof(int)*3]; |
| 227 | int input_fd; |
| 228 | unsigned ubinum, volnum; |
| 229 | unsigned leb_size; |
| 230 | ssize_t len; |
| 231 | char *input_data; |
| 232 | |
| 233 | // Make assumption that device not is in normal format. |
| 234 | // Removes need for scanning sysfs tree as full libubi does |
| 235 | if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2) |
| 236 | bb_error_msg_and_die("%s volume node not in correct format", "UBI"); |
| 237 | |
| 238 | sprintf(buf, "/sys/class/ubi/ubi%u_%u/usable_eb_size", ubinum, volnum); |
| 239 | if (open_read_close(buf, buf, sizeof(buf)) <= 0) |
| 240 | bb_error_msg_and_die("%s could not get LEB size", "UBI"); |
| 241 | if (sscanf(buf, "%u", &leb_size) != 1) |
| 242 | bb_error_msg_and_die("%s could not get LEB size", "UBI"); |
| 243 | |
| 244 | if (opts & OPTION_s) { |
| 245 | input_fd = 0; |
| 246 | } else { |
| 247 | if (!argv[optind+1]) |
| 248 | bb_show_usage(); |
| 249 | xstat(argv[optind+1], &st); |
| 250 | size_bytes = st.st_size; |
| 251 | input_fd = xopen(argv[optind+1], O_RDONLY); |
| 252 | } |
| 253 | |
| 254 | bytes = size_bytes; |
| 255 | xioctl(fd, UBI_IOCVOLUP, &bytes); |
| 256 | |
| 257 | input_data = xmalloc(leb_size); |
| 258 | while ((len = full_read(input_fd, input_data, leb_size)) > 0) { |
| 259 | xwrite(fd, input_data, len); |
| 260 | } |
| 261 | if (len < 0) |
| 262 | bb_error_msg_and_die("%s volume update failed", "UBI"); |
| 263 | if (ENABLE_FEATURE_CLEAN_UP) |
| 264 | close(input_fd); |
| 265 | } |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (ENABLE_FEATURE_CLEAN_UP) |
| 269 | close(fd); |
| 270 | |
| 271 | return EXIT_SUCCESS; |
| 272 | } |