1 /* Ported to busybox from mtd-utils.
3 * Licensed under GPLv2, see file LICENSE in this source tree.
6 //config:config UBIATTACH
7 //config: bool "ubiattach"
9 //config: select PLATFORM_LINUX
11 //config: Attach MTD device to an UBI device.
13 //config:config UBIDETACH
14 //config: bool "ubidetach"
16 //config: select PLATFORM_LINUX
18 //config: Detach MTD device from an UBI device.
20 //config:config UBIMKVOL
21 //config: bool "ubimkvol"
23 //config: select PLATFORM_LINUX
25 //config: Create a UBI volume.
27 //config:config UBIRMVOL
28 //config: bool "ubirmvol"
30 //config: select PLATFORM_LINUX
32 //config: Delete a UBI volume.
34 //config:config UBIRSVOL
35 //config: bool "ubirsvol"
37 //config: select PLATFORM_LINUX
39 //config: Resize a UBI volume.
41 //config:config UBIUPDATEVOL
42 //config: bool "ubiupdatevol"
44 //config: select PLATFORM_LINUX
46 //config: Update a UBI volume.
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))
53 //applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol))
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
63 /* Some versions of kernel have broken headers, need this hack */
65 # define __packed __attribute__((packed))
67 #include <mtd/ubi-user.h>
69 #define OPTION_M (1 << 0)
70 #define OPTION_D (1 << 1)
71 #define OPTION_n (1 << 2)
72 #define OPTION_N (1 << 3)
73 #define OPTION_s (1 << 4)
74 #define OPTION_a (1 << 5)
75 #define OPTION_t (1 << 6)
77 #define do_attach (ENABLE_UBIATTACH && applet_name[3] == 'a')
78 #define do_detach (ENABLE_UBIDETACH && applet_name[3] == 'd')
79 #define do_mkvol (ENABLE_UBIMKVOL && applet_name[3] == 'm')
80 #define do_rmvol (ENABLE_UBIRMVOL && applet_name[4] == 'm')
81 #define do_rsvol (ENABLE_UBIRSVOL && applet_name[4] == 's')
82 #define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u')
84 //usage:#define ubiattach_trivial_usage
85 //usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
86 //usage:#define ubiattach_full_usage "\n\n"
87 //usage: "Attach MTD device to UBI\n"
88 //usage: "\n -m MTD_NUM MTD device number to attach"
89 //usage: "\n -d UBI_NUM UBI device number to assign"
91 //usage:#define ubidetach_trivial_usage
92 //usage: "-d UBI_NUM UBI_CTRL_DEV"
93 //usage:#define ubidetach_full_usage "\n\n"
94 //usage: "Detach MTD device from UBI\n"
95 //usage: "\n -d UBI_NUM UBI device number"
97 //usage:#define ubimkvol_trivial_usage
98 //usage: "UBI_DEVICE -N NAME -s SIZE"
99 //usage:#define ubimkvol_full_usage "\n\n"
100 //usage: "Create UBI volume\n"
101 //usage: "\n -a ALIGNMENT Volume alignment (default 1)"
102 //usage: "\n -n VOLID Volume ID, if not specified, it"
103 //usage: "\n will be assigned automatically"
104 //usage: "\n -N NAME Volume name"
105 //usage: "\n -s SIZE Size in bytes"
106 //usage: "\n -t TYPE Volume type (static|dynamic)"
108 //usage:#define ubirmvol_trivial_usage
109 //usage: "UBI_DEVICE -n VOLID"
110 //usage:#define ubirmvol_full_usage "\n\n"
111 //usage: "Remove UBI volume\n"
112 //usage: "\n -n VOLID Volume ID"
114 //usage:#define ubirsvol_trivial_usage
115 //usage: "UBI_DEVICE -n VOLID -s SIZE"
116 //usage:#define ubirsvol_full_usage "\n\n"
117 //usage: "Resize UBI volume\n"
118 //usage: "\n -n VOLID Volume ID to resize"
119 //usage: "\n -s SIZE Size in bytes"
121 //usage:#define ubiupdatevol_trivial_usage
122 //usage: "UBI_DEVICE [IMG_FILE]"
123 //usage:#define ubiupdatevol_full_usage "\n\n"
124 //usage: "Update UBI volume\n"
125 //usage: "\n -t Truncate UBI volume"
126 //usage: "\n -s SIZE Bytes in input (if reading stdin)"
129 int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
130 int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
137 int dev_num = UBI_DEV_NUM_AUTO;
138 int vol_id = UBI_VOL_NUM_AUTO;
139 char *vol_name = NULL;
144 opt_complementary = "-1:m+:d+:n+:s+:a+";
145 opts = getopt32(argv, "m:d:n:N:s:a:t::",
146 &mtd_num, &dev_num, &vol_id,
147 &vol_name, &size_bytes, &alignment, &type
149 ubi_ctrl = argv[optind];
151 fd = xopen(ubi_ctrl, O_RDWR);
152 //xfstat(fd, &st, ubi_ctrl);
153 //if (!S_ISCHR(st.st_mode))
154 // bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
157 struct ubi_attach_req req;
158 if (!(opts & OPTION_M))
159 bb_error_msg_and_die("%s device not specified", "MTD");
161 memset(&req, 0, sizeof(req));
162 req.mtd_num = mtd_num;
163 req.ubi_num = dev_num;
165 xioctl(fd, UBI_IOCATT, &req);
168 if (!(opts & OPTION_D))
169 bb_error_msg_and_die("%s device not specified", "UBI");
171 xioctl(fd, UBI_IOCDET, &dev_num);
174 struct ubi_mkvol_req req;
176 if (!(opts & OPTION_s))
177 bb_error_msg_and_die("%s size not specified", "UBI");
178 if (!(opts & OPTION_N))
179 bb_error_msg_and_die("%s name not specified", "UBI");
180 vol_name_len = strlen(vol_name);
181 if (vol_name_len > UBI_MAX_VOLUME_NAME)
182 bb_error_msg_and_die("%s volume name too long", "UBI");
184 memset(&req, 0, sizeof(req));
186 if ((opts & OPTION_t) && type) {
188 req.vol_type = UBI_STATIC_VOLUME;
190 req.vol_type = UBI_DYNAMIC_VOLUME;
192 req.vol_type = UBI_DYNAMIC_VOLUME;
194 req.alignment = alignment;
195 req.bytes = size_bytes;
196 strncpy(req.name, vol_name, UBI_MAX_VOLUME_NAME);
197 req.name_len = vol_name_len;
199 xioctl(fd, UBI_IOCMKVOL, &req);
202 if (!(opts & OPTION_n))
203 bb_error_msg_and_die("%s volume id not specified", "UBI");
205 xioctl(fd, UBI_IOCRMVOL, &vol_id);
208 struct ubi_rsvol_req req;
209 if (!(opts & OPTION_s))
210 bb_error_msg_and_die("%s size not specified", "UBI");
211 if (!(opts & OPTION_n))
212 bb_error_msg_and_die("%s volume id not specified", "UBI");
214 memset(&req, 0, sizeof(req));
215 req.bytes = size_bytes;
218 xioctl(fd, UBI_IOCRSVOL, &req);
223 if (opts & OPTION_t) {
224 // truncate the volume by starting an update for size 0
226 xioctl(fd, UBI_IOCVOLUP, &bytes);
230 char buf[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size") + 2 * sizeof(int)*3];
232 unsigned ubinum, volnum;
237 // Make assumption that device not is in normal format.
238 // Removes need for scanning sysfs tree as full libubi does
239 if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
240 bb_error_msg_and_die("%s volume node not in correct format", "UBI");
242 sprintf(buf, "/sys/class/ubi/ubi%u_%u/usable_eb_size", ubinum, volnum);
243 if (open_read_close(buf, buf, sizeof(buf)) <= 0)
244 bb_error_msg_and_die("%s could not get LEB size", "UBI");
245 if (sscanf(buf, "%u", &leb_size) != 1)
246 bb_error_msg_and_die("%s could not get LEB size", "UBI");
248 if (opts & OPTION_s) {
253 xstat(argv[optind+1], &st);
254 size_bytes = st.st_size;
255 input_fd = xopen(argv[optind+1], O_RDONLY);
259 xioctl(fd, UBI_IOCVOLUP, &bytes);
261 input_data = xmalloc(leb_size);
262 while ((len = full_read(input_fd, input_data, leb_size)) > 0) {
263 xwrite(fd, input_data, len);
266 bb_error_msg_and_die("%s volume update failed", "UBI");
267 if (ENABLE_FEATURE_CLEAN_UP)
272 if (ENABLE_FEATURE_CLEAN_UP)