blob: dd99a44f4e1b5750e29a6d70ea9ad7838a601520 [file] [log] [blame]
Baruch Siach3324c962010-06-16 12:22:58 +02001/* Ported to busybox from mtd-utils.
2 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2, see file LICENSE in this source tree.
Baruch Siach3324c962010-06-16 12:22:58 +02004 */
5
Baruch Siach3324c962010-06-16 12:22:58 +02006//config:config UBIATTACH
7//config: bool "ubiattach"
Reuben Dowle3e2834f2011-02-05 03:18:08 +01008//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +01009//config: select PLATFORM_LINUX
Baruch Siach3324c962010-06-16 12:22:58 +020010//config: help
11//config: Attach MTD device to an UBI device.
12//config:
13//config:config UBIDETACH
14//config: bool "ubidetach"
Reuben Dowle3e2834f2011-02-05 03:18:08 +010015//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010016//config: select PLATFORM_LINUX
Baruch Siach3324c962010-06-16 12:22:58 +020017//config: help
18//config: Detach MTD device from an UBI device.
Reuben Dowle3e2834f2011-02-05 03:18:08 +010019//config:
20//config:config UBIMKVOL
21//config: bool "ubimkvol"
22//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010023//config: select PLATFORM_LINUX
Reuben Dowle3e2834f2011-02-05 03:18:08 +010024//config: help
25//config: Create a UBI volume.
26//config:
27//config:config UBIRMVOL
28//config: bool "ubirmvol"
29//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010030//config: select PLATFORM_LINUX
Reuben Dowle3e2834f2011-02-05 03:18:08 +010031//config: help
32//config: Delete a UBI volume.
33//config:
34//config:config UBIRSVOL
35//config: bool "ubirsvol"
36//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010037//config: select PLATFORM_LINUX
Reuben Dowle3e2834f2011-02-05 03:18:08 +010038//config: help
39//config: Resize a UBI volume.
Reuben Dowlea6108422011-04-26 04:27:48 +020040//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 Dowle3e2834f2011-02-05 03:18:08 +010047
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 Dowlea6108422011-04-26 04:27:48 +020053//applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol))
Reuben Dowle3e2834f2011-02-05 03:18:08 +010054
Denys Vlasenkod1993f12011-04-26 04:32:05 +020055//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 Siach3324c962010-06-16 12:22:58 +020061
62#include "libbb.h"
Denys Vlasenko7fe1e3f2012-03-11 18:04:14 +010063/* Some versions of kernel have broken headers, need this hack */
64#ifndef __packed
65# define __packed __attribute__((packed))
66#endif
Baruch Siach3324c962010-06-16 12:22:58 +020067#include <mtd/ubi-user.h>
68
Denys Vlasenkofb132e42010-10-29 11:46:52 +020069#define OPTION_M (1 << 0)
70#define OPTION_D (1 << 1)
Reuben Dowle3e2834f2011-02-05 03:18:08 +010071#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)
Baruch Siach3324c962010-06-16 12:22:58 +020076
Reuben Dowle3e2834f2011-02-05 03:18:08 +010077#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')
Reuben Dowlea6108422011-04-26 04:27:48 +020082#define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u')
Baruch Siach3324c962010-06-16 12:22:58 +020083
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"
Baruch Siach3324c962010-06-16 12:22:58 +020088//usage: "\n -m MTD_NUM MTD device number to attach"
89//usage: "\n -d UBI_NUM UBI device number to assign"
90//usage:
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"
Baruch Siach3324c962010-06-16 12:22:58 +020095//usage: "\n -d UBI_NUM UBI device number"
Reuben Dowle3e2834f2011-02-05 03:18:08 +010096//usage:
97//usage:#define ubimkvol_trivial_usage
98//usage: "UBI_DEVICE -N NAME -s SIZE"
99//usage:#define ubimkvol_full_usage "\n\n"
Reuben Dowlea6108422011-04-26 04:27:48 +0200100//usage: "Create UBI volume\n"
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100101//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)"
107//usage:
108//usage:#define ubirmvol_trivial_usage
109//usage: "UBI_DEVICE -n VOLID"
110//usage:#define ubirmvol_full_usage "\n\n"
Denys Vlasenko0bfb9c22011-04-26 04:31:03 +0200111//usage: "Remove UBI volume\n"
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100112//usage: "\n -n VOLID Volume ID"
113//usage:
114//usage:#define ubirsvol_trivial_usage
Reuben Dowleab940af2011-04-04 10:55:55 +1200115//usage: "UBI_DEVICE -n VOLID -s SIZE"
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100116//usage:#define ubirsvol_full_usage "\n\n"
Denys Vlasenko0bfb9c22011-04-26 04:31:03 +0200117//usage: "Resize UBI volume\n"
Reuben Dowleab940af2011-04-04 10:55:55 +1200118//usage: "\n -n VOLID Volume ID to resize"
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100119//usage: "\n -s SIZE Size in bytes"
Denys Vlasenko0bfb9c22011-04-26 04:31:03 +0200120//usage:
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"
Denys Vlasenko66426762011-06-05 03:58:28 +0200125//usage: "\n -t Truncate UBI volume"
126//usage: "\n -s SIZE Bytes in input (if reading stdin)"
Denys Vlasenko0bfb9c22011-04-26 04:31:03 +0200127
Baruch Siach3324c962010-06-16 12:22:58 +0200128
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100129int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
130int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
Baruch Siach3324c962010-06-16 12:22:58 +0200131{
132 unsigned opts;
133 char *ubi_ctrl;
134 //struct stat st;
Baruch Siach3324c962010-06-16 12:22:58 +0200135 int fd;
136 int mtd_num;
137 int dev_num = UBI_DEV_NUM_AUTO;
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100138 int vol_id = UBI_VOL_NUM_AUTO;
139 char *vol_name = NULL;
140 int size_bytes;
141 int alignment = 1;
142 char *type = NULL;
Baruch Siach3324c962010-06-16 12:22:58 +0200143
Reuben Dowlea6108422011-04-26 04:27:48 +0200144 opt_complementary = "-1:m+:d+:n+:s+:a+";
145 opts = getopt32(argv, "m:d:n:N:s:a:t::",
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100146 &mtd_num, &dev_num, &vol_id,
147 &vol_name, &size_bytes, &alignment, &type
148 );
Baruch Siach3324c962010-06-16 12:22:58 +0200149 ubi_ctrl = argv[optind];
150
151 fd = xopen(ubi_ctrl, O_RDWR);
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200152 //xfstat(fd, &st, ubi_ctrl);
Baruch Siach3324c962010-06-16 12:22:58 +0200153 //if (!S_ISCHR(st.st_mode))
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200154 // bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
Baruch Siach3324c962010-06-16 12:22:58 +0200155
156 if (do_attach) {
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100157 struct ubi_attach_req req;
Baruch Siach3324c962010-06-16 12:22:58 +0200158 if (!(opts & OPTION_M))
159 bb_error_msg_and_die("%s device not specified", "MTD");
160
161 memset(&req, 0, sizeof(req));
162 req.mtd_num = mtd_num;
163 req.ubi_num = dev_num;
164
165 xioctl(fd, UBI_IOCATT, &req);
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100166 } else
167 if (do_detach) {
Baruch Siach3324c962010-06-16 12:22:58 +0200168 if (!(opts & OPTION_D))
169 bb_error_msg_and_die("%s device not specified", "UBI");
170
171 xioctl(fd, UBI_IOCDET, &dev_num);
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100172 } else
173 if (do_mkvol) {
174 struct ubi_mkvol_req req;
175 int vol_name_len;
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");
183
184 memset(&req, 0, sizeof(req));
185 req.vol_id = vol_id;
Reuben Dowlea6108422011-04-26 04:27:48 +0200186 if ((opts & OPTION_t) && type) {
Reuben Dowle3e2834f2011-02-05 03:18:08 +0100187 if (type[0] == 's')
188 req.vol_type = UBI_STATIC_VOLUME;
189 else
190 req.vol_type = UBI_DYNAMIC_VOLUME;
191 } else {
192 req.vol_type = UBI_DYNAMIC_VOLUME;
193 }
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;
198
199 xioctl(fd, UBI_IOCMKVOL, &req);
200 } else
201 if (do_rmvol) {
202 if (!(opts & OPTION_n))
203 bb_error_msg_and_die("%s volume id not specified", "UBI");
204
205 xioctl(fd, UBI_IOCRMVOL, &vol_id);
206 } else
207 if (do_rsvol) {
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");
213
214 memset(&req, 0, sizeof(req));
215 req.bytes = size_bytes;
216 req.vol_id = vol_id;
217
218 xioctl(fd, UBI_IOCRSVOL, &req);
Reuben Dowlea6108422011-04-26 04:27:48 +0200219 } else
220 if (do_update) {
221 long long bytes;
222
223 if (opts & OPTION_t) {
224 // truncate the volume by starting an update for size 0
225 bytes = 0;
226 xioctl(fd, UBI_IOCVOLUP, &bytes);
227 }
228 else {
229 struct stat st;
230 char buf[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size") + 2 * sizeof(int)*3];
231 int input_fd;
232 unsigned ubinum, volnum;
233 unsigned leb_size;
234 ssize_t len;
235 char *input_data;
236
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");
241
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");
247
248 if (opts & OPTION_s) {
249 input_fd = 0;
250 } else {
251 if (!argv[optind+1])
252 bb_show_usage();
253 xstat(argv[optind+1], &st);
254 size_bytes = st.st_size;
255 input_fd = xopen(argv[optind+1], O_RDONLY);
256 }
257
258 bytes = size_bytes;
259 xioctl(fd, UBI_IOCVOLUP, &bytes);
260
261 input_data = xmalloc(leb_size);
262 while ((len = full_read(input_fd, input_data, leb_size)) > 0) {
263 xwrite(fd, input_data, len);
264 }
265 if (len < 0)
266 bb_error_msg_and_die("%s volume update failed", "UBI");
267 if (ENABLE_FEATURE_CLEAN_UP)
268 close(input_fd);
269 }
Baruch Siach3324c962010-06-16 12:22:58 +0200270 }
271
272 if (ENABLE_FEATURE_CLEAN_UP)
273 close(fd);
274
275 return EXIT_SUCCESS;
276}