busybox/libbb/ubi.c
Denys Vlasenko 798b94518e ubi tools: ubiupdatevol supports "-" input and actually respects -s SIZE
Decided to not make any flash applets NOEXEC.
Minor robustifications here and there. Better error messages. Save on strings:

function                                             old     new   delta
ubi_tools_main                                      1235    1288     +53
ubi_get_volid_by_name                                125     133      +8
ubirename_main                                       198     204      +6
get_num_from_file                                     90      94      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 71/0)               Total: 71 bytes
   text	   data	    bss	    dec	    hex	filename
 915696	    485	   6880	 923061	  e15b5	busybox_old
 915670	    485	   6880	 923035	  e159b	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-07 16:00:25 +02:00

45 lines
1.0 KiB
C

/* 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 ubi_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;
buf[UBI_MAX_VOLUME_NAME] = '\0';
strchrnul(buf, '\n')[0] = '\0';
if (strcmp(vol_name, buf) == 0)
return i;
}
bb_error_msg_and_die("volume '%s' not found", vol_name);
}