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"
32 #include <sys/ioctl.h>
34 #include <sys/types.h>
36 #include "libubi_int.h"
39 * mkpath - compose full path from 2 given components.
40 * @path: the first component
41 * @name: the second component
43 * This function returns the resulting path in case of success and %NULL in
46 static char *mkpath(const char *path, const char *name)
49 int len1 = strlen(path);
50 int len2 = strlen(name);
52 n = malloc(len1 + len2 + 2);
54 sys_errmsg("cannot allocate %d bytes", len1 + len2 + 2);
58 memcpy(n, path, len1);
59 if (n[len1 - 1] != '/')
62 memcpy(n + len1, name, len2 + 1);
67 * read_positive_ll - read a positive 'long long' value from a file.
68 * @file: the file to read from
69 * @value: the result is stored here
71 * This function reads file @file and interprets its contents as a positive
72 * 'long long' integer. If this is not true, it fails with %EINVAL error code.
73 * Returns %0 in case of success and %-1 in case of failure.
75 static int read_positive_ll(const char *file, long long *value)
80 fd = open(file, O_RDONLY);
84 rd = read(fd, buf, sizeof(buf));
86 sys_errmsg("cannot read \"%s\"", file);
89 if (rd == sizeof(buf)) {
90 errmsg("contents of \"%s\" is too long", file);
96 if (sscanf(buf, "%lld\n", value) != 1) {
97 errmsg("cannot read integer from \"%s\"\n", file);
103 errmsg("negative value %lld in \"%s\"", *value, file);
109 sys_errmsg("close failed on \"%s\"", file);
121 * read_positive_int - read a positive 'int' value from a file.
122 * @file: the file to read from
123 * @value: the result is stored here
125 * This function is the same as 'read_positive_ll()', but it reads an 'int'
126 * value, not 'long long'.
128 static int read_positive_int(const char *file, int *value)
132 if (read_positive_ll(file, &res))
135 /* Make sure the value is not too big */
137 errmsg("value %lld read from file \"%s\" is out of range",
148 * read_data - read data from a file.
149 * @file: the file to read from
150 * @buf: the buffer to read to
151 * @buf_len: buffer length
153 * This function returns number of read bytes in case of success and %-1 in
154 * case of failure. Note, if the file contains more then @buf_len bytes of
155 * date, this function fails with %EINVAL error code.
157 static int read_data(const char *file, void *buf, int buf_len)
159 int fd, rd, tmp, tmp1;
161 fd = open(file, O_RDONLY);
165 rd = read(fd, buf, buf_len);
167 sys_errmsg("cannot read \"%s\"", file);
172 errmsg("contents of \"%s\" is too long", file);
177 ((char *)buf)[rd] = '\0';
179 /* Make sure all data is read */
180 tmp1 = read(fd, &tmp, 1);
182 sys_errmsg("cannot read \"%s\"", file);
186 errmsg("file \"%s\" contains too much data (> %d bytes)",
193 sys_errmsg("close failed on \"%s\"", file);
205 * read_major - read major and minor numbers from a file.
206 * @file: name of the file to read from
207 * @major: major number is returned here
208 * @minor: minor number is returned here
210 * This function returns % in case of succes, and %-1 in case of failure.
212 static int read_major(const char *file, int *major, int *minor)
217 ret = read_data(file, buf, 50);
221 ret = sscanf(buf, "%d:%d\n", major, minor);
224 errmsg("\"%s\" does not have major:minor format", file);
228 if (*major < 0 || *minor < 0) {
230 errmsg("bad major:minor %d:%d in \"%s\"",
231 *major, *minor, file);
239 * dev_read_int - read a positive 'int' value from an UBI device sysfs file.
240 * @patt: file pattern to read from
241 * @dev_num: UBI device number
242 * @value: the result is stored here
244 * This function returns %0 in case of success and %-1 in case of failure.
246 static int dev_read_int(const char *patt, int dev_num, int *value)
248 char file[strlen(patt) + 50];
250 sprintf(file, patt, dev_num);
251 return read_positive_int(file, value);
255 * vol_read_int - read a positive 'int' value from an UBI volume sysfs file.
256 * @patt: file pattern to read from
257 * @dev_num: UBI device number
259 * @value: the result is stored here
261 * This function returns %0 in case of success and %-1 in case of failure.
263 static int vol_read_int(const char *patt, int dev_num, int vol_id, int *value)
265 char file[strlen(patt) + 100];
267 sprintf(file, patt, dev_num, vol_id);
268 return read_positive_int(file, value);
272 * dev_read_ll - read a positive 'long long' value from an UBI device sysfs file.
273 * @patt: file pattern to read from
274 * @dev_num: UBI device number
275 * @value: the result is stored here
277 * This function returns %0 in case of success and %-1 in case of failure.
279 static int dev_read_ll(const char *patt, int dev_num, long long *value)
281 char file[strlen(patt) + 50];
283 sprintf(file, patt, dev_num);
284 return read_positive_ll(file, value);
288 * vol_read_ll - read a positive 'long long' value from an UBI volume sysfs file.
289 * @patt: file pattern to read from
290 * @dev_num: UBI device number
292 * @value: the result is stored here
294 * This function returns %0 in case of success and %-1 in case of failure.
296 static int vol_read_ll(const char *patt, int dev_num, int vol_id,
299 char file[strlen(patt) + 100];
301 sprintf(file, patt, dev_num, vol_id);
302 return read_positive_ll(file, value);
306 * vol_read_data - read data from an UBI volume's sysfs file.
307 * @patt: file pattern to read from
308 * @dev_num: UBI device number
310 * @buf: buffer to read to
311 * @buf_len: buffer length
313 * This function returns number of read bytes in case of success and %-1 in
316 static int vol_read_data(const char *patt, int dev_num, int vol_id, void *buf,
319 char file[strlen(patt) + 100];
321 sprintf(file, patt, dev_num, vol_id);
322 return read_data(file, buf, buf_len);
326 * dev_get_major - get major and minor numbers of an UBI device.
327 * @lib: libubi descriptor
328 * @dev_num: UBI device number
329 * @major: major number is returned here
330 * @minor: minor number is returned here
332 * This function returns zero in case of succes and %-1 in case of failure.
334 static int dev_get_major(struct libubi *lib, int dev_num, int *major, int *minor)
336 char file[strlen(lib->dev_dev) + 50];
338 sprintf(file, lib->dev_dev, dev_num);
339 return read_major(file, major, minor);
343 * vol_get_major - get major and minor numbers of an UBI volume.
344 * @lib: libubi descriptor
345 * @dev_num: UBI device number
347 * @major: major number is returned here
348 * @minor: minor number is returned here
350 * This function returns zero in case of succes and %-1 in case of failure.
352 static int vol_get_major(struct libubi *lib, int dev_num, int vol_id,
353 int *major, int *minor)
355 char file[strlen(lib->vol_dev) + 100];
357 sprintf(file, lib->vol_dev, dev_num, vol_id);
358 return read_major(file, major, minor);
362 * vol_node2nums - find UBI device number and volume ID by volume device node
364 * @lib: UBI library descriptor
365 * @node: UBI character device node name
366 * @dev_num: UBI device number is returned here
367 * @vol_id: volume ID is returned hers
369 * This function returns zero in case of succes and %-1 in case of failure.
371 static int vol_node2nums(struct libubi *lib, const char *node, int *dev_num,
375 struct ubi_info info;
376 int i, fd, major, minor;
377 char file[strlen(lib->ubi_vol) + 100];
379 if (stat(node, &st)) {
380 sys_errmsg("cannot get information about \"%s\"",
384 if (!S_ISCHR(st.st_mode)) {
386 errmsg("\"%s\" is not a character device", node);
390 major = major(st.st_rdev);
391 minor = minor(st.st_rdev);
395 errmsg("\"%s\" is not a volume character device", node);
399 if (ubi_get_info((libubi_t *)lib, &info))
402 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
403 int major1, minor1, ret;
405 ret = dev_get_major(lib, i, &major1, &minor1);
416 if (i > info.highest_dev_num) {
421 /* Make sure this UBI volume exists */
422 sprintf(file, lib->ubi_vol, i, minor - 1);
423 fd = open(file, O_RDONLY);
436 * dev_node2num - find UBI device number by its character device node.
437 * @lib: UBI library descriptor
438 * @node: UBI character device node name
439 * @dev_num: UBI device number is returned here
441 * This function returns %0 in case of success and %-1 in case of failure.
443 static int dev_node2num(struct libubi *lib, const char *node, int *dev_num)
446 struct ubi_info info;
449 if (stat(node, &st)) {
450 sys_errmsg("cannot get information about \"%s\"", node);
453 if (!S_ISCHR(st.st_mode)) {
455 errmsg("\"%s\" is not a character device", node);
459 major = major(st.st_rdev);
460 minor = minor(st.st_rdev);
464 errmsg("\"%s\" is not an UBI character device", node);
468 if (ubi_get_info((libubi_t *)lib, &info))
471 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
472 int major1, minor1, ret;
474 ret = dev_get_major(lib, i, &major1, &minor1);
481 if (major1 == major) {
483 errmsg("UBI character device minor number is "
484 "%d, but must be 0", minor1);
498 int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num)
500 struct ubi_info info;
501 int i, ret, mtd_num1;
502 struct libubi *lib = desc;
504 if (ubi_get_info(desc, &info))
507 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
508 ret = dev_read_int(lib->dev_mtd_num, i, &mtd_num1);
515 if (mtd_num1 == mtd_num) {
526 libubi_t libubi_open(void)
531 lib = calloc(1, sizeof(struct libubi));
535 lib->sysfs_ctrl = mkpath("/sys", SYSFS_CTRL);
536 if (!lib->sysfs_ctrl)
539 lib->ctrl_dev = mkpath(lib->sysfs_ctrl, CTRL_DEV);
543 lib->sysfs_ubi = mkpath("/sys", SYSFS_UBI);
547 /* Make sure UBI is present */
548 fd = open(lib->sysfs_ubi, O_RDONLY);
555 sys_errmsg("close failed on \"%s\"", lib->sysfs_ubi);
559 lib->ubi_dev = mkpath(lib->sysfs_ubi, UBI_DEV_NAME_PATT);
563 lib->ubi_version = mkpath(lib->sysfs_ubi, UBI_VER);
564 if (!lib->ubi_version)
567 lib->dev_dev = mkpath(lib->ubi_dev, DEV_DEV);
571 lib->dev_avail_ebs = mkpath(lib->ubi_dev, DEV_AVAIL_EBS);
572 if (!lib->dev_avail_ebs)
575 lib->dev_total_ebs = mkpath(lib->ubi_dev, DEV_TOTAL_EBS);
576 if (!lib->dev_total_ebs)
579 lib->dev_bad_count = mkpath(lib->ubi_dev, DEV_BAD_COUNT);
580 if (!lib->dev_bad_count)
583 lib->dev_eb_size = mkpath(lib->ubi_dev, DEV_EB_SIZE);
584 if (!lib->dev_eb_size)
587 lib->dev_max_ec = mkpath(lib->ubi_dev, DEV_MAX_EC);
588 if (!lib->dev_max_ec)
591 lib->dev_bad_rsvd = mkpath(lib->ubi_dev, DEV_MAX_RSVD);
592 if (!lib->dev_bad_rsvd)
595 lib->dev_max_vols = mkpath(lib->ubi_dev, DEV_MAX_VOLS);
596 if (!lib->dev_max_vols)
599 lib->dev_min_io_size = mkpath(lib->ubi_dev, DEV_MIN_IO_SIZE);
600 if (!lib->dev_min_io_size)
603 lib->dev_mtd_num = mkpath(lib->ubi_dev, DEV_MTD_NUM);
604 if (!lib->dev_mtd_num)
607 lib->ubi_vol = mkpath(lib->sysfs_ubi, UBI_VOL_NAME_PATT);
611 lib->vol_type = mkpath(lib->ubi_vol, VOL_TYPE);
615 lib->vol_dev = mkpath(lib->ubi_vol, VOL_DEV);
619 lib->vol_alignment = mkpath(lib->ubi_vol, VOL_ALIGNMENT);
620 if (!lib->vol_alignment)
623 lib->vol_data_bytes = mkpath(lib->ubi_vol, VOL_DATA_BYTES);
624 if (!lib->vol_data_bytes)
627 lib->vol_rsvd_ebs = mkpath(lib->ubi_vol, VOL_RSVD_EBS);
628 if (!lib->vol_rsvd_ebs)
631 lib->vol_eb_size = mkpath(lib->ubi_vol, VOL_EB_SIZE);
632 if (!lib->vol_eb_size)
635 lib->vol_corrupted = mkpath(lib->ubi_vol, VOL_CORRUPTED);
636 if (!lib->vol_corrupted)
639 lib->vol_name = mkpath(lib->ubi_vol, VOL_NAME);
643 if (read_positive_int(lib->ubi_version, &version))
645 if (version != LIBUBI_UBI_VERSION) {
646 errmsg("this library was made for UBI version %d, but UBI "
647 "version %d is detected\n", LIBUBI_UBI_VERSION, version);
654 libubi_close((libubi_t)lib);
658 void libubi_close(libubi_t desc)
660 struct libubi *lib = (struct libubi *)desc;
663 free(lib->vol_corrupted);
664 free(lib->vol_eb_size);
665 free(lib->vol_rsvd_ebs);
666 free(lib->vol_data_bytes);
667 free(lib->vol_alignment);
671 free(lib->dev_mtd_num);
672 free(lib->dev_min_io_size);
673 free(lib->dev_max_vols);
674 free(lib->dev_bad_rsvd);
675 free(lib->dev_max_ec);
676 free(lib->dev_eb_size);
677 free(lib->dev_bad_count);
678 free(lib->dev_total_ebs);
679 free(lib->dev_avail_ebs);
681 free(lib->ubi_version);
683 free(lib->sysfs_ubi);
685 free(lib->sysfs_ctrl);
690 * do_attach - perform the actual attach operation.
691 * @node: name of the UBI control character device node
694 * This function performs the actual UBI attach operation. Returns %0 in case of
695 * success and %-1 in case of failure. @r->ubi_num contains newly created UBI
698 static int do_attach(const char *node, const struct ubi_attach_req *r)
702 fd = open(node, O_RDONLY);
704 sys_errmsg("cannot open \"%s\"", node);
707 ret = ioctl(fd, UBI_IOCATT, r);
712 #ifdef UDEV_SETTLE_HACK
713 // if (system("udevsettle") == -1)
720 #ifndef MTD_CHAR_MAJOR
722 * This is taken from kernel <linux/mtd/mtd.h> and is unlikely to change anytime
725 #define MTD_CHAR_MAJOR 90
729 * mtd_node_to_num - converts device node to MTD number.
730 * @mtd_dev_node: path to device node to convert
732 * This function converts given @mtd_dev_node to MTD device number.
733 * @mtd_dev_node should contain path to the MTD device node. Returns MTD device
734 * number in case of success and %-1 in case of failure (errno is set).
736 static int mtd_node_to_num(const char *mtd_dev_node)
741 if (stat(mtd_dev_node, &sb) < 0) {
742 sys_errmsg("cannot stat \"%s\"", mtd_dev_node);
746 if (!S_ISCHR(sb.st_mode)) {
748 sys_errmsg("\"%s\" is not a character device",
753 major = major(sb.st_rdev);
754 minor = minor(sb.st_rdev);
756 if (major != MTD_CHAR_MAJOR) {
758 sys_errmsg("\"%s\" is not an MTD device", mtd_dev_node);
765 int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
767 struct ubi_attach_req r;
772 if (req->mtd_dev_node) {
774 * User has passed path to device node. Lets find out MTD
775 * device number of the device and update req->mtd_num with it
777 req->mtd_num = mtd_node_to_num(req->mtd_dev_node);
778 if (req->mtd_num == -1)
782 memset(&r, 0, sizeof(struct ubi_attach_req));
783 r.ubi_num = req->dev_num;
784 r.mtd_num = req->mtd_num;
785 r.vid_hdr_offset = req->vid_hdr_offset;
787 if (req->max_beb_per1024) {
789 * We first have to check if the running kernel supports the
790 * 'max_beb_per1024' parameter. To do this, we invoke the
791 * "attach" ioctl 2 times: first with incorrect value %-1 of
794 * If the ioctl succeeds, it means that the kernel doesn't
795 * support the feature and just ignored our 'max_beb_per1024'
798 * If the ioctl returns -EINVAL, we assume this is because
799 * 'max_beb_per1024' was set to -1, and we invoke the ioctl for
800 * the second time with the 'max_beb_per1024' value.
802 r.max_beb_per1024 = -1;
803 ret = do_attach(node, &r);
805 req->dev_num = r.ubi_num;
807 * The call succeeded. It means that the kernel ignored
808 * 'max_beb_per1024' parameter.
811 } else if (errno != EINVAL)
815 r.max_beb_per1024 = req->max_beb_per1024;
817 ret = do_attach(node, &r);
819 req->dev_num = r.ubi_num;
824 int ubi_detach_mtd(libubi_t desc, const char *node, int mtd_num)
828 ret = mtd_num2ubi_dev(desc, mtd_num, &ubi_dev);
834 return ubi_remove_dev(desc, node, ubi_dev);
837 int ubi_detach(libubi_t desc, const char *node, const char *mtd_dev_node)
846 mtd_num = mtd_node_to_num(mtd_dev_node);
850 return ubi_detach_mtd(desc, node, mtd_num);
853 int ubi_remove_dev(libubi_t desc, const char *node, int ubi_dev)
859 fd = open(node, O_RDONLY);
861 sys_errmsg("cannot open \"%s\"", node);
864 ret = ioctl(fd, UBI_IOCFDET, &ubi_dev);
868 #ifdef UDEV_SETTLE_HACK
869 // if (system("udevsettle") == -1)
879 int ubi_probe_node(libubi_t desc, const char *node)
882 struct ubi_info info;
883 int i, fd, major, minor;
884 struct libubi *lib = (struct libubi *)desc;
885 char file[strlen(lib->ubi_vol) + 100];
887 if (stat(node, &st)) {
888 sys_errmsg("cannot get information about \"%s\"", node);
892 if (!S_ISCHR(st.st_mode)) {
893 errmsg("\"%s\" is not a character device", node);
898 major = major(st.st_rdev);
899 minor = minor(st.st_rdev);
901 if (ubi_get_info((libubi_t *)lib, &info))
904 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
905 int major1, minor1, ret;
907 ret = dev_get_major(lib, i, &major1, &minor1);
920 if (i > info.highest_dev_num)
926 /* This is supposdely an UBI volume device node */
927 sprintf(file, lib->ubi_vol, i, minor - 1);
928 fd = open(file, O_RDONLY);
935 errmsg("\"%s\" has major:minor %d:%d, but this does not correspond to "
936 "any existing UBI device or volume", node, major, minor);
941 int ubi_get_info(libubi_t desc, struct ubi_info *info)
944 struct dirent *dirent;
945 struct libubi *lib = (struct libubi *)desc;
947 memset(info, 0, sizeof(struct ubi_info));
949 if (read_major(lib->ctrl_dev, &info->ctrl_major, &info->ctrl_minor)) {
951 * Older UBI versions did not have control device, so we do not
952 * panic here for compatibility reasons. May be few years later
953 * we could return -1 here, but for now just set major:minor to
956 info->ctrl_major = info->ctrl_minor = -1;
960 * We have to scan the UBI sysfs directory to identify how many UBI
961 * devices are present.
963 sysfs_ubi = opendir(lib->sysfs_ubi);
967 info->lowest_dev_num = INT_MAX;
973 dirent = readdir(sysfs_ubi);
977 if (strlen(dirent->d_name) >= 255) {
978 errmsg("invalid entry in %s: \"%s\"",
979 lib->sysfs_ubi, dirent->d_name);
984 ret = sscanf(dirent->d_name, UBI_DEV_NAME_PATT"%s",
987 info->dev_count += 1;
988 if (dev_num > info->highest_dev_num)
989 info->highest_dev_num = dev_num;
990 if (dev_num < info->lowest_dev_num)
991 info->lowest_dev_num = dev_num;
995 if (!dirent && errno) {
996 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
1000 if (closedir(sysfs_ubi)) {
1001 sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
1004 if (info->lowest_dev_num == INT_MAX)
1005 info->lowest_dev_num = 0;
1007 if (read_positive_int(lib->ubi_version, &info->version))
1013 closedir(sysfs_ubi);
1017 int ubi_mkvol(libubi_t desc, const char *node, struct ubi_mkvol_request *req)
1020 struct ubi_mkvol_req r;
1023 memset(&r, 0, sizeof(struct ubi_mkvol_req));
1026 r.vol_id = req->vol_id;
1027 r.alignment = req->alignment;
1028 r.bytes = req->bytes;
1029 r.vol_type = req->vol_type;
1031 n = strlen(req->name);
1032 if (n > UBI_MAX_VOLUME_NAME)
1035 strncpy(r.name, req->name, UBI_MAX_VOLUME_NAME + 1);
1039 fd = open(node, O_RDONLY);
1041 sys_errmsg("cannot open \"%s\"", node);
1044 ret = ioctl(fd, UBI_IOCMKVOL, &r);
1051 req->vol_id = r.vol_id;
1053 #ifdef UDEV_SETTLE_HACK
1054 // if (system("udevsettle") == -1)
1062 int ubi_rmvol(libubi_t desc, const char *node, int vol_id)
1067 fd = open(node, O_RDONLY);
1069 sys_errmsg("cannot open \"%s\"", node);
1073 ret = ioctl(fd, UBI_IOCRMVOL, &vol_id);
1081 #ifdef UDEV_SETTLE_HACK
1082 // if (system("udevsettle") == -1)
1090 int ubi_rnvols(libubi_t desc, const char *node, struct ubi_rnvol_req *rnvol)
1095 fd = open(node, O_RDONLY);
1099 ret = ioctl(fd, UBI_IOCRNVOL, rnvol);
1107 #ifdef UDEV_SETTLE_HACK
1108 // if (system("udevsettle") == -1)
1116 int ubi_rsvol(libubi_t desc, const char *node, int vol_id, long long bytes)
1119 struct ubi_rsvol_req req;
1122 fd = open(node, O_RDONLY);
1124 sys_errmsg("cannot open \"%s\"", node);
1128 req.vol_id = vol_id;
1130 ret = ioctl(fd, UBI_IOCRSVOL, &req);
1135 int ubi_update_start(libubi_t desc, int fd, long long bytes)
1138 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
1143 int ubi_leb_change_start(libubi_t desc, int fd, int lnum, int bytes)
1145 struct ubi_leb_change_req req;
1148 memset(&req, 0, sizeof(struct ubi_leb_change_req));
1153 if (ioctl(fd, UBI_IOCEBCH, &req))
1158 int ubi_dev_present(libubi_t desc, int dev_num)
1161 struct libubi *lib = (struct libubi *)desc;
1162 char file[strlen(lib->ubi_dev) + 50];
1164 sprintf(file, lib->ubi_dev, dev_num);
1165 return !stat(file, &st);
1168 int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info)
1171 struct dirent *dirent;
1172 struct libubi *lib = (struct libubi *)desc;
1174 memset(info, 0, sizeof(struct ubi_dev_info));
1175 info->dev_num = dev_num;
1177 if (!ubi_dev_present(desc, dev_num))
1180 sysfs_ubi = opendir(lib->sysfs_ubi);
1184 info->lowest_vol_id = INT_MAX;
1187 int vol_id, ret, devno;
1191 dirent = readdir(sysfs_ubi);
1195 if (strlen(dirent->d_name) >= 255) {
1196 errmsg("invalid entry in %s: \"%s\"",
1197 lib->sysfs_ubi, dirent->d_name);
1201 ret = sscanf(dirent->d_name, UBI_VOL_NAME_PATT"%s", &devno, &vol_id, tmp_buf);
1202 if (ret == 2 && devno == dev_num) {
1203 info->vol_count += 1;
1204 if (vol_id > info->highest_vol_id)
1205 info->highest_vol_id = vol_id;
1206 if (vol_id < info->lowest_vol_id)
1207 info->lowest_vol_id = vol_id;
1211 if (!dirent && errno) {
1212 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
1216 if (closedir(sysfs_ubi)) {
1217 sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
1220 if (info->lowest_vol_id == INT_MAX)
1221 info->lowest_vol_id = 0;
1223 if (dev_get_major(lib, dev_num, &info->major, &info->minor))
1226 if (dev_read_int(lib->dev_mtd_num, dev_num, &info->mtd_num))
1228 if (dev_read_int(lib->dev_avail_ebs, dev_num, &info->avail_lebs))
1230 if (dev_read_int(lib->dev_total_ebs, dev_num, &info->total_lebs))
1232 if (dev_read_int(lib->dev_bad_count, dev_num, &info->bad_count))
1234 if (dev_read_int(lib->dev_eb_size, dev_num, &info->leb_size))
1236 if (dev_read_int(lib->dev_bad_rsvd, dev_num, &info->bad_rsvd))
1238 if (dev_read_ll(lib->dev_max_ec, dev_num, &info->max_ec))
1240 if (dev_read_int(lib->dev_max_vols, dev_num, &info->max_vol_count))
1242 if (dev_read_int(lib->dev_min_io_size, dev_num, &info->min_io_size))
1245 info->avail_bytes = (long long)info->avail_lebs * info->leb_size;
1246 info->total_bytes = (long long)info->total_lebs * info->leb_size;
1251 closedir(sysfs_ubi);
1255 int ubi_get_dev_info(libubi_t desc, const char *node, struct ubi_dev_info *info)
1257 int err, dev_num = 0;
1258 struct libubi *lib = (struct libubi *)desc;
1260 err = ubi_probe_node(desc, node);
1267 if (dev_node2num(lib, node, &dev_num))
1270 return ubi_get_dev_info1(desc, dev_num, info);
1273 int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id,
1274 struct ubi_vol_info *info)
1277 struct libubi *lib = (struct libubi *)desc;
1280 memset(info, 0, sizeof(struct ubi_vol_info));
1281 info->dev_num = dev_num;
1282 info->vol_id = vol_id;
1284 if (vol_get_major(lib, dev_num, vol_id, &info->major, &info->minor))
1287 ret = vol_read_data(lib->vol_type, dev_num, vol_id, buf, 50);
1291 if (strncmp(buf, "static\n", ret) == 0)
1292 info->type = UBI_STATIC_VOLUME;
1293 else if (strncmp(buf, "dynamic\n", ret) == 0)
1294 info->type = UBI_DYNAMIC_VOLUME;
1296 errmsg("bad value at \"%s\"", buf);
1301 ret = vol_read_int(lib->vol_alignment, dev_num, vol_id,
1305 ret = vol_read_ll(lib->vol_data_bytes, dev_num, vol_id,
1309 ret = vol_read_int(lib->vol_rsvd_ebs, dev_num, vol_id, &info->rsvd_lebs);
1312 ret = vol_read_int(lib->vol_eb_size, dev_num, vol_id, &info->leb_size);
1315 ret = vol_read_int(lib->vol_corrupted, dev_num, vol_id,
1319 info->rsvd_bytes = (long long)info->leb_size * info->rsvd_lebs;
1321 ret = vol_read_data(lib->vol_name, dev_num, vol_id, &info->name,
1322 UBI_VOL_NAME_MAX + 2);
1326 info->name[ret - 1] = '\0';
1330 int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info)
1332 int err, vol_id = 0, dev_num = 0;
1333 struct libubi *lib = (struct libubi *)desc;
1335 err = ubi_probe_node(desc, node);
1342 if (vol_node2nums(lib, node, &dev_num, &vol_id))
1345 return ubi_get_vol_info1(desc, dev_num, vol_id, info);
1348 int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
1349 struct ubi_vol_info *info)
1352 unsigned int nlen = strlen(name);
1353 struct ubi_dev_info dev_info;
1356 errmsg("bad \"name\" input parameter");
1361 err = ubi_get_dev_info1(desc, dev_num, &dev_info);
1365 for (i = dev_info.lowest_vol_id;
1366 i <= dev_info.highest_vol_id; i++) {
1367 err = ubi_get_vol_info1(desc, dev_num, i, info);
1369 if (errno == ENOENT)
1374 if (nlen == strlen(info->name) && !strcmp(name, info->name))
1382 int ubi_set_property(int fd, uint8_t property, uint64_t value)
1384 struct ubi_set_vol_prop_req r;
1386 memset(&r, 0, sizeof(struct ubi_set_vol_prop_req));
1387 r.property = property;
1390 return ioctl(fd, UBI_IOCSETVOLPROP, &r);
1393 int ubi_leb_unmap(int fd, int lnum)
1395 return ioctl(fd, UBI_IOCEBUNMAP, &lnum);
1398 int ubi_is_mapped(int fd, int lnum)
1400 return ioctl(fd, UBI_IOCEBISMAP, &lnum);