diff options
author | Sven Eisenberg <sven.eisenberg@novero.com> | 2016-04-03 20:12:03 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-04-03 20:12:03 +0200 |
commit | cb9264099822505dc2930cfea0b1f9027a02dc06 (patch) | |
tree | a7dd9147a69dc4ed608067ac5036ee75c0ab43bc | |
parent | 46b494635e6ef54e282749d2c65dd1922b167931 (diff) | |
download | busybox-w32-cb9264099822505dc2930cfea0b1f9027a02dc06.tar.gz busybox-w32-cb9264099822505dc2930cfea0b1f9027a02dc06.tar.bz2 busybox-w32-cb9264099822505dc2930cfea0b1f9027a02dc06.zip |
ubirename: new applet
function old new delta
ubirename_main - 394 +394
packed_usage 30611 30674 +63
applet_names 2530 2540 +10
Signed-off-by: Sven Eisenberg <sven.eisenberg@novero.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/ubirename.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/miscutils/ubirename.c b/miscutils/ubirename.c new file mode 100644 index 000000000..455a49485 --- /dev/null +++ b/miscutils/ubirename.c | |||
@@ -0,0 +1,111 @@ | |||
1 | /* ubirename - port of the ubirename from the mtd-utils package | ||
2 | * | ||
3 | * A utility to rename one UBI volume. | ||
4 | * | ||
5 | * 2016-03-01 Sven Eisenberg <sven.eisenberg@novero.com> | ||
6 | * | ||
7 | * Licensed under GPLv2, see file LICENSE in this source tree. | ||
8 | */ | ||
9 | //config:config UBIRENAME | ||
10 | //config: bool "ubirename" | ||
11 | //config: default y | ||
12 | //config: select PLATFORM_LINUX | ||
13 | //config: help | ||
14 | //config: Utility to rename UBI volumes | ||
15 | |||
16 | //applet:IF_UBIRENAME(APPLET(ubirename, BB_DIR_USR_SBIN, BB_SUID_DROP)) | ||
17 | |||
18 | //kbuild:lib-$(CONFIG_UBIRENAME) += ubirename.o | ||
19 | |||
20 | //usage:#define ubirename_trivial_usage | ||
21 | //usage: "UBI_DEVICE OLD_VOLNAME NEW_VOLNAME [OLD2 NEW2]..." | ||
22 | //usage:#define ubirename_full_usage "\n\n" | ||
23 | //usage: "Rename UBI volumes on UBI_DEVICE" | ||
24 | |||
25 | #include "libbb.h" | ||
26 | #include <mtd/mtd-user.h> | ||
27 | |||
28 | #ifndef __packed | ||
29 | # define __packed __attribute__((packed)) | ||
30 | #endif | ||
31 | |||
32 | // from ubi-media.h | ||
33 | #define UBI_VOL_NAME_MAX 127 | ||
34 | #define UBI_MAX_VOLUMES 128 | ||
35 | // end ubi-media.h | ||
36 | |||
37 | #define UBI_MAX_VOLUME_NAME UBI_VOL_NAME_MAX | ||
38 | |||
39 | // from ubi-user.h | ||
40 | /* ioctl commands of UBI character devices */ | ||
41 | #define UBI_IOC_MAGIC 'o' | ||
42 | |||
43 | /* Re-name volumes */ | ||
44 | #define UBI_IOCRNVOL _IOW(UBI_IOC_MAGIC, 3, struct ubi_rnvol_req) | ||
45 | |||
46 | /* Maximum amount of UBI volumes that can be re-named at one go */ | ||
47 | #define UBI_MAX_RNVOL 32 | ||
48 | |||
49 | struct ubi_rnvol_req { | ||
50 | int32_t count; | ||
51 | int8_t padding1[12]; | ||
52 | struct { | ||
53 | int32_t vol_id; | ||
54 | int16_t name_len; | ||
55 | int8_t padding2[2]; | ||
56 | char name[UBI_MAX_VOLUME_NAME + 1]; | ||
57 | } ents[UBI_MAX_RNVOL]; | ||
58 | } __packed; | ||
59 | // end ubi-user.h | ||
60 | |||
61 | int ubirename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
62 | int ubirename_main(int argc, char **argv) | ||
63 | { | ||
64 | struct ubi_rnvol_req *rnvol; | ||
65 | const char *ubi_devname; | ||
66 | unsigned ubi_devnum; | ||
67 | unsigned i, n; | ||
68 | |||
69 | /* argc can be 4, 6, 8, ... */ | ||
70 | if ((argc & 1) || (argc >>= 1) < 2) | ||
71 | bb_show_usage(); | ||
72 | |||
73 | rnvol = xzalloc(sizeof(*rnvol)); | ||
74 | rnvol->count = --argc; | ||
75 | if (argc > ARRAY_SIZE(rnvol->ents)) | ||
76 | bb_error_msg_and_die("too many renames requested"); | ||
77 | |||
78 | ubi_devname = argv[1]; | ||
79 | if (sscanf(ubi_devname, "/dev/ubi%u", &ubi_devnum) != 1) | ||
80 | bb_error_msg_and_die("not a ubi device: '%s'", ubi_devname); | ||
81 | |||
82 | n = 0; | ||
83 | argv += 2; | ||
84 | while (argv[0]) { | ||
85 | for (i = 0; i < UBI_MAX_VOLUMES; i++) { | ||
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]); | ||
101 | 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]); | ||
103 | strcpy(rnvol->ents[n].name, argv[1]); | ||
104 | n++; | ||
105 | argv += 2; | ||
106 | } | ||
107 | |||
108 | xioctl(xopen(ubi_devname, O_RDONLY), UBI_IOCRNVOL, rnvol); | ||
109 | |||
110 | return 0; | ||
111 | } | ||