Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "ubirename (2.4 kb)" |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: select PLATFORM_LINUX |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: Utility to rename UBI volumes |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 15 | |
| 16 | //applet:IF_UBIRENAME(APPLET(ubirename, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 17 | /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */ |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 18 | |
| 19 | //kbuild:lib-$(CONFIG_UBIRENAME) += ubirename.o |
| 20 | |
| 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" |
| 25 | |
| 26 | #include "libbb.h" |
| 27 | #include <mtd/mtd-user.h> |
| 28 | |
| 29 | #ifndef __packed |
| 30 | # define __packed __attribute__((packed)) |
| 31 | #endif |
| 32 | |
| 33 | // from ubi-media.h |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 34 | #define UBI_MAX_VOLUME_NAME 127 |
Denys Vlasenko | 6aab992 | 2016-04-03 22:24:51 +0200 | [diff] [blame] | 35 | #define UBI_MAX_VOLUMES 128 |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 36 | // end ubi-media.h |
| 37 | |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 38 | // from ubi-user.h |
| 39 | /* ioctl commands of UBI character devices */ |
| 40 | #define UBI_IOC_MAGIC 'o' |
| 41 | |
| 42 | /* Re-name volumes */ |
| 43 | #define UBI_IOCRNVOL _IOW(UBI_IOC_MAGIC, 3, struct ubi_rnvol_req) |
| 44 | |
| 45 | /* Maximum amount of UBI volumes that can be re-named at one go */ |
| 46 | #define UBI_MAX_RNVOL 32 |
| 47 | |
| 48 | struct ubi_rnvol_req { |
| 49 | int32_t count; |
| 50 | int8_t padding1[12]; |
| 51 | struct { |
| 52 | int32_t vol_id; |
| 53 | int16_t name_len; |
| 54 | int8_t padding2[2]; |
| 55 | char name[UBI_MAX_VOLUME_NAME + 1]; |
| 56 | } ents[UBI_MAX_RNVOL]; |
| 57 | } __packed; |
| 58 | // end ubi-user.h |
| 59 | |
| 60 | int ubirename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 61 | int ubirename_main(int argc, char **argv) |
| 62 | { |
| 63 | struct ubi_rnvol_req *rnvol; |
| 64 | const char *ubi_devname; |
| 65 | unsigned ubi_devnum; |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 66 | unsigned n; |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 67 | |
| 68 | /* argc can be 4, 6, 8, ... */ |
| 69 | if ((argc & 1) || (argc >>= 1) < 2) |
| 70 | bb_show_usage(); |
| 71 | |
| 72 | rnvol = xzalloc(sizeof(*rnvol)); |
| 73 | rnvol->count = --argc; |
| 74 | if (argc > ARRAY_SIZE(rnvol->ents)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 75 | bb_simple_error_msg_and_die("too many renames requested"); |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 76 | |
| 77 | ubi_devname = argv[1]; |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 78 | ubi_devnum = ubi_devnum_from_devname(ubi_devname); |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 79 | |
| 80 | n = 0; |
| 81 | argv += 2; |
| 82 | while (argv[0]) { |
Denys Vlasenko | 6aab992 | 2016-04-03 22:24:51 +0200 | [diff] [blame] | 83 | rnvol->ents[n].vol_id = ubi_get_volid_by_name(ubi_devnum, argv[0]); |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 84 | |
| 85 | /* strnlen avoids overflow of 16-bit field (paranoia) */ |
| 86 | rnvol->ents[n].name_len = strnlen(argv[1], sizeof(rnvol->ents[n].name)); |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 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]); |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 89 | |
Sven Eisenberg | cb92640 | 2016-04-03 20:12:03 +0200 | [diff] [blame] | 90 | strcpy(rnvol->ents[n].name, argv[1]); |
| 91 | n++; |
| 92 | argv += 2; |
| 93 | } |
| 94 | |
| 95 | xioctl(xopen(ubi_devname, O_RDONLY), UBI_IOCRNVOL, rnvol); |
| 96 | |
| 97 | return 0; |
| 98 | } |