blob: 06a0adacfa8dea2c8eda4cce814bf0f1c797aae0 [file] [log] [blame]
Sven Eisenbergcb926402016-04-03 20:12:03 +02001/* 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
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "ubirename (2.4 kb)"
Sven Eisenbergcb926402016-04-03 20:12:03 +020011//config: default y
Sven Eisenbergcb926402016-04-03 20:12:03 +020012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Utility to rename UBI volumes
Sven Eisenbergcb926402016-04-03 20:12:03 +020014
15//applet:IF_UBIRENAME(APPLET(ubirename, BB_DIR_USR_SBIN, BB_SUID_DROP))
Denys Vlasenko798b9452017-08-07 16:00:25 +020016/* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
Sven Eisenbergcb926402016-04-03 20:12:03 +020017
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
Sven Eisenbergb068cf22016-04-03 21:53:12 +020033#define UBI_MAX_VOLUME_NAME 127
Denys Vlasenko6aab9922016-04-03 22:24:51 +020034#define UBI_MAX_VOLUMES 128
Sven Eisenbergcb926402016-04-03 20:12:03 +020035// end ubi-media.h
36
Sven Eisenbergcb926402016-04-03 20:12:03 +020037// from ubi-user.h
38/* ioctl commands of UBI character devices */
39#define UBI_IOC_MAGIC 'o'
40
41/* Re-name volumes */
42#define UBI_IOCRNVOL _IOW(UBI_IOC_MAGIC, 3, struct ubi_rnvol_req)
43
44/* Maximum amount of UBI volumes that can be re-named at one go */
45#define UBI_MAX_RNVOL 32
46
47struct ubi_rnvol_req {
48 int32_t count;
49 int8_t padding1[12];
50 struct {
51 int32_t vol_id;
52 int16_t name_len;
53 int8_t padding2[2];
54 char name[UBI_MAX_VOLUME_NAME + 1];
55 } ents[UBI_MAX_RNVOL];
56} __packed;
57// end ubi-user.h
58
59int ubirename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60int ubirename_main(int argc, char **argv)
61{
62 struct ubi_rnvol_req *rnvol;
63 const char *ubi_devname;
64 unsigned ubi_devnum;
Sven Eisenbergb068cf22016-04-03 21:53:12 +020065 unsigned n;
Sven Eisenbergcb926402016-04-03 20:12:03 +020066
67 /* argc can be 4, 6, 8, ... */
68 if ((argc & 1) || (argc >>= 1) < 2)
69 bb_show_usage();
70
71 rnvol = xzalloc(sizeof(*rnvol));
72 rnvol->count = --argc;
73 if (argc > ARRAY_SIZE(rnvol->ents))
James Byrne69374872019-07-02 11:35:03 +020074 bb_simple_error_msg_and_die("too many renames requested");
Sven Eisenbergcb926402016-04-03 20:12:03 +020075
76 ubi_devname = argv[1];
Sven Eisenbergb068cf22016-04-03 21:53:12 +020077 ubi_devnum = ubi_devnum_from_devname(ubi_devname);
Sven Eisenbergcb926402016-04-03 20:12:03 +020078
79 n = 0;
80 argv += 2;
81 while (argv[0]) {
Denys Vlasenko6aab9922016-04-03 22:24:51 +020082 rnvol->ents[n].vol_id = ubi_get_volid_by_name(ubi_devnum, argv[0]);
Denys Vlasenko798b9452017-08-07 16:00:25 +020083
84 /* strnlen avoids overflow of 16-bit field (paranoia) */
85 rnvol->ents[n].name_len = strnlen(argv[1], sizeof(rnvol->ents[n].name));
Sven Eisenbergcb926402016-04-03 20:12:03 +020086 if (rnvol->ents[n].name_len >= sizeof(rnvol->ents[n].name))
87 bb_error_msg_and_die("new name '%s' is too long", argv[1]);
Denys Vlasenko798b9452017-08-07 16:00:25 +020088
Sven Eisenbergcb926402016-04-03 20:12:03 +020089 strcpy(rnvol->ents[n].name, argv[1]);
90 n++;
91 argv += 2;
92 }
93
94 xioctl(xopen(ubi_devname, O_RDONLY), UBI_IOCRNVOL, rnvol);
95
96 return 0;
97}