1 /* ubirename - port of the ubirename from the mtd-utils package
3 * A utility to rename one UBI volume.
5 * 2016-03-01 Sven Eisenberg <sven.eisenberg@novero.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
9 //config:config UBIRENAME
10 //config: bool "ubirename (2.2 kb)"
12 //config: select PLATFORM_LINUX
14 //config: Utility to rename UBI volumes
16 //applet:IF_UBIRENAME(APPLET(ubirename, BB_DIR_USR_SBIN, BB_SUID_DROP))
17 /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
19 //kbuild:lib-$(CONFIG_UBIRENAME) += ubirename.o
21 //usage:#define ubirename_trivial_usage
22 //usage: "UBI_DEVICE OLD_VOLNAME NEW_VOLNAME [OLD2 NEW2]..."
23 //usage:#define ubirename_full_usage "\n\n"
24 //usage: "Rename UBI volumes on UBI_DEVICE"
27 #include <mtd/mtd-user.h>
30 # define __packed __attribute__((packed))
34 #define UBI_MAX_VOLUME_NAME 127
35 #define UBI_MAX_VOLUMES 128
39 /* ioctl commands of UBI character devices */
40 #define UBI_IOC_MAGIC 'o'
43 #define UBI_IOCRNVOL _IOW(UBI_IOC_MAGIC, 3, struct ubi_rnvol_req)
45 /* Maximum amount of UBI volumes that can be re-named at one go */
46 #define UBI_MAX_RNVOL 32
48 struct ubi_rnvol_req {
55 char name[UBI_MAX_VOLUME_NAME + 1];
56 } ents[UBI_MAX_RNVOL];
60 int ubirename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
61 int ubirename_main(int argc, char **argv)
63 struct ubi_rnvol_req *rnvol;
64 const char *ubi_devname;
68 /* argc can be 4, 6, 8, ... */
69 if ((argc & 1) || (argc >>= 1) < 2)
72 rnvol = xzalloc(sizeof(*rnvol));
73 rnvol->count = --argc;
74 if (argc > ARRAY_SIZE(rnvol->ents))
75 bb_error_msg_and_die("too many renames requested");
77 ubi_devname = argv[1];
78 ubi_devnum = ubi_devnum_from_devname(ubi_devname);
83 rnvol->ents[n].vol_id = ubi_get_volid_by_name(ubi_devnum, argv[0]);
85 /* strnlen avoids overflow of 16-bit field (paranoia) */
86 rnvol->ents[n].name_len = strnlen(argv[1], sizeof(rnvol->ents[n].name));
87 if (rnvol->ents[n].name_len >= sizeof(rnvol->ents[n].name))
88 bb_error_msg_and_die("new name '%s' is too long", argv[1]);
90 strcpy(rnvol->ents[n].name, argv[1]);
95 xioctl(xopen(ubi_devname, O_RDONLY), UBI_IOCRNVOL, rnvol);