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
, 4);
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
);
438 * dev_node2num - find UBI device number by its character device node.
439 * @lib: UBI library descriptor
440 * @node: UBI character device node name
441 * @dev_num: UBI device number is returned here
443 * This function returns %0 in case of success and %-1 in case of failure.
445 static int dev_node2num(struct libubi
*lib
, const char *node
, int *dev_num
)
448 struct ubi_info info
;
451 if (stat(node
, &st
)) {
452 sys_errmsg("cannot get information about \"%s\"", node
);
455 if (!S_ISCHR(st
.st_mode
)) {
457 errmsg("\"%s\" is not a character device", node
);
461 major
= major(st
.st_rdev
);
462 minor
= minor(st
.st_rdev
);
466 errmsg("\"%s\" is not an UBI character device", node
);
470 if (ubi_get_info((libubi_t
*)lib
, &info
))
473 for (i
= info
.lowest_dev_num
; i
<= info
.highest_dev_num
; i
++) {
474 int major1
, minor1
, ret
;
476 ret
= dev_get_major(lib
, i
, &major1
, &minor1
);
483 if (major1
== major
) {
485 errmsg("UBI character device minor number is "
486 "%d, but must be 0", minor1
);
500 int mtd_num2ubi_dev(libubi_t desc
, int mtd_num
, int *dev_num
)
502 struct ubi_info info
;
503 int i
, ret
, mtd_num1
;
504 struct libubi
*lib
= desc
;
506 if (ubi_get_info(desc
, &info
))
509 for (i
= info
.lowest_dev_num
; i
<= info
.highest_dev_num
; i
++) {
510 ret
= dev_read_int(lib
->dev_mtd_num
, i
, &mtd_num1
);
517 if (mtd_num1
== mtd_num
) {
528 libubi_t
libubi_open(void)
533 lib
= calloc(1, sizeof(struct libubi
));
537 lib
->sysfs_ctrl
= mkpath("/sys", SYSFS_CTRL
);
538 if (!lib
->sysfs_ctrl
)
541 lib
->ctrl_dev
= mkpath(lib
->sysfs_ctrl
, CTRL_DEV
);
545 lib
->sysfs_ubi
= mkpath("/sys", SYSFS_UBI
);
549 /* Make sure UBI is present */
550 fd
= open(lib
->sysfs_ubi
, O_RDONLY
);
557 sys_errmsg("close failed on \"%s\"", lib
->sysfs_ubi
);
561 lib
->ubi_dev
= mkpath(lib
->sysfs_ubi
, UBI_DEV_NAME_PATT
);
565 lib
->ubi_version
= mkpath(lib
->sysfs_ubi
, UBI_VER
);
566 if (!lib
->ubi_version
)
569 lib
->dev_dev
= mkpath(lib
->ubi_dev
, DEV_DEV
);
573 lib
->dev_avail_ebs
= mkpath(lib
->ubi_dev
, DEV_AVAIL_EBS
);
574 if (!lib
->dev_avail_ebs
)
577 lib
->dev_total_ebs
= mkpath(lib
->ubi_dev
, DEV_TOTAL_EBS
);
578 if (!lib
->dev_total_ebs
)
581 lib
->dev_bad_count
= mkpath(lib
->ubi_dev
, DEV_BAD_COUNT
);
582 if (!lib
->dev_bad_count
)
585 lib
->dev_eb_size
= mkpath(lib
->ubi_dev
, DEV_EB_SIZE
);
586 if (!lib
->dev_eb_size
)
589 lib
->dev_max_ec
= mkpath(lib
->ubi_dev
, DEV_MAX_EC
);
590 if (!lib
->dev_max_ec
)
593 lib
->dev_bad_rsvd
= mkpath(lib
->ubi_dev
, DEV_MAX_RSVD
);
594 if (!lib
->dev_bad_rsvd
)
597 lib
->dev_max_vols
= mkpath(lib
->ubi_dev
, DEV_MAX_VOLS
);
598 if (!lib
->dev_max_vols
)
601 lib
->dev_min_io_size
= mkpath(lib
->ubi_dev
, DEV_MIN_IO_SIZE
);
602 if (!lib
->dev_min_io_size
)
605 lib
->dev_mtd_num
= mkpath(lib
->ubi_dev
, DEV_MTD_NUM
);
606 if (!lib
->dev_mtd_num
)
609 lib
->ubi_vol
= mkpath(lib
->sysfs_ubi
, UBI_VOL_NAME_PATT
);
613 lib
->vol_type
= mkpath(lib
->ubi_vol
, VOL_TYPE
);
617 lib
->vol_dev
= mkpath(lib
->ubi_vol
, VOL_DEV
);
621 lib
->vol_alignment
= mkpath(lib
->ubi_vol
, VOL_ALIGNMENT
);
622 if (!lib
->vol_alignment
)
625 lib
->vol_data_bytes
= mkpath(lib
->ubi_vol
, VOL_DATA_BYTES
);
626 if (!lib
->vol_data_bytes
)
629 lib
->vol_rsvd_ebs
= mkpath(lib
->ubi_vol
, VOL_RSVD_EBS
);
630 if (!lib
->vol_rsvd_ebs
)
633 lib
->vol_eb_size
= mkpath(lib
->ubi_vol
, VOL_EB_SIZE
);
634 if (!lib
->vol_eb_size
)
637 lib
->vol_corrupted
= mkpath(lib
->ubi_vol
, VOL_CORRUPTED
);
638 if (!lib
->vol_corrupted
)
641 lib
->vol_name
= mkpath(lib
->ubi_vol
, VOL_NAME
);
645 if (read_positive_int(lib
->ubi_version
, &version
))
647 if (version
!= LIBUBI_UBI_VERSION
) {
648 errmsg("this library was made for UBI version %d, but UBI "
649 "version %d is detected\n", LIBUBI_UBI_VERSION
, version
);
656 libubi_close((libubi_t
)lib
);
660 void libubi_close(libubi_t desc
)
662 struct libubi
*lib
= (struct libubi
*)desc
;
665 free(lib
->vol_corrupted
);
666 free(lib
->vol_eb_size
);
667 free(lib
->vol_rsvd_ebs
);
668 free(lib
->vol_data_bytes
);
669 free(lib
->vol_alignment
);
673 free(lib
->dev_mtd_num
);
674 free(lib
->dev_min_io_size
);
675 free(lib
->dev_max_vols
);
676 free(lib
->dev_bad_rsvd
);
677 free(lib
->dev_max_ec
);
678 free(lib
->dev_eb_size
);
679 free(lib
->dev_bad_count
);
680 free(lib
->dev_total_ebs
);
681 free(lib
->dev_avail_ebs
);
683 free(lib
->ubi_version
);
685 free(lib
->sysfs_ubi
);
687 free(lib
->sysfs_ctrl
);
692 * do_attach - perform the actual attach operation.
693 * @node: name of the UBI control character device node
696 * This function performs the actual UBI attach operation. Returns %0 in case of
697 * success and %-1 in case of failure. @r->ubi_num contains newly created UBI
700 static int do_attach(const char *node
, const struct ubi_attach_req
*r
)
704 fd
= open(node
, O_RDONLY
);
706 sys_errmsg("cannot open \"%s\"", node
);
709 ret
= ioctl(fd
, UBI_IOCATT
, r
);
714 #ifdef UDEV_SETTLE_HACK
715 // if (system("udevsettle") == -1)
722 #ifndef MTD_CHAR_MAJOR
724 * This is taken from kernel <linux/mtd/mtd.h> and is unlikely to change anytime
727 #define MTD_CHAR_MAJOR 90
731 * mtd_node_to_num - converts device node to MTD number.
732 * @mtd_dev_node: path to device node to convert
734 * This function converts given @mtd_dev_node to MTD device number.
735 * @mtd_dev_node should contain path to the MTD device node. Returns MTD device
736 * number in case of success and %-1 in case of failure (errno is set).
738 static int mtd_node_to_num(const char *mtd_dev_node
)
743 if (stat(mtd_dev_node
, &sb
) < 0) {
744 sys_errmsg("cannot stat \"%s\"", mtd_dev_node
);
748 if (!S_ISCHR(sb
.st_mode
)) {
750 sys_errmsg("\"%s\" is not a character device",
755 major
= major(sb
.st_rdev
);
756 minor
= minor(sb
.st_rdev
);
758 if (major
!= MTD_CHAR_MAJOR
) {
760 sys_errmsg("\"%s\" is not an MTD device", mtd_dev_node
);
767 int ubi_attach(libubi_t desc
, const char *node
, struct ubi_attach_request
*req
)
769 struct ubi_attach_req r
;
774 if (req
->mtd_dev_node
) {
776 * User has passed path to device node. Lets find out MTD
777 * device number of the device and update req->mtd_num with it
779 req
->mtd_num
= mtd_node_to_num(req
->mtd_dev_node
);
780 if (req
->mtd_num
== -1)
784 memset(&r
, 0, sizeof(struct ubi_attach_req
));
785 r
.ubi_num
= req
->dev_num
;
786 r
.mtd_num
= req
->mtd_num
;
787 r
.vid_hdr_offset
= req
->vid_hdr_offset
;
789 if (req
->max_beb_per1024
) {
791 * We first have to check if the running kernel supports the
792 * 'max_beb_per1024' parameter. To do this, we invoke the
793 * "attach" ioctl 2 times: first with incorrect value %-1 of
796 * If the ioctl succeeds, it means that the kernel doesn't
797 * support the feature and just ignored our 'max_beb_per1024'
800 * If the ioctl returns -EINVAL, we assume this is because
801 * 'max_beb_per1024' was set to -1, and we invoke the ioctl for
802 * the second time with the 'max_beb_per1024' value.
804 r
.max_beb_per1024
= -1;
805 ret
= do_attach(node
, &r
);
807 req
->dev_num
= r
.ubi_num
;
809 * The call succeeded. It means that the kernel ignored
810 * 'max_beb_per1024' parameter.
813 } else if (errno
!= EINVAL
)
817 r
.max_beb_per1024
= req
->max_beb_per1024
;
819 ret
= do_attach(node
, &r
);
821 req
->dev_num
= r
.ubi_num
;
826 int ubi_detach_mtd(libubi_t desc
, const char *node
, int mtd_num
)
830 ret
= mtd_num2ubi_dev(desc
, mtd_num
, &ubi_dev
);
836 return ubi_remove_dev(desc
, node
, ubi_dev
);
839 int ubi_detach(libubi_t desc
, const char *node
, const char *mtd_dev_node
)
848 mtd_num
= mtd_node_to_num(mtd_dev_node
);
852 return ubi_detach_mtd(desc
, node
, mtd_num
);
855 int ubi_remove_dev(libubi_t desc
, const char *node
, int ubi_dev
)
861 fd
= open(node
, O_RDONLY
);
863 sys_errmsg("cannot open \"%s\"", node
);
866 ret
= ioctl(fd
, UBI_IOCFDET
, &ubi_dev
);
870 #ifdef UDEV_SETTLE_HACK
871 // if (system("udevsettle") == -1)
881 int ubi_probe_node(libubi_t desc
, const char *node
)
884 struct ubi_info info
;
885 int i
, fd
, major
, minor
;
886 struct libubi
*lib
= (struct libubi
*)desc
;
887 char file
[strlen(lib
->ubi_vol
) + 100];
889 if (stat(node
, &st
)) {
890 sys_errmsg("cannot get information about \"%s\"", node
);
894 if (!S_ISCHR(st
.st_mode
)) {
895 errmsg("\"%s\" is not a character device", node
);
900 major
= major(st
.st_rdev
);
901 minor
= minor(st
.st_rdev
);
903 if (ubi_get_info((libubi_t
*)lib
, &info
))
906 for (i
= info
.lowest_dev_num
; i
<= info
.highest_dev_num
; i
++) {
907 int major1
, minor1
, ret
;
909 ret
= dev_get_major(lib
, i
, &major1
, &minor1
);
922 if (i
> info
.highest_dev_num
)
928 /* This is supposdely an UBI volume device node */
929 sprintf(file
, lib
->ubi_vol
, i
, minor
- 1);
930 fd
= open(file
, O_RDONLY
);
938 errmsg("\"%s\" has major:minor %d:%d, but this does not correspond to "
939 "any existing UBI device or volume", node
, major
, minor
);
944 int ubi_get_info(libubi_t desc
, struct ubi_info
*info
)
947 struct dirent
*dirent
;
948 struct libubi
*lib
= (struct libubi
*)desc
;
950 memset(info
, 0, sizeof(struct ubi_info
));
952 if (read_major(lib
->ctrl_dev
, &info
->ctrl_major
, &info
->ctrl_minor
)) {
954 * Older UBI versions did not have control device, so we do not
955 * panic here for compatibility reasons. May be few years later
956 * we could return -1 here, but for now just set major:minor to
959 info
->ctrl_major
= info
->ctrl_minor
= -1;
963 * We have to scan the UBI sysfs directory to identify how many UBI
964 * devices are present.
966 sysfs_ubi
= opendir(lib
->sysfs_ubi
);
970 info
->lowest_dev_num
= INT_MAX
;
976 dirent
= readdir(sysfs_ubi
);
980 if (strlen(dirent
->d_name
) >= 255) {
981 errmsg("invalid entry in %s: \"%s\"",
982 lib
->sysfs_ubi
, dirent
->d_name
);
987 ret
= sscanf(dirent
->d_name
, UBI_DEV_NAME_PATT
"%s",
990 info
->dev_count
+= 1;
991 if (dev_num
> info
->highest_dev_num
)
992 info
->highest_dev_num
= dev_num
;
993 if (dev_num
< info
->lowest_dev_num
)
994 info
->lowest_dev_num
= dev_num
;
998 if (!dirent
&& errno
) {
999 sys_errmsg("readdir failed on \"%s\"", lib
->sysfs_ubi
);
1003 if (closedir(sysfs_ubi
)) {
1004 sys_errmsg("closedir failed on \"%s\"", lib
->sysfs_ubi
);
1007 if (info
->lowest_dev_num
== INT_MAX
)
1008 info
->lowest_dev_num
= 0;
1010 if (read_positive_int(lib
->ubi_version
, &info
->version
))
1016 closedir(sysfs_ubi
);
1020 int ubi_mkvol(libubi_t desc
, const char *node
, struct ubi_mkvol_request
*req
)
1023 struct ubi_mkvol_req r
;
1026 memset(&r
, 0, sizeof(struct ubi_mkvol_req
));
1029 r
.vol_id
= req
->vol_id
;
1030 r
.alignment
= req
->alignment
;
1031 r
.bytes
= req
->bytes
;
1032 r
.vol_type
= req
->vol_type
;
1034 n
= strlen(req
->name
);
1035 if (n
> UBI_MAX_VOLUME_NAME
)
1038 strncpy(r
.name
, req
->name
, UBI_MAX_VOLUME_NAME
+ 1);
1042 fd
= open(node
, O_RDONLY
);
1044 sys_errmsg("cannot open \"%s\"", node
);
1047 ret
= ioctl(fd
, UBI_IOCMKVOL
, &r
);
1054 req
->vol_id
= r
.vol_id
;
1056 #ifdef UDEV_SETTLE_HACK
1057 // if (system("udevsettle") == -1)
1065 int ubi_rmvol(libubi_t desc
, const char *node
, int vol_id
)
1070 fd
= open(node
, O_RDONLY
);
1072 sys_errmsg("cannot open \"%s\"", node
);
1076 ret
= ioctl(fd
, UBI_IOCRMVOL
, &vol_id
);
1084 #ifdef UDEV_SETTLE_HACK
1085 // if (system("udevsettle") == -1)
1093 int ubi_rnvols(libubi_t desc
, const char *node
, struct ubi_rnvol_req
*rnvol
)
1098 fd
= open(node
, O_RDONLY
);
1102 ret
= ioctl(fd
, UBI_IOCRNVOL
, rnvol
);
1110 #ifdef UDEV_SETTLE_HACK
1111 // if (system("udevsettle") == -1)
1119 int ubi_rsvol(libubi_t desc
, const char *node
, int vol_id
, long long bytes
)
1122 struct ubi_rsvol_req req
;
1125 fd
= open(node
, O_RDONLY
);
1127 sys_errmsg("cannot open \"%s\"", node
);
1131 req
.vol_id
= vol_id
;
1133 ret
= ioctl(fd
, UBI_IOCRSVOL
, &req
);
1138 int ubi_update_start(libubi_t desc
, int fd
, long long bytes
)
1141 if (ioctl(fd
, UBI_IOCVOLUP
, &bytes
))
1146 int ubi_leb_change_start(libubi_t desc
, int fd
, int lnum
, int bytes
)
1148 struct ubi_leb_change_req req
;
1151 memset(&req
, 0, sizeof(struct ubi_leb_change_req
));
1156 if (ioctl(fd
, UBI_IOCEBCH
, &req
))
1161 int ubi_dev_present(libubi_t desc
, int dev_num
)
1164 struct libubi
*lib
= (struct libubi
*)desc
;
1165 char file
[strlen(lib
->ubi_dev
) + 50];
1167 sprintf(file
, lib
->ubi_dev
, dev_num
);
1168 return !stat(file
, &st
);
1171 int ubi_get_dev_info1(libubi_t desc
, int dev_num
, struct ubi_dev_info
*info
)
1174 struct dirent
*dirent
;
1175 struct libubi
*lib
= (struct libubi
*)desc
;
1177 memset(info
, 0, sizeof(struct ubi_dev_info
));
1178 info
->dev_num
= dev_num
;
1180 if (!ubi_dev_present(desc
, dev_num
))
1183 sysfs_ubi
= opendir(lib
->sysfs_ubi
);
1187 info
->lowest_vol_id
= INT_MAX
;
1190 int vol_id
, ret
, devno
;
1194 dirent
= readdir(sysfs_ubi
);
1198 if (strlen(dirent
->d_name
) >= 255) {
1199 errmsg("invalid entry in %s: \"%s\"",
1200 lib
->sysfs_ubi
, dirent
->d_name
);
1204 ret
= sscanf(dirent
->d_name
, UBI_VOL_NAME_PATT
"%s", &devno
, &vol_id
, tmp_buf
);
1205 if (ret
== 2 && devno
== dev_num
) {
1206 info
->vol_count
+= 1;
1207 if (vol_id
> info
->highest_vol_id
)
1208 info
->highest_vol_id
= vol_id
;
1209 if (vol_id
< info
->lowest_vol_id
)
1210 info
->lowest_vol_id
= vol_id
;
1214 if (!dirent
&& errno
) {
1215 sys_errmsg("readdir failed on \"%s\"", lib
->sysfs_ubi
);
1219 if (closedir(sysfs_ubi
)) {
1220 sys_errmsg("closedir failed on \"%s\"", lib
->sysfs_ubi
);
1223 if (info
->lowest_vol_id
== INT_MAX
)
1224 info
->lowest_vol_id
= 0;
1226 if (dev_get_major(lib
, dev_num
, &info
->major
, &info
->minor
))
1229 if (dev_read_int(lib
->dev_mtd_num
, dev_num
, &info
->mtd_num
))
1231 if (dev_read_int(lib
->dev_avail_ebs
, dev_num
, &info
->avail_lebs
))
1233 if (dev_read_int(lib
->dev_total_ebs
, dev_num
, &info
->total_lebs
))
1235 if (dev_read_int(lib
->dev_bad_count
, dev_num
, &info
->bad_count
))
1237 if (dev_read_int(lib
->dev_eb_size
, dev_num
, &info
->leb_size
))
1239 if (dev_read_int(lib
->dev_bad_rsvd
, dev_num
, &info
->bad_rsvd
))
1241 if (dev_read_ll(lib
->dev_max_ec
, dev_num
, &info
->max_ec
))
1243 if (dev_read_int(lib
->dev_max_vols
, dev_num
, &info
->max_vol_count
))
1245 if (dev_read_int(lib
->dev_min_io_size
, dev_num
, &info
->min_io_size
))
1248 info
->avail_bytes
= (long long)info
->avail_lebs
* info
->leb_size
;
1249 info
->total_bytes
= (long long)info
->total_lebs
* info
->leb_size
;
1254 closedir(sysfs_ubi
);
1258 int ubi_get_dev_info(libubi_t desc
, const char *node
, struct ubi_dev_info
*info
)
1260 int err
, dev_num
= 0;
1261 struct libubi
*lib
= (struct libubi
*)desc
;
1263 err
= ubi_probe_node(desc
, node
);
1270 if (dev_node2num(lib
, node
, &dev_num
))
1273 return ubi_get_dev_info1(desc
, dev_num
, info
);
1276 int ubi_get_vol_info1(libubi_t desc
, int dev_num
, int vol_id
,
1277 struct ubi_vol_info
*info
)
1280 struct libubi
*lib
= (struct libubi
*)desc
;
1283 memset(info
, 0, sizeof(struct ubi_vol_info
));
1284 info
->dev_num
= dev_num
;
1285 info
->vol_id
= vol_id
;
1287 if (vol_get_major(lib
, dev_num
, vol_id
, &info
->major
, &info
->minor
))
1290 ret
= vol_read_data(lib
->vol_type
, dev_num
, vol_id
, buf
, 50);
1294 if (strncmp(buf
, "static\n", ret
) == 0)
1295 info
->type
= UBI_STATIC_VOLUME
;
1296 else if (strncmp(buf
, "dynamic\n", ret
) == 0)
1297 info
->type
= UBI_DYNAMIC_VOLUME
;
1299 errmsg("bad value at \"%s\"", buf
);
1304 ret
= vol_read_int(lib
->vol_alignment
, dev_num
, vol_id
,
1308 ret
= vol_read_ll(lib
->vol_data_bytes
, dev_num
, vol_id
,
1312 ret
= vol_read_int(lib
->vol_rsvd_ebs
, dev_num
, vol_id
, &info
->rsvd_lebs
);
1315 ret
= vol_read_int(lib
->vol_eb_size
, dev_num
, vol_id
, &info
->leb_size
);
1318 ret
= vol_read_int(lib
->vol_corrupted
, dev_num
, vol_id
,
1322 info
->rsvd_bytes
= (long long)info
->leb_size
* info
->rsvd_lebs
;
1324 ret
= vol_read_data(lib
->vol_name
, dev_num
, vol_id
, &info
->name
,
1325 UBI_VOL_NAME_MAX
+ 1);
1329 info
->name
[ret
- 1] = '\0';
1333 int ubi_get_vol_info(libubi_t desc
, const char *node
, struct ubi_vol_info
*info
)
1335 int err
, vol_id
= 0, dev_num
= 0;
1336 struct libubi
*lib
= (struct libubi
*)desc
;
1338 err
= ubi_probe_node(desc
, node
);
1345 if (vol_node2nums(lib
, node
, &dev_num
, &vol_id
))
1348 return ubi_get_vol_info1(desc
, dev_num
, vol_id
, info
);
1351 int ubi_get_vol_info1_nm(libubi_t desc
, int dev_num
, const char *name
,
1352 struct ubi_vol_info
*info
)
1355 unsigned int nlen
= strlen(name
);
1356 struct ubi_dev_info dev_info
;
1359 errmsg("bad \"name\" input parameter");
1364 err
= ubi_get_dev_info1(desc
, dev_num
, &dev_info
);
1368 for (i
= dev_info
.lowest_vol_id
;
1369 i
<= dev_info
.highest_vol_id
; i
++) {
1370 err
= ubi_get_vol_info1(desc
, dev_num
, i
, info
);
1372 if (errno
== ENOENT
)
1377 if (nlen
== strlen(info
->name
) && !strcmp(name
, info
->name
))
1385 int ubi_set_property(int fd
, uint8_t property
, uint64_t value
)
1387 struct ubi_set_vol_prop_req r
;
1389 memset(&r
, 0, sizeof(struct ubi_set_vol_prop_req
));
1390 r
.property
= property
;
1393 return ioctl(fd
, UBI_IOCSETVOLPROP
, &r
);
1396 int ubi_leb_unmap(int fd
, int lnum
)
1398 return ioctl(fd
, UBI_IOCEBUNMAP
, &lnum
);
1401 int ubi_is_mapped(int fd
, int lnum
)
1403 return ioctl(fd
, UBI_IOCEBISMAP
, &lnum
);