2 * Copyright (c) International Business Machines Corp., 2006
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Author: Artem Bityutskiy
20 * UBI (Unsorted Block Images) library.
23 #define PROGRAM_NAME "libubi"
25 #include <sys/sysmacros.h>
33 #include <sys/ioctl.h>
35 #include <sys/types.h>
37 #include "libubi_int.h"
40 * mkpath - compose full path from 2 given components.
41 * @path: the first component
42 * @name: the second component
44 * This function returns the resulting path in case of success and %NULL in
47 static char *mkpath(const char *path, const char *name)
50 int len1 = strlen(path);
51 int len2 = strlen(name);
53 n = malloc(len1 + len2 + 2);
55 sys_errmsg("cannot allocate %d bytes", len1 + len2 + 2);
59 memcpy(n, path, len1);
60 if (n[len1 - 1] != '/')
63 memcpy(n + len1, name, len2 + 1);
68 * read_positive_ll - read a positive 'long long' value from a file.
69 * @file: the file to read from
70 * @value: the result is stored here
72 * This function reads file @file and interprets its contents as a positive
73 * 'long long' integer. If this is not true, it fails with %EINVAL error code.
74 * Returns %0 in case of success and %-1 in case of failure.
76 static int read_positive_ll(const char *file, long long *value)
81 fd = open(file, O_RDONLY);
85 rd = read(fd, buf, sizeof(buf));
87 sys_errmsg("cannot read \"%s\"", file);
90 if (rd == sizeof(buf)) {
91 errmsg("contents of \"%s\" is too long", file);
97 if (sscanf(buf, "%lld\n", value) != 1) {
98 errmsg("cannot read integer from \"%s\"\n", file);
104 errmsg("negative value %lld in \"%s\"", *value, file);
110 sys_errmsg("close failed on \"%s\"", file);
122 * read_positive_int - read a positive 'int' value from a file.
123 * @file: the file to read from
124 * @value: the result is stored here
126 * This function is the same as 'read_positive_ll()', but it reads an 'int'
127 * value, not 'long long'.
129 static int read_positive_int(const char *file, int *value)
133 if (read_positive_ll(file, &res))
136 /* Make sure the value is not too big */
138 errmsg("value %lld read from file \"%s\" is out of range",
149 * read_data - read data from a file.
150 * @file: the file to read from
151 * @buf: the buffer to read to
152 * @buf_len: buffer length
154 * This function returns number of read bytes in case of success and %-1 in
155 * case of failure. Note, if the file contains more then @buf_len bytes of
156 * date, this function fails with %EINVAL error code.
158 static int read_data(const char *file, void *buf, int buf_len)
160 int fd, rd, tmp, tmp1;
162 fd = open(file, O_RDONLY);
166 rd = read(fd, buf, buf_len);
168 sys_errmsg("cannot read \"%s\"", file);
173 errmsg("contents of \"%s\" is too long", file);
178 ((char *)buf)[rd] = '\0';
180 /* Make sure all data is read */
181 tmp1 = read(fd, &tmp, 1);
183 sys_errmsg("cannot read \"%s\"", file);
187 errmsg("file \"%s\" contains too much data (> %d bytes)",
194 sys_errmsg("close failed on \"%s\"", file);
206 * read_major - read major and minor numbers from a file.
207 * @file: name of the file to read from
208 * @major: major number is returned here
209 * @minor: minor number is returned here
211 * This function returns % in case of succes, and %-1 in case of failure.
213 static int read_major(const char *file, int *major, int *minor)
218 ret = read_data(file, buf, 50);
222 ret = sscanf(buf, "%d:%d\n", major, minor);
225 errmsg("\"%s\" does not have major:minor format", file);
229 if (*major < 0 || *minor < 0) {
231 errmsg("bad major:minor %d:%d in \"%s\"",
232 *major, *minor, file);
240 * dev_read_int - read a positive 'int' value from an UBI device sysfs file.
241 * @patt: file pattern to read from
242 * @dev_num: UBI device number
243 * @value: the result is stored here
245 * This function returns %0 in case of success and %-1 in case of failure.
247 static int dev_read_int(const char *patt, int dev_num, int *value)
249 char file[strlen(patt) + 50];
251 sprintf(file, patt, dev_num);
252 return read_positive_int(file, value);
256 * vol_read_int - read a positive 'int' value from an UBI volume sysfs file.
257 * @patt: file pattern to read from
258 * @dev_num: UBI device number
260 * @value: the result is stored here
262 * This function returns %0 in case of success and %-1 in case of failure.
264 static int vol_read_int(const char *patt, int dev_num, int vol_id, int *value)
266 char file[strlen(patt) + 100];
268 sprintf(file, patt, dev_num, vol_id);
269 return read_positive_int(file, value);
273 * dev_read_ll - read a positive 'long long' value from an UBI device sysfs file.
274 * @patt: file pattern to read from
275 * @dev_num: UBI device number
276 * @value: the result is stored here
278 * This function returns %0 in case of success and %-1 in case of failure.
280 static int dev_read_ll(const char *patt, int dev_num, long long *value)
282 char file[strlen(patt) + 50];
284 sprintf(file, patt, dev_num);
285 return read_positive_ll(file, value);
289 * vol_read_ll - read a positive 'long long' value from an UBI volume sysfs file.
290 * @patt: file pattern to read from
291 * @dev_num: UBI device number
293 * @value: the result is stored here
295 * This function returns %0 in case of success and %-1 in case of failure.
297 static int vol_read_ll(const char *patt, int dev_num, int vol_id,
300 char file[strlen(patt) + 100];
302 sprintf(file, patt, dev_num, vol_id);
303 return read_positive_ll(file, value);
307 * vol_read_data - read data from an UBI volume's sysfs file.
308 * @patt: file pattern to read from
309 * @dev_num: UBI device number
311 * @buf: buffer to read to
312 * @buf_len: buffer length
314 * This function returns number of read bytes in case of success and %-1 in
317 static int vol_read_data(const char *patt, int dev_num, int vol_id, void *buf,
320 char file[strlen(patt) + 100];
322 sprintf(file, patt, dev_num, vol_id);
323 return read_data(file, buf, buf_len);
327 * dev_get_major - get major and minor numbers of an UBI device.
328 * @lib: libubi descriptor
329 * @dev_num: UBI device number
330 * @major: major number is returned here
331 * @minor: minor number is returned here
333 * This function returns zero in case of succes and %-1 in case of failure.
335 static int dev_get_major(struct libubi *lib, int dev_num, int *major, int *minor)
337 char file[strlen(lib->dev_dev) + 50];
339 sprintf(file, lib->dev_dev, dev_num);
340 return read_major(file, major, minor);
344 * vol_get_major - get major and minor numbers of an UBI volume.
345 * @lib: libubi descriptor
346 * @dev_num: UBI device number
348 * @major: major number is returned here
349 * @minor: minor number is returned here
351 * This function returns zero in case of succes and %-1 in case of failure.
353 static int vol_get_major(struct libubi *lib, int dev_num, int vol_id,
354 int *major, int *minor)
356 char file[strlen(lib->vol_dev) + 100];
358 sprintf(file, lib->vol_dev, dev_num, vol_id);
359 return read_major(file, major, minor);
363 * vol_node2nums - find UBI device number and volume ID by volume device node
365 * @lib: UBI library descriptor
366 * @node: UBI character device node name
367 * @dev_num: UBI device number is returned here
368 * @vol_id: volume ID is returned hers
370 * This function returns zero in case of succes and %-1 in case of failure.
372 static int vol_node2nums(struct libubi *lib, const char *node, int *dev_num,
376 struct ubi_info info;
377 int i, fd, major, minor;
378 char file[strlen(lib->ubi_vol) + 100];
380 if (stat(node, &st)) {
381 sys_errmsg("cannot get information about \"%s\"",
385 if (!S_ISCHR(st.st_mode)) {
387 errmsg("\"%s\" is not a character device", node);
391 major = major(st.st_rdev);
392 minor = minor(st.st_rdev);
396 errmsg("\"%s\" is not a volume character device", node);
400 if (ubi_get_info((libubi_t *)lib, &info))
403 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
404 int major1, minor1, ret;
406 ret = dev_get_major(lib, i, &major1, &minor1);
417 if (i > info.highest_dev_num) {
422 /* Make sure this UBI volume exists */
423 sprintf(file, lib->ubi_vol, i, minor - 1);
424 fd = open(file, O_RDONLY);
437 * dev_node2num - find UBI device number by its character device node.
438 * @lib: UBI library descriptor
439 * @node: UBI character device node name
440 * @dev_num: UBI device number is returned here
442 * This function returns %0 in case of success and %-1 in case of failure.
444 static int dev_node2num(struct libubi *lib, const char *node, int *dev_num)
447 struct ubi_info info;
450 if (stat(node, &st)) {
451 sys_errmsg("cannot get information about \"%s\"", node);
454 if (!S_ISCHR(st.st_mode)) {
456 errmsg("\"%s\" is not a character device", node);
460 major = major(st.st_rdev);
461 minor = minor(st.st_rdev);
465 errmsg("\"%s\" is not an UBI character device", node);
469 if (ubi_get_info((libubi_t *)lib, &info))
472 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
473 int major1, minor1, ret;
475 ret = dev_get_major(lib, i, &major1, &minor1);
482 if (major1 == major) {
484 errmsg("UBI character device minor number is "
485 "%d, but must be 0", minor1);
499 int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num)
501 struct ubi_info info;
502 int i, ret, mtd_num1;
503 struct libubi *lib = desc;
505 if (ubi_get_info(desc, &info))
508 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
509 ret = dev_read_int(lib->dev_mtd_num, i, &mtd_num1);
516 if (mtd_num1 == mtd_num) {
527 libubi_t libubi_open(void)
532 lib = calloc(1, sizeof(struct libubi));
536 lib->sysfs_ctrl = mkpath("/sys", SYSFS_CTRL);
537 if (!lib->sysfs_ctrl)
540 lib->ctrl_dev = mkpath(lib->sysfs_ctrl, CTRL_DEV);
544 lib->sysfs_ubi = mkpath("/sys", SYSFS_UBI);
548 /* Make sure UBI is present */
549 fd = open(lib->sysfs_ubi, O_RDONLY);
556 sys_errmsg("close failed on \"%s\"", lib->sysfs_ubi);
560 lib->ubi_dev = mkpath(lib->sysfs_ubi, UBI_DEV_NAME_PATT);
564 lib->ubi_version = mkpath(lib->sysfs_ubi, UBI_VER);
565 if (!lib->ubi_version)
568 lib->dev_dev = mkpath(lib->ubi_dev, DEV_DEV);
572 lib->dev_avail_ebs = mkpath(lib->ubi_dev, DEV_AVAIL_EBS);
573 if (!lib->dev_avail_ebs)
576 lib->dev_total_ebs = mkpath(lib->ubi_dev, DEV_TOTAL_EBS);
577 if (!lib->dev_total_ebs)
580 lib->dev_bad_count = mkpath(lib->ubi_dev, DEV_BAD_COUNT);
581 if (!lib->dev_bad_count)
584 lib->dev_eb_size = mkpath(lib->ubi_dev, DEV_EB_SIZE);
585 if (!lib->dev_eb_size)
588 lib->dev_max_ec = mkpath(lib->ubi_dev, DEV_MAX_EC);
589 if (!lib->dev_max_ec)
592 lib->dev_bad_rsvd = mkpath(lib->ubi_dev, DEV_MAX_RSVD);
593 if (!lib->dev_bad_rsvd)
596 lib->dev_max_vols = mkpath(lib->ubi_dev, DEV_MAX_VOLS);
597 if (!lib->dev_max_vols)
600 lib->dev_min_io_size = mkpath(lib->ubi_dev, DEV_MIN_IO_SIZE);
601 if (!lib->dev_min_io_size)
604 lib->dev_mtd_num = mkpath(lib->ubi_dev, DEV_MTD_NUM);
605 if (!lib->dev_mtd_num)
608 lib->ubi_vol = mkpath(lib->sysfs_ubi, UBI_VOL_NAME_PATT);
612 lib->vol_type = mkpath(lib->ubi_vol, VOL_TYPE);
616 lib->vol_dev = mkpath(lib->ubi_vol, VOL_DEV);
620 lib->vol_alignment = mkpath(lib->ubi_vol, VOL_ALIGNMENT);
621 if (!lib->vol_alignment)
624 lib->vol_data_bytes = mkpath(lib->ubi_vol, VOL_DATA_BYTES);
625 if (!lib->vol_data_bytes)
628 lib->vol_rsvd_ebs = mkpath(lib->ubi_vol, VOL_RSVD_EBS);
629 if (!lib->vol_rsvd_ebs)
632 lib->vol_eb_size = mkpath(lib->ubi_vol, VOL_EB_SIZE);
633 if (!lib->vol_eb_size)
636 lib->vol_corrupted = mkpath(lib->ubi_vol, VOL_CORRUPTED);
637 if (!lib->vol_corrupted)
640 lib->vol_name = mkpath(lib->ubi_vol, VOL_NAME);
644 if (read_positive_int(lib->ubi_version, &version))
646 if (version != LIBUBI_UBI_VERSION) {
647 errmsg("this library was made for UBI version %d, but UBI "
648 "version %d is detected\n", LIBUBI_UBI_VERSION, version);
655 libubi_close((libubi_t)lib);
659 void libubi_close(libubi_t desc)
661 struct libubi *lib = (struct libubi *)desc;
664 free(lib->vol_corrupted);
665 free(lib->vol_eb_size);
666 free(lib->vol_rsvd_ebs);
667 free(lib->vol_data_bytes);
668 free(lib->vol_alignment);
672 free(lib->dev_mtd_num);
673 free(lib->dev_min_io_size);
674 free(lib->dev_max_vols);
675 free(lib->dev_bad_rsvd);
676 free(lib->dev_max_ec);
677 free(lib->dev_eb_size);
678 free(lib->dev_bad_count);
679 free(lib->dev_total_ebs);
680 free(lib->dev_avail_ebs);
682 free(lib->ubi_version);
684 free(lib->sysfs_ubi);
686 free(lib->sysfs_ctrl);
691 * do_attach - perform the actual attach operation.
692 * @node: name of the UBI control character device node
695 * This function performs the actual UBI attach operation. Returns %0 in case of
696 * success and %-1 in case of failure. @r->ubi_num contains newly created UBI
699 static int do_attach(const char *node, const struct ubi_attach_req *r)
703 fd = open(node, O_RDONLY);
705 sys_errmsg("cannot open \"%s\"", node);
708 ret = ioctl(fd, UBI_IOCATT, r);
713 #ifdef UDEV_SETTLE_HACK
714 // if (system("udevsettle") == -1)
721 #ifndef MTD_CHAR_MAJOR
723 * This is taken from kernel <linux/mtd/mtd.h> and is unlikely to change anytime
726 #define MTD_CHAR_MAJOR 90
730 * mtd_node_to_num - converts device node to MTD number.
731 * @mtd_dev_node: path to device node to convert
733 * This function converts given @mtd_dev_node to MTD device number.
734 * @mtd_dev_node should contain path to the MTD device node. Returns MTD device
735 * number in case of success and %-1 in case of failure (errno is set).
737 static int mtd_node_to_num(const char *mtd_dev_node)
742 if (stat(mtd_dev_node, &sb) < 0) {
743 sys_errmsg("cannot stat \"%s\"", mtd_dev_node);
747 if (!S_ISCHR(sb.st_mode)) {
749 sys_errmsg("\"%s\" is not a character device",
754 major = major(sb.st_rdev);
755 minor = minor(sb.st_rdev);
757 if (major != MTD_CHAR_MAJOR) {
759 sys_errmsg("\"%s\" is not an MTD device", mtd_dev_node);
766 int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
768 struct ubi_attach_req r;
773 if (req->mtd_dev_node) {
775 * User has passed path to device node. Lets find out MTD
776 * device number of the device and update req->mtd_num with it
778 req->mtd_num = mtd_node_to_num(req->mtd_dev_node);
779 if (req->mtd_num == -1)
783 memset(&r, 0, sizeof(struct ubi_attach_req));
784 r.ubi_num = req->dev_num;
785 r.mtd_num = req->mtd_num;
786 r.vid_hdr_offset = req->vid_hdr_offset;
788 if (req->max_beb_per1024) {
790 * We first have to check if the running kernel supports the
791 * 'max_beb_per1024' parameter. To do this, we invoke the
792 * "attach" ioctl 2 times: first with incorrect value %-1 of
795 * If the ioctl succeeds, it means that the kernel doesn't
796 * support the feature and just ignored our 'max_beb_per1024'
799 * If the ioctl returns -EINVAL, we assume this is because
800 * 'max_beb_per1024' was set to -1, and we invoke the ioctl for
801 * the second time with the 'max_beb_per1024' value.
803 r.max_beb_per1024 = -1;
804 ret = do_attach(node, &r);
806 req->dev_num = r.ubi_num;
808 * The call succeeded. It means that the kernel ignored
809 * 'max_beb_per1024' parameter.
812 } else if (errno != EINVAL)
816 r.max_beb_per1024 = req->max_beb_per1024;
818 ret = do_attach(node, &r);
820 req->dev_num = r.ubi_num;
825 int ubi_detach_mtd(libubi_t desc, const char *node, int mtd_num)
829 ret = mtd_num2ubi_dev(desc, mtd_num, &ubi_dev);
835 return ubi_remove_dev(desc, node, ubi_dev);
838 int ubi_detach(libubi_t desc, const char *node, const char *mtd_dev_node)
847 mtd_num = mtd_node_to_num(mtd_dev_node);
851 return ubi_detach_mtd(desc, node, mtd_num);
854 int ubi_remove_dev(libubi_t desc, const char *node, int ubi_dev)
860 fd = open(node, O_RDONLY);
862 sys_errmsg("cannot open \"%s\"", node);
865 ret = ioctl(fd, UBI_IOCFDET, &ubi_dev);
869 #ifdef UDEV_SETTLE_HACK
870 // if (system("udevsettle") == -1)
880 int ubi_probe_node(libubi_t desc, const char *node)
883 struct ubi_info info;
884 int i, fd, major, minor;
885 struct libubi *lib = (struct libubi *)desc;
886 char file[strlen(lib->ubi_vol) + 100];
888 if (stat(node, &st)) {
889 sys_errmsg("cannot get information about \"%s\"", node);
893 if (!S_ISCHR(st.st_mode)) {
894 errmsg("\"%s\" is not a character device", node);
899 major = major(st.st_rdev);
900 minor = minor(st.st_rdev);
902 if (ubi_get_info((libubi_t *)lib, &info))
905 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
906 int major1, minor1, ret;
908 ret = dev_get_major(lib, i, &major1, &minor1);
921 if (i > info.highest_dev_num)
927 /* This is supposdely an UBI volume device node */
928 sprintf(file, lib->ubi_vol, i, minor - 1);
929 fd = open(file, O_RDONLY);
936 errmsg("\"%s\" has major:minor %d:%d, but this does not correspond to "
937 "any existing UBI device or volume", node, major, minor);
942 int ubi_get_info(libubi_t desc, struct ubi_info *info)
945 struct dirent *dirent;
946 struct libubi *lib = (struct libubi *)desc;
948 memset(info, 0, sizeof(struct ubi_info));
950 if (read_major(lib->ctrl_dev, &info->ctrl_major, &info->ctrl_minor)) {
952 * Older UBI versions did not have control device, so we do not
953 * panic here for compatibility reasons. May be few years later
954 * we could return -1 here, but for now just set major:minor to
957 info->ctrl_major = info->ctrl_minor = -1;
961 * We have to scan the UBI sysfs directory to identify how many UBI
962 * devices are present.
964 sysfs_ubi = opendir(lib->sysfs_ubi);
968 info->lowest_dev_num = INT_MAX;
974 dirent = readdir(sysfs_ubi);
978 if (strlen(dirent->d_name) >= 255) {
979 errmsg("invalid entry in %s: \"%s\"",
980 lib->sysfs_ubi, dirent->d_name);
985 ret = sscanf(dirent->d_name, UBI_DEV_NAME_PATT"%s",
988 info->dev_count += 1;
989 if (dev_num > info->highest_dev_num)
990 info->highest_dev_num = dev_num;
991 if (dev_num < info->lowest_dev_num)
992 info->lowest_dev_num = dev_num;
996 if (!dirent && errno) {
997 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
1001 if (closedir(sysfs_ubi)) {
1002 sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
1005 if (info->lowest_dev_num == INT_MAX)
1006 info->lowest_dev_num = 0;
1008 if (read_positive_int(lib->ubi_version, &info->version))
1014 closedir(sysfs_ubi);
1018 int ubi_mkvol(libubi_t desc, const char *node, struct ubi_mkvol_request *req)
1021 struct ubi_mkvol_req r;
1024 memset(&r, 0, sizeof(struct ubi_mkvol_req));
1027 r.vol_id = req->vol_id;
1028 r.alignment = req->alignment;
1029 r.bytes = req->bytes;
1030 r.vol_type = req->vol_type;
1032 n = strlen(req->name);
1033 if (n > UBI_MAX_VOLUME_NAME)
1036 strncpy(r.name, req->name, UBI_MAX_VOLUME_NAME + 1);
1040 fd = open(node, O_RDONLY);
1042 sys_errmsg("cannot open \"%s\"", node);
1045 ret = ioctl(fd, UBI_IOCMKVOL, &r);
1052 req->vol_id = r.vol_id;
1054 #ifdef UDEV_SETTLE_HACK
1055 // if (system("udevsettle") == -1)
1063 int ubi_rmvol(libubi_t desc, const char *node, int vol_id)
1068 fd = open(node, O_RDONLY);
1070 sys_errmsg("cannot open \"%s\"", node);
1074 ret = ioctl(fd, UBI_IOCRMVOL, &vol_id);
1082 #ifdef UDEV_SETTLE_HACK
1083 // if (system("udevsettle") == -1)
1091 int ubi_rnvols(libubi_t desc, const char *node, struct ubi_rnvol_req *rnvol)
1096 fd = open(node, O_RDONLY);
1100 ret = ioctl(fd, UBI_IOCRNVOL, rnvol);
1108 #ifdef UDEV_SETTLE_HACK
1109 // if (system("udevsettle") == -1)
1117 int ubi_rsvol(libubi_t desc, const char *node, int vol_id, long long bytes)
1120 struct ubi_rsvol_req req;
1123 fd = open(node, O_RDONLY);
1125 sys_errmsg("cannot open \"%s\"", node);
1129 req.vol_id = vol_id;
1131 ret = ioctl(fd, UBI_IOCRSVOL, &req);
1136 int ubi_update_start(libubi_t desc, int fd, long long bytes)
1139 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
1144 int ubi_leb_change_start(libubi_t desc, int fd, int lnum, int bytes)
1146 struct ubi_leb_change_req req;
1149 memset(&req, 0, sizeof(struct ubi_leb_change_req));
1154 if (ioctl(fd, UBI_IOCEBCH, &req))
1159 int ubi_dev_present(libubi_t desc, int dev_num)
1162 struct libubi *lib = (struct libubi *)desc;
1163 char file[strlen(lib->ubi_dev) + 50];
1165 sprintf(file, lib->ubi_dev, dev_num);
1166 return !stat(file, &st);
1169 int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info)
1172 struct dirent *dirent;
1173 struct libubi *lib = (struct libubi *)desc;
1175 memset(info, 0, sizeof(struct ubi_dev_info));
1176 info->dev_num = dev_num;
1178 if (!ubi_dev_present(desc, dev_num))
1181 sysfs_ubi = opendir(lib->sysfs_ubi);
1185 info->lowest_vol_id = INT_MAX;
1188 int vol_id, ret, devno;
1192 dirent = readdir(sysfs_ubi);
1196 if (strlen(dirent->d_name) >= 255) {
1197 errmsg("invalid entry in %s: \"%s\"",
1198 lib->sysfs_ubi, dirent->d_name);
1202 ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT"%s", &devno, &vol_id, tmp_buf);
1203 if (ret == 2 && devno == dev_num) {
1204 info->vol_count += 1;
1205 if (vol_id > info->highest_vol_id)
1206 info->highest_vol_id = vol_id;
1207 if (vol_id < info->lowest_vol_id)
1208 info->lowest_vol_id = vol_id;
1212 if (!dirent && errno) {
1213 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
1217 if (closedir(sysfs_ubi)) {
1218 sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
1221 if (info->lowest_vol_id == INT_MAX)
1222 info->lowest_vol_id = 0;
1224 if (dev_get_major(lib, dev_num, &info->major, &info->minor))
1227 if (dev_read_int(lib->dev_mtd_num, dev_num, &info->mtd_num))
1229 if (dev_read_int(lib->dev_avail_ebs, dev_num, &info->avail_lebs))
1231 if (dev_read_int(lib->dev_total_ebs, dev_num, &info->total_lebs))
1233 if (dev_read_int(lib->dev_bad_count, dev_num, &info->bad_count))
1235 if (dev_read_int(lib->dev_eb_size, dev_num, &info->leb_size))
1237 if (dev_read_int(lib->dev_bad_rsvd, dev_num, &info->bad_rsvd))
1239 if (dev_read_ll(lib->dev_max_ec, dev_num, &info->max_ec))
1241 if (dev_read_int(lib->dev_max_vols, dev_num, &info->max_vol_count))
1243 if (dev_read_int(lib->dev_min_io_size, dev_num, &info->min_io_size))
1246 info->avail_bytes = (long long)info->avail_lebs * info->leb_size;
1247 info->total_bytes = (long long)info->total_lebs * info->leb_size;
1252 closedir(sysfs_ubi);
1256 int ubi_get_dev_info(libubi_t desc, const char *node, struct ubi_dev_info *info)
1258 int err, dev_num = 0;
1259 struct libubi *lib = (struct libubi *)desc;
1261 err = ubi_probe_node(desc, node);
1268 if (dev_node2num(lib, node, &dev_num))
1271 return ubi_get_dev_info1(desc, dev_num, info);
1274 int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id,
1275 struct ubi_vol_info *info)
1278 struct libubi *lib = (struct libubi *)desc;
1281 memset(info, 0, sizeof(struct ubi_vol_info));
1282 info->dev_num = dev_num;
1283 info->vol_id = vol_id;
1285 if (vol_get_major(lib, dev_num, vol_id, &info->major, &info->minor))
1288 ret = vol_read_data(lib->vol_type, dev_num, vol_id, buf, 50);
1292 if (strncmp(buf, "static\n", ret) == 0)
1293 info->type = UBI_STATIC_VOLUME;
1294 else if (strncmp(buf, "dynamic\n", ret) == 0)
1295 info->type = UBI_DYNAMIC_VOLUME;
1297 errmsg("bad value at \"%s\"", buf);
1302 ret = vol_read_int(lib->vol_alignment, dev_num, vol_id,
1306 ret = vol_read_ll(lib->vol_data_bytes, dev_num, vol_id,
1310 ret = vol_read_int(lib->vol_rsvd_ebs, dev_num, vol_id, &info->rsvd_lebs);
1313 ret = vol_read_int(lib->vol_eb_size, dev_num, vol_id, &info->leb_size);
1316 ret = vol_read_int(lib->vol_corrupted, dev_num, vol_id,
1320 info->rsvd_bytes = (long long)info->leb_size * info->rsvd_lebs;
1322 ret = vol_read_data(lib->vol_name, dev_num, vol_id, &info->name,
1323 UBI_VOL_NAME_MAX + 2);
1327 info->name[ret - 1] = '\0';
1331 int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info)
1333 int err, vol_id = 0, dev_num = 0;
1334 struct libubi *lib = (struct libubi *)desc;
1336 err = ubi_probe_node(desc, node);
1343 if (vol_node2nums(lib, node, &dev_num, &vol_id))
1346 return ubi_get_vol_info1(desc, dev_num, vol_id, info);
1349 int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
1350 struct ubi_vol_info *info)
1353 unsigned int nlen = strlen(name);
1354 struct ubi_dev_info dev_info;
1357 errmsg("bad \"name\" input parameter");
1362 err = ubi_get_dev_info1(desc, dev_num, &dev_info);
1366 for (i = dev_info.lowest_vol_id;
1367 i <= dev_info.highest_vol_id; i++) {
1368 err = ubi_get_vol_info1(desc, dev_num, i, info);
1370 if (errno == ENOENT)
1375 if (nlen == strlen(info->name) && !strcmp(name, info->name))
1383 int ubi_set_property(int fd, uint8_t property, uint64_t value)
1385 struct ubi_set_vol_prop_req r;
1387 memset(&r, 0, sizeof(struct ubi_set_vol_prop_req));
1388 r.property = property;
1391 return ioctl(fd, UBI_IOCSETVOLPROP, &r);
1394 int ubi_leb_unmap(int fd, int lnum)
1396 return ioctl(fd, UBI_IOCEBUNMAP, &lnum);
1399 int ubi_is_mapped(int fd, int lnum)
1401 return ioctl(fd, UBI_IOCEBISMAP, &lnum);