Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 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 | |
| 11 | #include "libbb.h" |
| 12 | |
| 13 | // from ubi-media.h |
| 14 | #define UBI_MAX_VOLUME_NAME 127 |
| 15 | #define UBI_MAX_VOLUMES 128 |
| 16 | |
| 17 | unsigned FAST_FUNC ubi_devnum_from_devname(const char *str) |
| 18 | { |
Denys Vlasenko | 6aab992 | 2016-04-03 22:24:51 +0200 | [diff] [blame] | 19 | unsigned ubi_devnum; |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 20 | |
Denys Vlasenko | 6aab992 | 2016-04-03 22:24:51 +0200 | [diff] [blame] | 21 | if (sscanf(str, "/dev/ubi%u", &ubi_devnum) != 1) |
| 22 | bb_error_msg_and_die("not an UBI device: '%s'", str); |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 23 | return ubi_devnum; |
| 24 | } |
| 25 | |
Denys Vlasenko | 6aab992 | 2016-04-03 22:24:51 +0200 | [diff] [blame] | 26 | int FAST_FUNC ubi_get_volid_by_name(unsigned ubi_devnum, const char *vol_name) |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 27 | { |
| 28 | unsigned i; |
| 29 | |
| 30 | for (i = 0; i < UBI_MAX_VOLUMES; i++) { |
| 31 | char buf[UBI_MAX_VOLUME_NAME + 1]; |
| 32 | char fname[sizeof("/sys/class/ubi/ubi%u_%u/name") + 2 * sizeof(int)*3]; |
| 33 | |
| 34 | sprintf(fname, "/sys/class/ubi/ubi%u_%u/name", ubi_devnum, i); |
| 35 | if (open_read_close(fname, buf, sizeof(buf)) <= 0) |
| 36 | continue; |
| 37 | |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 38 | buf[UBI_MAX_VOLUME_NAME] = '\0'; |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 39 | strchrnul(buf, '\n')[0] = '\0'; |
| 40 | if (strcmp(vol_name, buf) == 0) |
| 41 | return i; |
| 42 | } |
| 43 | bb_error_msg_and_die("volume '%s' not found", vol_name); |
| 44 | } |