3
0

ubi.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2016 Denys Vlasenko
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //kbuild:lib-y += ubi.o
  10. #include "libbb.h"
  11. // from ubi-media.h
  12. #define UBI_MAX_VOLUME_NAME 127
  13. #define UBI_MAX_VOLUMES 128
  14. unsigned FAST_FUNC ubi_devnum_from_devname(const char *str)
  15. {
  16. unsigned ubi_devnum;
  17. if (sscanf(str, "/dev/ubi%u", &ubi_devnum) != 1)
  18. bb_error_msg_and_die("not an UBI device: '%s'", str);
  19. return ubi_devnum;
  20. }
  21. int FAST_FUNC ubi_get_volid_by_name(unsigned ubi_devnum, const char *vol_name)
  22. {
  23. unsigned i;
  24. for (i = 0; i < UBI_MAX_VOLUMES; i++) {
  25. char buf[UBI_MAX_VOLUME_NAME + 1];
  26. char fname[sizeof("/sys/class/ubi/ubi%u_%u/name") + 2 * sizeof(int)*3];
  27. sprintf(fname, "/sys/class/ubi/ubi%u_%u/name", ubi_devnum, i);
  28. if (open_read_close(fname, buf, sizeof(buf)) <= 0)
  29. continue;
  30. buf[UBI_MAX_VOLUME_NAME] = '\0';
  31. strchrnul(buf, '\n')[0] = '\0';
  32. if (strcmp(vol_name, buf) == 0)
  33. return i;
  34. }
  35. bb_error_msg_and_die("volume '%s' not found", vol_name);
  36. }