diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-04-26 04:32:05 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-04-26 04:32:05 +0200 |
commit | d1993f180cd4ba0992c559e7165ec4c220af4ca0 (patch) | |
tree | 2e588170d938e4ef5d79cca2c1da8b64b1247512 /miscutils/ubi_tools.c | |
parent | 0bfb9c2cf0e8b711634014e06d39b5f47c5a95c2 (diff) | |
download | busybox-w32-d1993f180cd4ba0992c559e7165ec4c220af4ca0.tar.gz busybox-w32-d1993f180cd4ba0992c559e7165ec4c220af4ca0.tar.bz2 busybox-w32-d1993f180cd4ba0992c559e7165ec4c220af4ca0.zip |
rename miscutils/ubi_attach_detach.c -> miscutils/ubi_tools.c
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils/ubi_tools.c')
-rw-r--r-- | miscutils/ubi_tools.c | 278 |
1 files changed, 278 insertions, 0 deletions
diff --git a/miscutils/ubi_tools.c b/miscutils/ubi_tools.c new file mode 100644 index 000000000..fc7f38c5d --- /dev/null +++ b/miscutils/ubi_tools.c | |||
@@ -0,0 +1,278 @@ | |||
1 | /* Ported to busybox from mtd-utils. | ||
2 | * | ||
3 | * Licensed under GPLv2, see file LICENSE in this source tree. | ||
4 | */ | ||
5 | |||
6 | //config:config UBIATTACH | ||
7 | //config: bool "ubiattach" | ||
8 | //config: default y | ||
9 | //config: select PLATFORM_LINUX | ||
10 | //config: help | ||
11 | //config: Attach MTD device to an UBI device. | ||
12 | //config: | ||
13 | //config:config UBIDETACH | ||
14 | //config: bool "ubidetach" | ||
15 | //config: default y | ||
16 | //config: select PLATFORM_LINUX | ||
17 | //config: help | ||
18 | //config: Detach MTD device from an UBI device. | ||
19 | //config: | ||
20 | //config:config UBIMKVOL | ||
21 | //config: bool "ubimkvol" | ||
22 | //config: default y | ||
23 | //config: select PLATFORM_LINUX | ||
24 | //config: help | ||
25 | //config: Create a UBI volume. | ||
26 | //config: | ||
27 | //config:config UBIRMVOL | ||
28 | //config: bool "ubirmvol" | ||
29 | //config: default y | ||
30 | //config: select PLATFORM_LINUX | ||
31 | //config: help | ||
32 | //config: Delete a UBI volume. | ||
33 | //config: | ||
34 | //config:config UBIRSVOL | ||
35 | //config: bool "ubirsvol" | ||
36 | //config: default y | ||
37 | //config: select PLATFORM_LINUX | ||
38 | //config: help | ||
39 | //config: Resize a UBI volume. | ||
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. | ||
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)) | ||
53 | //applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol)) | ||
54 | |||
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 | ||
61 | |||
62 | #include "libbb.h" | ||
63 | #include <mtd/ubi-user.h> | ||
64 | |||
65 | #define OPTION_M (1 << 0) | ||
66 | #define OPTION_D (1 << 1) | ||
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) | ||
72 | |||
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') | ||
78 | #define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u') | ||
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" | ||
84 | //usage: "\nOptions:" | ||
85 | //usage: "\n -m MTD_NUM MTD device number to attach" | ||
86 | //usage: "\n -d UBI_NUM UBI device number to assign" | ||
87 | //usage: | ||
88 | //usage:#define ubidetach_trivial_usage | ||
89 | //usage: "-d UBI_NUM UBI_CTRL_DEV" | ||
90 | //usage:#define ubidetach_full_usage "\n\n" | ||
91 | //usage: "Detach MTD device from UBI\n" | ||
92 | //usage: "\nOptions:" | ||
93 | //usage: "\n -d UBI_NUM UBI device number" | ||
94 | //usage: | ||
95 | //usage:#define ubimkvol_trivial_usage | ||
96 | //usage: "UBI_DEVICE -N NAME -s SIZE" | ||
97 | //usage:#define ubimkvol_full_usage "\n\n" | ||
98 | //usage: "Create UBI volume\n" | ||
99 | //usage: "\nOptions:" | ||
100 | //usage: "\n -a ALIGNMENT Volume alignment (default 1)" | ||
101 | //usage: "\n -n VOLID Volume ID, if not specified, it" | ||
102 | //usage: "\n will be assigned automatically" | ||
103 | //usage: "\n -N NAME Volume name" | ||
104 | //usage: "\n -s SIZE Size in bytes" | ||
105 | //usage: "\n -t TYPE Volume type (static|dynamic)" | ||
106 | //usage: | ||
107 | //usage:#define ubirmvol_trivial_usage | ||
108 | //usage: "UBI_DEVICE -n VOLID" | ||
109 | //usage:#define ubirmvol_full_usage "\n\n" | ||
110 | //usage: "Remove UBI volume\n" | ||
111 | //usage: "\nOptions:" | ||
112 | //usage: "\n -n VOLID Volume ID" | ||
113 | //usage: | ||
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: "\nOptions:" | ||
119 | //usage: "\n -n VOLID Volume ID to resize" | ||
120 | //usage: "\n -s SIZE Size in bytes" | ||
121 | //usage: | ||
122 | //usage:#define ubiupdatevol_trivial_usage | ||
123 | //usage: "UBI_DEVICE [IMG_FILE]" | ||
124 | //usage:#define ubiupdatevol_full_usage "\n\n" | ||
125 | //usage: "Update UBI volume\n" | ||
126 | //usage: "\nOptions:" | ||
127 | //usage: "\n -t Truncate UBI volume" | ||
128 | //usage: "\n -s SIZE Bytes in input (if reading stdin)" | ||
129 | |||
130 | |||
131 | int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
132 | int ubi_tools_main(int argc UNUSED_PARAM, char **argv) | ||
133 | { | ||
134 | unsigned opts; | ||
135 | char *ubi_ctrl; | ||
136 | //struct stat st; | ||
137 | int fd; | ||
138 | int mtd_num; | ||
139 | int dev_num = UBI_DEV_NUM_AUTO; | ||
140 | int vol_id = UBI_VOL_NUM_AUTO; | ||
141 | char *vol_name = NULL; | ||
142 | int size_bytes; | ||
143 | int alignment = 1; | ||
144 | char *type = NULL; | ||
145 | |||
146 | opt_complementary = "-1:m+:d+:n+:s+:a+"; | ||
147 | opts = getopt32(argv, "m:d:n:N:s:a:t::", | ||
148 | &mtd_num, &dev_num, &vol_id, | ||
149 | &vol_name, &size_bytes, &alignment, &type | ||
150 | ); | ||
151 | ubi_ctrl = argv[optind]; | ||
152 | |||
153 | fd = xopen(ubi_ctrl, O_RDWR); | ||
154 | //xfstat(fd, &st, ubi_ctrl); | ||
155 | //if (!S_ISCHR(st.st_mode)) | ||
156 | // bb_error_msg_and_die("%s: not a char device", ubi_ctrl); | ||
157 | |||
158 | if (do_attach) { | ||
159 | struct ubi_attach_req req; | ||
160 | if (!(opts & OPTION_M)) | ||
161 | bb_error_msg_and_die("%s device not specified", "MTD"); | ||
162 | |||
163 | memset(&req, 0, sizeof(req)); | ||
164 | req.mtd_num = mtd_num; | ||
165 | req.ubi_num = dev_num; | ||
166 | |||
167 | xioctl(fd, UBI_IOCATT, &req); | ||
168 | } else | ||
169 | if (do_detach) { | ||
170 | if (!(opts & OPTION_D)) | ||
171 | bb_error_msg_and_die("%s device not specified", "UBI"); | ||
172 | |||
173 | xioctl(fd, UBI_IOCDET, &dev_num); | ||
174 | } else | ||
175 | if (do_mkvol) { | ||
176 | struct ubi_mkvol_req req; | ||
177 | int vol_name_len; | ||
178 | if (!(opts & OPTION_s)) | ||
179 | bb_error_msg_and_die("%s size not specified", "UBI"); | ||
180 | if (!(opts & OPTION_N)) | ||
181 | bb_error_msg_and_die("%s name not specified", "UBI"); | ||
182 | vol_name_len = strlen(vol_name); | ||
183 | if (vol_name_len > UBI_MAX_VOLUME_NAME) | ||
184 | bb_error_msg_and_die("%s volume name too long", "UBI"); | ||
185 | |||
186 | memset(&req, 0, sizeof(req)); | ||
187 | req.vol_id = vol_id; | ||
188 | if ((opts & OPTION_t) && type) { | ||
189 | if (type[0] == 's') | ||
190 | req.vol_type = UBI_STATIC_VOLUME; | ||
191 | else | ||
192 | req.vol_type = UBI_DYNAMIC_VOLUME; | ||
193 | } else { | ||
194 | req.vol_type = UBI_DYNAMIC_VOLUME; | ||
195 | } | ||
196 | req.alignment = alignment; | ||
197 | req.bytes = size_bytes; | ||
198 | strncpy(req.name, vol_name, UBI_MAX_VOLUME_NAME); | ||
199 | req.name_len = vol_name_len; | ||
200 | |||
201 | xioctl(fd, UBI_IOCMKVOL, &req); | ||
202 | } else | ||
203 | if (do_rmvol) { | ||
204 | if (!(opts & OPTION_n)) | ||
205 | bb_error_msg_and_die("%s volume id not specified", "UBI"); | ||
206 | |||
207 | xioctl(fd, UBI_IOCRMVOL, &vol_id); | ||
208 | } else | ||
209 | if (do_rsvol) { | ||
210 | struct ubi_rsvol_req req; | ||
211 | if (!(opts & OPTION_s)) | ||
212 | bb_error_msg_and_die("%s size not specified", "UBI"); | ||
213 | if (!(opts & OPTION_n)) | ||
214 | bb_error_msg_and_die("%s volume id not specified", "UBI"); | ||
215 | |||
216 | memset(&req, 0, sizeof(req)); | ||
217 | req.bytes = size_bytes; | ||
218 | req.vol_id = vol_id; | ||
219 | |||
220 | xioctl(fd, UBI_IOCRSVOL, &req); | ||
221 | } else | ||
222 | if (do_update) { | ||
223 | long long bytes; | ||
224 | |||
225 | if (opts & OPTION_t) { | ||
226 | // truncate the volume by starting an update for size 0 | ||
227 | bytes = 0; | ||
228 | xioctl(fd, UBI_IOCVOLUP, &bytes); | ||
229 | } | ||
230 | else { | ||
231 | struct stat st; | ||
232 | char buf[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size") + 2 * sizeof(int)*3]; | ||
233 | int input_fd; | ||
234 | unsigned ubinum, volnum; | ||
235 | unsigned leb_size; | ||
236 | ssize_t len; | ||
237 | char *input_data; | ||
238 | |||
239 | // Make assumption that device not is in normal format. | ||
240 | // Removes need for scanning sysfs tree as full libubi does | ||
241 | if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2) | ||
242 | bb_error_msg_and_die("%s volume node not in correct format", "UBI"); | ||
243 | |||
244 | sprintf(buf, "/sys/class/ubi/ubi%u_%u/usable_eb_size", ubinum, volnum); | ||
245 | if (open_read_close(buf, buf, sizeof(buf)) <= 0) | ||
246 | bb_error_msg_and_die("%s could not get LEB size", "UBI"); | ||
247 | if (sscanf(buf, "%u", &leb_size) != 1) | ||
248 | bb_error_msg_and_die("%s could not get LEB size", "UBI"); | ||
249 | |||
250 | if (opts & OPTION_s) { | ||
251 | input_fd = 0; | ||
252 | } else { | ||
253 | if (!argv[optind+1]) | ||
254 | bb_show_usage(); | ||
255 | xstat(argv[optind+1], &st); | ||
256 | size_bytes = st.st_size; | ||
257 | input_fd = xopen(argv[optind+1], O_RDONLY); | ||
258 | } | ||
259 | |||
260 | bytes = size_bytes; | ||
261 | xioctl(fd, UBI_IOCVOLUP, &bytes); | ||
262 | |||
263 | input_data = xmalloc(leb_size); | ||
264 | while ((len = full_read(input_fd, input_data, leb_size)) > 0) { | ||
265 | xwrite(fd, input_data, len); | ||
266 | } | ||
267 | if (len < 0) | ||
268 | bb_error_msg_and_die("%s volume update failed", "UBI"); | ||
269 | if (ENABLE_FEATURE_CLEAN_UP) | ||
270 | close(input_fd); | ||
271 | } | ||
272 | } | ||
273 | |||
274 | if (ENABLE_FEATURE_CLEAN_UP) | ||
275 | close(fd); | ||
276 | |||
277 | return EXIT_SUCCESS; | ||
278 | } | ||