From b068cf2a7e036da8d0b3533b41886c5605c8139d Mon Sep 17 00:00:00 2001 From: Sven Eisenberg Date: Sun, 3 Apr 2016 21:53:12 +0200 Subject: 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 --- libbb/ubi.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 libbb/ubi.c (limited to 'libbb/ubi.c') diff --git a/libbb/ubi.c b/libbb/ubi.c new file mode 100644 index 000000000..7d3b2952d --- /dev/null +++ b/libbb/ubi.c @@ -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); +} -- cgit v1.2.3