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>
This commit is contained in:
Sven Eisenberg 2016-04-03 21:53:12 +02:00 committed by Denys Vlasenko
parent cb92640998
commit b068cf2a7e
4 changed files with 69 additions and 31 deletions

View File

@ -1770,6 +1770,9 @@ void bb_progress_update(bb_progress_t *p,
uoff_t transferred,
uoff_t totalsize) FAST_FUNC;
unsigned ubi_devnum_from_devname(const char *str) FAST_FUNC;
int get_volid_by_name(unsigned ubi_devnum, const char *vol_name) FAST_FUNC;
extern const char *applet_name;

43
libbb/ubi.c Normal file
View File

@ -0,0 +1,43 @@
/* vi: set sw=4 ts=4: */
/*
* Utility routines.
*
* Copyright (C) 2016 Denys Vlasenko
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
//kbuild:lib-y += ubi.o
#include "libbb.h"
// from ubi-media.h
#define UBI_MAX_VOLUME_NAME 127
#define UBI_MAX_VOLUMES 128
unsigned FAST_FUNC ubi_devnum_from_devname(const char *str)
{
unsigned ubi_devnum;
if (sscanf(str, "/dev/ubi%u", &ubi_devnum) != 1)
bb_error_msg_and_die("not an UBI device: '%s'", str);
return ubi_devnum;
}
int FAST_FUNC get_volid_by_name(unsigned ubi_devnum, const char *vol_name)
{
unsigned i;
for (i = 0; i < UBI_MAX_VOLUMES; i++) {
char buf[UBI_MAX_VOLUME_NAME + 1];
char fname[sizeof("/sys/class/ubi/ubi%u_%u/name") + 2 * sizeof(int)*3];
sprintf(fname, "/sys/class/ubi/ubi%u_%u/name", ubi_devnum, i);
if (open_read_close(fname, buf, sizeof(buf)) <= 0)
continue;
strchrnul(buf, '\n')[0] = '\0';
if (strcmp(vol_name, buf) == 0)
return i;
}
bb_error_msg_and_die("volume '%s' not found", vol_name);
}

View File

@ -195,7 +195,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
} else
//usage:#define ubimkvol_trivial_usage
//usage: "UBI_DEVICE -N NAME [-s SIZE | -m]"
//usage: "-N NAME [-s SIZE | -m] UBI_DEVICE"
//usage:#define ubimkvol_full_usage "\n\n"
//usage: "Create UBI volume\n"
//usage: "\n -a ALIGNMENT Volume alignment (default 1)"
@ -212,9 +212,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
unsigned num;
char *p;
if (sscanf(ubi_ctrl, "/dev/ubi%u", &num) != 1)
bb_error_msg_and_die("wrong format of UBI device name");
num = ubi_devnum_from_devname(ubi_ctrl);
p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
strcpy(p, "avail_eraseblocks");
@ -248,20 +246,31 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
} else
//usage:#define ubirmvol_trivial_usage
//usage: "UBI_DEVICE -n VOLID"
//usage: "-n VOLID / -N VOLNAME UBI_DEVICE"
//usage:#define ubirmvol_full_usage "\n\n"
//usage: "Remove UBI volume\n"
//usage: "\n -n VOLID Volume ID"
//usage: "\n -N VOLNAME Volume name"
if (do_rmvol) {
if (!(opts & OPTION_n))
if (!(opts & (OPTION_n|OPTION_N)))
bb_error_msg_and_die("volume id not specified");
/* FIXME? kernel expects int32_t* here: */
xioctl(fd, UBI_IOCRMVOL, &vol_id);
if (opts & OPTION_N) {
unsigned num = ubi_devnum_from_devname(ubi_ctrl);
vol_id = get_volid_by_name(num, vol_name);
}
if (sizeof(vol_id) != 4) {
/* kernel expects int32_t* in this ioctl */
int32_t t = vol_id;
xioctl(fd, UBI_IOCRMVOL, &t);
} else {
xioctl(fd, UBI_IOCRMVOL, &vol_id);
}
} else
//usage:#define ubirsvol_trivial_usage
//usage: "UBI_DEVICE -n VOLID -s SIZE"
//usage: "-n VOLID -s SIZE UBI_DEVICE"
//usage:#define ubirsvol_full_usage "\n\n"
//usage: "Resize UBI volume\n"
//usage: "\n -n VOLID Volume ID"
@ -279,7 +288,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
} else
//usage:#define ubiupdatevol_trivial_usage
//usage: "UBI_DEVICE [-t | [-s SIZE] IMG_FILE]"
//usage: "[-t | [-s SIZE] IMG_FILE] UBI_DEVICE"
//usage:#define ubiupdatevol_full_usage "\n\n"
//usage: "Update UBI volume\n"
//usage: "\n -t Truncate to zero size"

View File

@ -30,12 +30,10 @@
#endif
// from ubi-media.h
#define UBI_VOL_NAME_MAX 127
#define UBI_MAX_VOLUME_NAME 127
#define UBI_MAX_VOLUMES 128
// end ubi-media.h
#define UBI_MAX_VOLUME_NAME UBI_VOL_NAME_MAX
// from ubi-user.h
/* ioctl commands of UBI character devices */
#define UBI_IOC_MAGIC 'o'
@ -64,7 +62,7 @@ int ubirename_main(int argc, char **argv)
struct ubi_rnvol_req *rnvol;
const char *ubi_devname;
unsigned ubi_devnum;
unsigned i, n;
unsigned n;
/* argc can be 4, 6, 8, ... */
if ((argc & 1) || (argc >>= 1) < 2)
@ -76,27 +74,12 @@ int ubirename_main(int argc, char **argv)
bb_error_msg_and_die("too many renames requested");
ubi_devname = argv[1];
if (sscanf(ubi_devname, "/dev/ubi%u", &ubi_devnum) != 1)
bb_error_msg_and_die("not a ubi device: '%s'", ubi_devname);
ubi_devnum = ubi_devnum_from_devname(ubi_devname);
n = 0;
argv += 2;
while (argv[0]) {
for (i = 0; i < UBI_MAX_VOLUMES; i++) {
char buf[UBI_VOL_NAME_MAX + 1];
char fname[sizeof("/sys/class/ubi/ubi%u_%u/name") + 2 * sizeof(int)*3];
sprintf(fname, "/sys/class/ubi/ubi%u_%u/name", ubi_devnum, i);
if (open_read_close(fname, buf, sizeof(buf)) <= 0)
continue;
strchrnul(buf, '\n')[0] = '\0';
if (strcmp(buf, argv[0]) == 0)
goto found;
}
bb_error_msg_and_die("no volume '%s' found", argv[0]);
found:
rnvol->ents[n].vol_id = i;
rnvol->ents[n].vol_id = get_volid_by_name(ubi_devnum, argv[0]);
rnvol->ents[n].name_len = strlen(argv[1]);
if (rnvol->ents[n].name_len >= sizeof(rnvol->ents[n].name))
bb_error_msg_and_die("new name '%s' is too long", argv[1]);