diff options
author | Sven Eisenberg <sven.eisenberg@novero.com> | 2016-04-03 21:53:12 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-04-03 22:01:43 +0200 |
commit | b068cf2a7e036da8d0b3533b41886c5605c8139d (patch) | |
tree | 0c29bccfabd054c5cafc5dd68e9d161d70bf1286 | |
parent | cb9264099822505dc2930cfea0b1f9027a02dc06 (diff) | |
download | busybox-w32-b068cf2a7e036da8d0b3533b41886c5605c8139d.tar.gz busybox-w32-b068cf2a7e036da8d0b3533b41886c5605c8139d.tar.bz2 busybox-w32-b068cf2a7e036da8d0b3533b41886c5605c8139d.zip |
ubirmvol: Implement -N switch for ubirmvol
function old new delta
get_volid_by_name - 125 +125
ubi_devnum_from_devname - 43 +43
ubi_tools_main 1215 1220 +5
packed_usage 30674 30655 -19
ubirename_main 394 221 -173
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 1/2 up/down: 173/-192) Total: -19 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 3 | ||||
-rw-r--r-- | libbb/ubi.c | 43 | ||||
-rw-r--r-- | miscutils/ubi_tools.c | 29 | ||||
-rw-r--r-- | miscutils/ubirename.c | 25 |
4 files changed, 69 insertions, 31 deletions
diff --git a/include/libbb.h b/include/libbb.h index 64e61cd26..35c28df51 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1770,6 +1770,9 @@ void bb_progress_update(bb_progress_t *p, | |||
1770 | uoff_t transferred, | 1770 | uoff_t transferred, |
1771 | uoff_t totalsize) FAST_FUNC; | 1771 | uoff_t totalsize) FAST_FUNC; |
1772 | 1772 | ||
1773 | unsigned ubi_devnum_from_devname(const char *str) FAST_FUNC; | ||
1774 | int get_volid_by_name(unsigned ubi_devnum, const char *vol_name) FAST_FUNC; | ||
1775 | |||
1773 | 1776 | ||
1774 | extern const char *applet_name; | 1777 | extern const char *applet_name; |
1775 | 1778 | ||
diff --git a/libbb/ubi.c b/libbb/ubi.c new file mode 100644 index 000000000..7d3b2952d --- /dev/null +++ b/libbb/ubi.c | |||
@@ -0,0 +1,43 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) 2016 Denys Vlasenko | ||
6 | * | ||
7 | * Licensed under GPLv2, see file LICENSE in this source tree. | ||
8 | */ | ||
9 | //kbuild:lib-y += ubi.o | ||
10 | |||
11 | #include "libbb.h" | ||
12 | |||
13 | // from ubi-media.h | ||
14 | #define UBI_MAX_VOLUME_NAME 127 | ||
15 | #define UBI_MAX_VOLUMES 128 | ||
16 | |||
17 | unsigned FAST_FUNC ubi_devnum_from_devname(const char *str) | ||
18 | { | ||
19 | unsigned ubi_devnum; | ||
20 | |||
21 | if (sscanf(str, "/dev/ubi%u", &ubi_devnum) != 1) | ||
22 | bb_error_msg_and_die("not an UBI device: '%s'", str); | ||
23 | return ubi_devnum; | ||
24 | } | ||
25 | |||
26 | int FAST_FUNC get_volid_by_name(unsigned ubi_devnum, const char *vol_name) | ||
27 | { | ||
28 | unsigned i; | ||
29 | |||
30 | for (i = 0; i < UBI_MAX_VOLUMES; i++) { | ||
31 | char buf[UBI_MAX_VOLUME_NAME + 1]; | ||
32 | char fname[sizeof("/sys/class/ubi/ubi%u_%u/name") + 2 * sizeof(int)*3]; | ||
33 | |||
34 | sprintf(fname, "/sys/class/ubi/ubi%u_%u/name", ubi_devnum, i); | ||
35 | if (open_read_close(fname, buf, sizeof(buf)) <= 0) | ||
36 | continue; | ||
37 | |||
38 | strchrnul(buf, '\n')[0] = '\0'; | ||
39 | if (strcmp(vol_name, buf) == 0) | ||
40 | return i; | ||
41 | } | ||
42 | bb_error_msg_and_die("volume '%s' not found", vol_name); | ||
43 | } | ||
diff --git a/miscutils/ubi_tools.c b/miscutils/ubi_tools.c index dd1bda300..ac0c56d6b 100644 --- a/miscutils/ubi_tools.c +++ b/miscutils/ubi_tools.c | |||
@@ -195,7 +195,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) | |||
195 | } else | 195 | } else |
196 | 196 | ||
197 | //usage:#define ubimkvol_trivial_usage | 197 | //usage:#define ubimkvol_trivial_usage |
198 | //usage: "UBI_DEVICE -N NAME [-s SIZE | -m]" | 198 | //usage: "-N NAME [-s SIZE | -m] UBI_DEVICE" |
199 | //usage:#define ubimkvol_full_usage "\n\n" | 199 | //usage:#define ubimkvol_full_usage "\n\n" |
200 | //usage: "Create UBI volume\n" | 200 | //usage: "Create UBI volume\n" |
201 | //usage: "\n -a ALIGNMENT Volume alignment (default 1)" | 201 | //usage: "\n -a ALIGNMENT Volume alignment (default 1)" |
@@ -212,9 +212,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) | |||
212 | unsigned num; | 212 | unsigned num; |
213 | char *p; | 213 | char *p; |
214 | 214 | ||
215 | if (sscanf(ubi_ctrl, "/dev/ubi%u", &num) != 1) | 215 | num = ubi_devnum_from_devname(ubi_ctrl); |
216 | bb_error_msg_and_die("wrong format of UBI device name"); | ||
217 | |||
218 | p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num); | 216 | p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num); |
219 | 217 | ||
220 | strcpy(p, "avail_eraseblocks"); | 218 | strcpy(p, "avail_eraseblocks"); |
@@ -248,20 +246,31 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) | |||
248 | } else | 246 | } else |
249 | 247 | ||
250 | //usage:#define ubirmvol_trivial_usage | 248 | //usage:#define ubirmvol_trivial_usage |
251 | //usage: "UBI_DEVICE -n VOLID" | 249 | //usage: "-n VOLID / -N VOLNAME UBI_DEVICE" |
252 | //usage:#define ubirmvol_full_usage "\n\n" | 250 | //usage:#define ubirmvol_full_usage "\n\n" |
253 | //usage: "Remove UBI volume\n" | 251 | //usage: "Remove UBI volume\n" |
254 | //usage: "\n -n VOLID Volume ID" | 252 | //usage: "\n -n VOLID Volume ID" |
253 | //usage: "\n -N VOLNAME Volume name" | ||
255 | if (do_rmvol) { | 254 | if (do_rmvol) { |
256 | if (!(opts & OPTION_n)) | 255 | if (!(opts & (OPTION_n|OPTION_N))) |
257 | bb_error_msg_and_die("volume id not specified"); | 256 | bb_error_msg_and_die("volume id not specified"); |
258 | 257 | ||
259 | /* FIXME? kernel expects int32_t* here: */ | 258 | if (opts & OPTION_N) { |
260 | xioctl(fd, UBI_IOCRMVOL, &vol_id); | 259 | unsigned num = ubi_devnum_from_devname(ubi_ctrl); |
260 | vol_id = get_volid_by_name(num, vol_name); | ||
261 | } | ||
262 | |||
263 | if (sizeof(vol_id) != 4) { | ||
264 | /* kernel expects int32_t* in this ioctl */ | ||
265 | int32_t t = vol_id; | ||
266 | xioctl(fd, UBI_IOCRMVOL, &t); | ||
267 | } else { | ||
268 | xioctl(fd, UBI_IOCRMVOL, &vol_id); | ||
269 | } | ||
261 | } else | 270 | } else |
262 | 271 | ||
263 | //usage:#define ubirsvol_trivial_usage | 272 | //usage:#define ubirsvol_trivial_usage |
264 | //usage: "UBI_DEVICE -n VOLID -s SIZE" | 273 | //usage: "-n VOLID -s SIZE UBI_DEVICE" |
265 | //usage:#define ubirsvol_full_usage "\n\n" | 274 | //usage:#define ubirsvol_full_usage "\n\n" |
266 | //usage: "Resize UBI volume\n" | 275 | //usage: "Resize UBI volume\n" |
267 | //usage: "\n -n VOLID Volume ID" | 276 | //usage: "\n -n VOLID Volume ID" |
@@ -279,7 +288,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) | |||
279 | } else | 288 | } else |
280 | 289 | ||
281 | //usage:#define ubiupdatevol_trivial_usage | 290 | //usage:#define ubiupdatevol_trivial_usage |
282 | //usage: "UBI_DEVICE [-t | [-s SIZE] IMG_FILE]" | 291 | //usage: "[-t | [-s SIZE] IMG_FILE] UBI_DEVICE" |
283 | //usage:#define ubiupdatevol_full_usage "\n\n" | 292 | //usage:#define ubiupdatevol_full_usage "\n\n" |
284 | //usage: "Update UBI volume\n" | 293 | //usage: "Update UBI volume\n" |
285 | //usage: "\n -t Truncate to zero size" | 294 | //usage: "\n -t Truncate to zero size" |
diff --git a/miscutils/ubirename.c b/miscutils/ubirename.c index 455a49485..d6ccfcb10 100644 --- a/miscutils/ubirename.c +++ b/miscutils/ubirename.c | |||
@@ -30,12 +30,10 @@ | |||
30 | #endif | 30 | #endif |
31 | 31 | ||
32 | // from ubi-media.h | 32 | // from ubi-media.h |
33 | #define UBI_VOL_NAME_MAX 127 | 33 | #define UBI_MAX_VOLUME_NAME 127 |
34 | #define UBI_MAX_VOLUMES 128 | 34 | #define UBI_MAX_VOLUMES 128 |
35 | // end ubi-media.h | 35 | // end ubi-media.h |
36 | 36 | ||
37 | #define UBI_MAX_VOLUME_NAME UBI_VOL_NAME_MAX | ||
38 | |||
39 | // from ubi-user.h | 37 | // from ubi-user.h |
40 | /* ioctl commands of UBI character devices */ | 38 | /* ioctl commands of UBI character devices */ |
41 | #define UBI_IOC_MAGIC 'o' | 39 | #define UBI_IOC_MAGIC 'o' |
@@ -64,7 +62,7 @@ int ubirename_main(int argc, char **argv) | |||
64 | struct ubi_rnvol_req *rnvol; | 62 | struct ubi_rnvol_req *rnvol; |
65 | const char *ubi_devname; | 63 | const char *ubi_devname; |
66 | unsigned ubi_devnum; | 64 | unsigned ubi_devnum; |
67 | unsigned i, n; | 65 | unsigned n; |
68 | 66 | ||
69 | /* argc can be 4, 6, 8, ... */ | 67 | /* argc can be 4, 6, 8, ... */ |
70 | if ((argc & 1) || (argc >>= 1) < 2) | 68 | if ((argc & 1) || (argc >>= 1) < 2) |
@@ -76,27 +74,12 @@ int ubirename_main(int argc, char **argv) | |||
76 | bb_error_msg_and_die("too many renames requested"); | 74 | bb_error_msg_and_die("too many renames requested"); |
77 | 75 | ||
78 | ubi_devname = argv[1]; | 76 | ubi_devname = argv[1]; |
79 | if (sscanf(ubi_devname, "/dev/ubi%u", &ubi_devnum) != 1) | 77 | ubi_devnum = ubi_devnum_from_devname(ubi_devname); |
80 | bb_error_msg_and_die("not a ubi device: '%s'", ubi_devname); | ||
81 | 78 | ||
82 | n = 0; | 79 | n = 0; |
83 | argv += 2; | 80 | argv += 2; |
84 | while (argv[0]) { | 81 | while (argv[0]) { |
85 | for (i = 0; i < UBI_MAX_VOLUMES; i++) { | 82 | rnvol->ents[n].vol_id = get_volid_by_name(ubi_devnum, argv[0]); |
86 | char buf[UBI_VOL_NAME_MAX + 1]; | ||
87 | char fname[sizeof("/sys/class/ubi/ubi%u_%u/name") + 2 * sizeof(int)*3]; | ||
88 | |||
89 | sprintf(fname, "/sys/class/ubi/ubi%u_%u/name", ubi_devnum, i); | ||
90 | if (open_read_close(fname, buf, sizeof(buf)) <= 0) | ||
91 | continue; | ||
92 | |||
93 | strchrnul(buf, '\n')[0] = '\0'; | ||
94 | if (strcmp(buf, argv[0]) == 0) | ||
95 | goto found; | ||
96 | } | ||
97 | bb_error_msg_and_die("no volume '%s' found", argv[0]); | ||
98 | found: | ||
99 | rnvol->ents[n].vol_id = i; | ||
100 | rnvol->ents[n].name_len = strlen(argv[1]); | 83 | rnvol->ents[n].name_len = strlen(argv[1]); |
101 | if (rnvol->ents[n].name_len >= sizeof(rnvol->ents[n].name)) | 84 | if (rnvol->ents[n].name_len >= sizeof(rnvol->ents[n].name)) |
102 | bb_error_msg_and_die("new name '%s' is too long", argv[1]); | 85 | bb_error_msg_and_die("new name '%s' is too long", argv[1]); |