3494f9dbdd558c94daea0924c1021db7d74513a1
[project/fstools.git] / libubi / libubi.c
1 /*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
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.
8 *
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.
13 *
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.
17 *
18 * Author: Artem Bityutskiy
19 *
20 * UBI (Unsorted Block Images) library.
21 */
22
23 #define PROGRAM_NAME "libubi"
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <dirent.h>
30 #include <unistd.h>
31 #include <limits.h>
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include "libubi.h"
36 #include "libubi_int.h"
37
38 /**
39 * mkpath - compose full path from 2 given components.
40 * @path: the first component
41 * @name: the second component
42 *
43 * This function returns the resulting path in case of success and %NULL in
44 * case of failure.
45 */
46 static char *mkpath(const char *path, const char *name)
47 {
48 char *n;
49 int len1 = strlen(path);
50 int len2 = strlen(name);
51
52 n = malloc(len1 + len2 + 2);
53 if (!n) {
54 sys_errmsg("cannot allocate %d bytes", len1 + len2 + 2);
55 return NULL;
56 }
57
58 memcpy(n, path, len1);
59 if (n[len1 - 1] != '/')
60 n[len1++] = '/';
61
62 memcpy(n + len1, name, len2 + 1);
63 return n;
64 }
65
66 /**
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
70 *
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.
74 */
75 static int read_positive_ll(const char *file, long long *value)
76 {
77 int fd, rd;
78 char buf[50];
79
80 fd = open(file, O_RDONLY);
81 if (fd == -1)
82 return -1;
83
84 rd = read(fd, buf, sizeof(buf));
85 if (rd == -1) {
86 sys_errmsg("cannot read \"%s\"", file);
87 goto out_error;
88 }
89 if (rd == sizeof(buf)) {
90 errmsg("contents of \"%s\" is too long", file);
91 errno = EINVAL;
92 goto out_error;
93 }
94 buf[rd] = '\0';
95
96 if (sscanf(buf, "%lld\n", value) != 1) {
97 errmsg("cannot read integer from \"%s\"\n", file);
98 errno = EINVAL;
99 goto out_error;
100 }
101
102 if (*value < 0) {
103 errmsg("negative value %lld in \"%s\"", *value, file);
104 errno = EINVAL;
105 goto out_error;
106 }
107
108 if (close(fd)) {
109 sys_errmsg("close failed on \"%s\"", file);
110 return -1;
111 }
112
113 return 0;
114
115 out_error:
116 close(fd);
117 return -1;
118 }
119
120 /**
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
124 *
125 * This function is the same as 'read_positive_ll()', but it reads an 'int'
126 * value, not 'long long'.
127 */
128 static int read_positive_int(const char *file, int *value)
129 {
130 long long res;
131
132 if (read_positive_ll(file, &res))
133 return -1;
134
135 /* Make sure the value is not too big */
136 if (res > INT_MAX) {
137 errmsg("value %lld read from file \"%s\" is out of range",
138 res, file);
139 errno = EINVAL;
140 return -1;
141 }
142
143 *value = res;
144 return 0;
145 }
146
147 /**
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
152 *
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.
156 */
157 static int read_data(const char *file, void *buf, int buf_len)
158 {
159 int fd, rd, tmp, tmp1;
160
161 fd = open(file, O_RDONLY);
162 if (fd == -1)
163 return -1;
164
165 rd = read(fd, buf, buf_len);
166 if (rd == -1) {
167 sys_errmsg("cannot read \"%s\"", file);
168 goto out_error;
169 }
170
171 if (rd == buf_len) {
172 errmsg("contents of \"%s\" is too long", file);
173 errno = EINVAL;
174 goto out_error;
175 }
176
177 ((char *)buf)[rd] = '\0';
178
179 /* Make sure all data is read */
180 tmp1 = read(fd, &tmp, 1);
181 if (tmp1 == 1) {
182 sys_errmsg("cannot read \"%s\"", file);
183 goto out_error;
184 }
185 if (tmp1) {
186 errmsg("file \"%s\" contains too much data (> %d bytes)",
187 file, buf_len);
188 errno = EINVAL;
189 goto out_error;
190 }
191
192 if (close(fd)) {
193 sys_errmsg("close failed on \"%s\"", file);
194 return -1;
195 }
196
197 return rd;
198
199 out_error:
200 close(fd);
201 return -1;
202 }
203
204 /**
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
209 *
210 * This function returns % in case of succes, and %-1 in case of failure.
211 */
212 static int read_major(const char *file, int *major, int *minor)
213 {
214 int ret;
215 char buf[50];
216
217 ret = read_data(file, buf, 50);
218 if (ret < 0)
219 return ret;
220
221 ret = sscanf(buf, "%d:%d\n", major, minor);
222 if (ret != 2) {
223 errno = EINVAL;
224 errmsg("\"%s\" does not have major:minor format", file);
225 return -1;
226 }
227
228 if (*major < 0 || *minor < 0) {
229 errno = EINVAL;
230 errmsg("bad major:minor %d:%d in \"%s\"",
231 *major, *minor, file);
232 return -1;
233 }
234
235 return 0;
236 }
237
238 /**
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
243 *
244 * This function returns %0 in case of success and %-1 in case of failure.
245 */
246 static int dev_read_int(const char *patt, int dev_num, int *value)
247 {
248 char file[strlen(patt) + 50];
249
250 sprintf(file, patt, dev_num);
251 return read_positive_int(file, value);
252 }
253
254 /**
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
258 * @vol_id: volume ID
259 * @value: the result is stored here
260 *
261 * This function returns %0 in case of success and %-1 in case of failure.
262 */
263 static int vol_read_int(const char *patt, int dev_num, int vol_id, int *value)
264 {
265 char file[strlen(patt) + 100];
266
267 sprintf(file, patt, dev_num, vol_id);
268 return read_positive_int(file, value);
269 }
270
271 /**
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
276 *
277 * This function returns %0 in case of success and %-1 in case of failure.
278 */
279 static int dev_read_ll(const char *patt, int dev_num, long long *value)
280 {
281 char file[strlen(patt) + 50];
282
283 sprintf(file, patt, dev_num);
284 return read_positive_ll(file, value);
285 }
286
287 /**
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
291 * @vol_id: volume ID
292 * @value: the result is stored here
293 *
294 * This function returns %0 in case of success and %-1 in case of failure.
295 */
296 static int vol_read_ll(const char *patt, int dev_num, int vol_id,
297 long long *value)
298 {
299 char file[strlen(patt) + 100];
300
301 sprintf(file, patt, dev_num, vol_id);
302 return read_positive_ll(file, value);
303 }
304
305 /**
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
309 * @vol_id: volume ID
310 * @buf: buffer to read to
311 * @buf_len: buffer length
312 *
313 * This function returns number of read bytes in case of success and %-1 in
314 * case of failure.
315 */
316 static int vol_read_data(const char *patt, int dev_num, int vol_id, void *buf,
317 int buf_len)
318 {
319 char file[strlen(patt) + 100];
320
321 sprintf(file, patt, dev_num, vol_id);
322 return read_data(file, buf, buf_len);
323 }
324
325 /**
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
331 *
332 * This function returns zero in case of succes and %-1 in case of failure.
333 */
334 static int dev_get_major(struct libubi *lib, int dev_num, int *major, int *minor)
335 {
336 char file[strlen(lib->dev_dev) + 50];
337
338 sprintf(file, lib->dev_dev, dev_num);
339 return read_major(file, major, minor);
340 }
341
342 /**
343 * vol_get_major - get major and minor numbers of an UBI volume.
344 * @lib: libubi descriptor
345 * @dev_num: UBI device number
346 * @vol_id: volume ID
347 * @major: major number is returned here
348 * @minor: minor number is returned here
349 *
350 * This function returns zero in case of succes and %-1 in case of failure.
351 */
352 static int vol_get_major(struct libubi *lib, int dev_num, int vol_id,
353 int *major, int *minor)
354 {
355 char file[strlen(lib->vol_dev) + 100];
356
357 sprintf(file, lib->vol_dev, dev_num, vol_id);
358 return read_major(file, major, minor);
359 }
360
361 /**
362 * vol_node2nums - find UBI device number and volume ID by volume device node
363 * file.
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
368 *
369 * This function returns zero in case of succes and %-1 in case of failure.
370 */
371 static int vol_node2nums(struct libubi *lib, const char *node, int *dev_num,
372 int *vol_id)
373 {
374 struct stat st;
375 struct ubi_info info;
376 int i, fd, major, minor;
377 char file[strlen(lib->ubi_vol) + 100];
378
379 if (stat(node, &st)) {
380 sys_errmsg("cannot get information about \"%s\"",
381 node);
382 return -1;
383 }
384 if (!S_ISCHR(st.st_mode)) {
385 errno = EINVAL;
386 errmsg("\"%s\" is not a character device", node);
387 return -1;
388 }
389
390 major = major(st.st_rdev);
391 minor = minor(st.st_rdev);
392
393 if (minor == 0) {
394 errno = EINVAL;
395 errmsg("\"%s\" is not a volume character device", node);
396 return -1;
397 }
398
399 if (ubi_get_info((libubi_t *)lib, &info))
400 return -1;
401
402 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
403 int major1, minor1, ret;
404
405 ret = dev_get_major(lib, i, &major1, &minor1);
406 if (ret) {
407 if (errno == ENOENT)
408 continue;
409 return -1;
410 }
411
412 if (major1 == major)
413 break;
414 }
415
416 if (i > info.highest_dev_num) {
417 errno = ENODEV;
418 return -1;
419 }
420
421 /* Make sure this UBI volume exists */
422 sprintf(file, lib->ubi_vol, i, minor - 1);
423 fd = open(file, O_RDONLY);
424 if (fd == -1) {
425 errno = ENODEV;
426 return -1;
427 }
428
429 *dev_num = i;
430 *vol_id = minor - 1;
431 errno = 0;
432 return 0;
433 }
434
435 /**
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
440 *
441 * This function returns %0 in case of success and %-1 in case of failure.
442 */
443 static int dev_node2num(struct libubi *lib, const char *node, int *dev_num)
444 {
445 struct stat st;
446 struct ubi_info info;
447 int i, major, minor;
448
449 if (stat(node, &st)) {
450 sys_errmsg("cannot get information about \"%s\"", node);
451 return -1;
452 }
453 if (!S_ISCHR(st.st_mode)) {
454 errno = EINVAL;
455 errmsg("\"%s\" is not a character device", node);
456 return -1;
457 }
458
459 major = major(st.st_rdev);
460 minor = minor(st.st_rdev);
461
462 if (minor != 0) {
463 errno = EINVAL;
464 errmsg("\"%s\" is not an UBI character device", node);
465 return -1;
466 }
467
468 if (ubi_get_info((libubi_t *)lib, &info))
469 return -1;
470
471 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
472 int major1, minor1, ret;
473
474 ret = dev_get_major(lib, i, &major1, &minor1);
475 if (ret) {
476 if (errno == ENOENT)
477 continue;
478 return -1;
479 }
480
481 if (major1 == major) {
482 if (minor1 != 0) {
483 errmsg("UBI character device minor number is "
484 "%d, but must be 0", minor1);
485 errno = EINVAL;
486 return -1;
487 }
488 errno = 0;
489 *dev_num = i;
490 return 0;
491 }
492 }
493
494 errno = ENODEV;
495 return -1;
496 }
497
498 int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num)
499 {
500 struct ubi_info info;
501 int i, ret, mtd_num1;
502 struct libubi *lib = desc;
503
504 if (ubi_get_info(desc, &info))
505 return -1;
506
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);
509 if (ret) {
510 if (errno == ENOENT)
511 continue;
512 return -1;
513 }
514
515 if (mtd_num1 == mtd_num) {
516 errno = 0;
517 *dev_num = i;
518 return 0;
519 }
520 }
521
522 errno = 0;
523 return -1;
524 }
525
526 libubi_t libubi_open(void)
527 {
528 int fd, version;
529 struct libubi *lib;
530
531 lib = calloc(1, sizeof(struct libubi));
532 if (!lib)
533 return NULL;
534
535 lib->sysfs_ctrl = mkpath("/sys", SYSFS_CTRL);
536 if (!lib->sysfs_ctrl)
537 goto out_error;
538
539 lib->ctrl_dev = mkpath(lib->sysfs_ctrl, CTRL_DEV);
540 if (!lib->ctrl_dev)
541 goto out_error;
542
543 lib->sysfs_ubi = mkpath("/sys", SYSFS_UBI);
544 if (!lib->sysfs_ubi)
545 goto out_error;
546
547 /* Make sure UBI is present */
548 fd = open(lib->sysfs_ubi, O_RDONLY);
549 if (fd == -1) {
550 errno = 0;
551 goto out_error;
552 }
553
554 if (close(fd)) {
555 sys_errmsg("close failed on \"%s\"", lib->sysfs_ubi);
556 goto out_error;
557 }
558
559 lib->ubi_dev = mkpath(lib->sysfs_ubi, UBI_DEV_NAME_PATT);
560 if (!lib->ubi_dev)
561 goto out_error;
562
563 lib->ubi_version = mkpath(lib->sysfs_ubi, UBI_VER);
564 if (!lib->ubi_version)
565 goto out_error;
566
567 lib->dev_dev = mkpath(lib->ubi_dev, DEV_DEV);
568 if (!lib->dev_dev)
569 goto out_error;
570
571 lib->dev_avail_ebs = mkpath(lib->ubi_dev, DEV_AVAIL_EBS);
572 if (!lib->dev_avail_ebs)
573 goto out_error;
574
575 lib->dev_total_ebs = mkpath(lib->ubi_dev, DEV_TOTAL_EBS);
576 if (!lib->dev_total_ebs)
577 goto out_error;
578
579 lib->dev_bad_count = mkpath(lib->ubi_dev, DEV_BAD_COUNT);
580 if (!lib->dev_bad_count)
581 goto out_error;
582
583 lib->dev_eb_size = mkpath(lib->ubi_dev, DEV_EB_SIZE);
584 if (!lib->dev_eb_size)
585 goto out_error;
586
587 lib->dev_max_ec = mkpath(lib->ubi_dev, DEV_MAX_EC);
588 if (!lib->dev_max_ec)
589 goto out_error;
590
591 lib->dev_bad_rsvd = mkpath(lib->ubi_dev, DEV_MAX_RSVD);
592 if (!lib->dev_bad_rsvd)
593 goto out_error;
594
595 lib->dev_max_vols = mkpath(lib->ubi_dev, DEV_MAX_VOLS);
596 if (!lib->dev_max_vols)
597 goto out_error;
598
599 lib->dev_min_io_size = mkpath(lib->ubi_dev, DEV_MIN_IO_SIZE);
600 if (!lib->dev_min_io_size)
601 goto out_error;
602
603 lib->dev_mtd_num = mkpath(lib->ubi_dev, DEV_MTD_NUM);
604 if (!lib->dev_mtd_num)
605 goto out_error;
606
607 lib->ubi_vol = mkpath(lib->sysfs_ubi, UBI_VOL_NAME_PATT);
608 if (!lib->ubi_vol)
609 goto out_error;
610
611 lib->vol_type = mkpath(lib->ubi_vol, VOL_TYPE);
612 if (!lib->vol_type)
613 goto out_error;
614
615 lib->vol_dev = mkpath(lib->ubi_vol, VOL_DEV);
616 if (!lib->vol_dev)
617 goto out_error;
618
619 lib->vol_alignment = mkpath(lib->ubi_vol, VOL_ALIGNMENT);
620 if (!lib->vol_alignment)
621 goto out_error;
622
623 lib->vol_data_bytes = mkpath(lib->ubi_vol, VOL_DATA_BYTES);
624 if (!lib->vol_data_bytes)
625 goto out_error;
626
627 lib->vol_rsvd_ebs = mkpath(lib->ubi_vol, VOL_RSVD_EBS);
628 if (!lib->vol_rsvd_ebs)
629 goto out_error;
630
631 lib->vol_eb_size = mkpath(lib->ubi_vol, VOL_EB_SIZE);
632 if (!lib->vol_eb_size)
633 goto out_error;
634
635 lib->vol_corrupted = mkpath(lib->ubi_vol, VOL_CORRUPTED);
636 if (!lib->vol_corrupted)
637 goto out_error;
638
639 lib->vol_name = mkpath(lib->ubi_vol, VOL_NAME);
640 if (!lib->vol_name)
641 goto out_error;
642
643 if (read_positive_int(lib->ubi_version, &version))
644 goto out_error;
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);
648 goto out_error;
649 }
650
651 return lib;
652
653 out_error:
654 libubi_close((libubi_t)lib);
655 return NULL;
656 }
657
658 void libubi_close(libubi_t desc)
659 {
660 struct libubi *lib = (struct libubi *)desc;
661
662 free(lib->vol_name);
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);
668 free(lib->vol_dev);
669 free(lib->vol_type);
670 free(lib->ubi_vol);
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);
680 free(lib->dev_dev);
681 free(lib->ubi_version);
682 free(lib->ubi_dev);
683 free(lib->sysfs_ubi);
684 free(lib->ctrl_dev);
685 free(lib->sysfs_ctrl);
686 free(lib);
687 }
688
689 /**
690 * do_attach - perform the actual attach operation.
691 * @node: name of the UBI control character device node
692 * @r: attach request
693 *
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
696 * device number.
697 */
698 static int do_attach(const char *node, const struct ubi_attach_req *r)
699 {
700 int fd, ret;
701
702 fd = open(node, O_RDONLY);
703 if (fd == -1) {
704 sys_errmsg("cannot open \"%s\"", node);
705 return -1;
706 }
707 ret = ioctl(fd, UBI_IOCATT, r);
708 close(fd);
709 if (ret == -1)
710 return -1;
711
712 #ifdef UDEV_SETTLE_HACK
713 // if (system("udevsettle") == -1)
714 // return -1;
715 usleep(100000);
716 #endif
717 return ret;
718 }
719
720 #ifndef MTD_CHAR_MAJOR
721 /*
722 * This is taken from kernel <linux/mtd/mtd.h> and is unlikely to change anytime
723 * soon.
724 */
725 #define MTD_CHAR_MAJOR 90
726 #endif
727
728 /**
729 * mtd_node_to_num - converts device node to MTD number.
730 * @mtd_dev_node: path to device node to convert
731 *
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).
735 */
736 static int mtd_node_to_num(const char *mtd_dev_node)
737 {
738 int major, minor;
739 struct stat sb;
740
741 if (stat(mtd_dev_node, &sb) < 0) {
742 sys_errmsg("cannot stat \"%s\"", mtd_dev_node);
743 return -1;
744 }
745
746 if (!S_ISCHR(sb.st_mode)) {
747 errno = EINVAL;
748 sys_errmsg("\"%s\" is not a character device",
749 mtd_dev_node);
750 return -1;
751 }
752
753 major = major(sb.st_rdev);
754 minor = minor(sb.st_rdev);
755
756 if (major != MTD_CHAR_MAJOR) {
757 errno = EINVAL;
758 sys_errmsg("\"%s\" is not an MTD device", mtd_dev_node);
759 return -1;
760 }
761
762 return minor / 2;
763 }
764
765 int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
766 {
767 struct ubi_attach_req r;
768 int ret;
769
770 (void)desc;
771
772 if (req->mtd_dev_node) {
773 /*
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
776 */
777 req->mtd_num = mtd_node_to_num(req->mtd_dev_node);
778 if (req->mtd_num == -1)
779 return -1;
780 }
781
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;
786
787 if (req->max_beb_per1024) {
788 /*
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
792 * 'max_beb_per1024'.
793 *
794 * If the ioctl succeeds, it means that the kernel doesn't
795 * support the feature and just ignored our 'max_beb_per1024'
796 * value.
797 *
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.
801 */
802 r.max_beb_per1024 = -1;
803 ret = do_attach(node, &r);
804 if (ret == 0) {
805 req->dev_num = r.ubi_num;
806 /*
807 * The call succeeded. It means that the kernel ignored
808 * 'max_beb_per1024' parameter.
809 */
810 return 1;
811 } else if (errno != EINVAL)
812 return ret;
813 }
814
815 r.max_beb_per1024 = req->max_beb_per1024;
816
817 ret = do_attach(node, &r);
818 if (ret == 0)
819 req->dev_num = r.ubi_num;
820
821 return ret;
822 }
823
824 int ubi_detach_mtd(libubi_t desc, const char *node, int mtd_num)
825 {
826 int ret, ubi_dev;
827
828 ret = mtd_num2ubi_dev(desc, mtd_num, &ubi_dev);
829 if (ret == -1) {
830 errno = ENODEV;
831 return ret;
832 }
833
834 return ubi_remove_dev(desc, node, ubi_dev);
835 }
836
837 int ubi_detach(libubi_t desc, const char *node, const char *mtd_dev_node)
838 {
839 int mtd_num;
840
841 if (!mtd_dev_node) {
842 errno = EINVAL;
843 return -1;
844 }
845
846 mtd_num = mtd_node_to_num(mtd_dev_node);
847 if (mtd_num == -1)
848 return -1;
849
850 return ubi_detach_mtd(desc, node, mtd_num);
851 }
852
853 int ubi_remove_dev(libubi_t desc, const char *node, int ubi_dev)
854 {
855 int fd, ret;
856
857 desc = desc;
858
859 fd = open(node, O_RDONLY);
860 if (fd == -1) {
861 sys_errmsg("cannot open \"%s\"", node);
862 return -1;
863 }
864 ret = ioctl(fd, UBI_IOCDET, &ubi_dev);
865 if (ret == -1)
866 goto out_close;
867
868 #ifdef UDEV_SETTLE_HACK
869 // if (system("udevsettle") == -1)
870 // return -1;
871 usleep(100000);
872 #endif
873
874 out_close:
875 close(fd);
876 return ret;
877 }
878
879 int ubi_probe_node(libubi_t desc, const char *node)
880 {
881 struct stat st;
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];
886
887 if (stat(node, &st)) {
888 sys_errmsg("cannot get information about \"%s\"", node);
889 return -1;
890 }
891
892 if (!S_ISCHR(st.st_mode)) {
893 errmsg("\"%s\" is not a character device", node);
894 errno = EINVAL;
895 return -1;
896 }
897
898 major = major(st.st_rdev);
899 minor = minor(st.st_rdev);
900
901 if (ubi_get_info((libubi_t *)lib, &info))
902 return -1;
903
904 for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
905 int major1, minor1, ret;
906
907 ret = dev_get_major(lib, i, &major1, &minor1);
908 if (ret) {
909 if (errno == ENOENT)
910 continue;
911 if (!errno)
912 goto out_not_ubi;
913 return -1;
914 }
915
916 if (major1 == major)
917 break;
918 }
919
920 if (i > info.highest_dev_num)
921 goto out_not_ubi;
922
923 if (minor == 0)
924 return 1;
925
926 /* This is supposdely an UBI volume device node */
927 sprintf(file, lib->ubi_vol, i, minor - 1);
928 fd = open(file, O_RDONLY);
929 if (fd == -1)
930 goto out_not_ubi;
931
932 return 2;
933
934 out_not_ubi:
935 errmsg("\"%s\" has major:minor %d:%d, but this does not correspond to "
936 "any existing UBI device or volume", node, major, minor);
937 errno = ENODEV;
938 return -1;
939 }
940
941 int ubi_get_info(libubi_t desc, struct ubi_info *info)
942 {
943 DIR *sysfs_ubi;
944 struct dirent *dirent;
945 struct libubi *lib = (struct libubi *)desc;
946
947 memset(info, 0, sizeof(struct ubi_info));
948
949 if (read_major(lib->ctrl_dev, &info->ctrl_major, &info->ctrl_minor)) {
950 /*
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
954 * -1.
955 */
956 info->ctrl_major = info->ctrl_minor = -1;
957 }
958
959 /*
960 * We have to scan the UBI sysfs directory to identify how many UBI
961 * devices are present.
962 */
963 sysfs_ubi = opendir(lib->sysfs_ubi);
964 if (!sysfs_ubi)
965 return -1;
966
967 info->lowest_dev_num = INT_MAX;
968 while (1) {
969 int dev_num, ret;
970 char tmp_buf[256];
971
972 errno = 0;
973 dirent = readdir(sysfs_ubi);
974 if (!dirent)
975 break;
976
977 if (strlen(dirent->d_name) >= 255) {
978 errmsg("invalid entry in %s: \"%s\"",
979 lib->sysfs_ubi, dirent->d_name);
980 errno = EINVAL;
981 goto out_close;
982 }
983
984 ret = sscanf(dirent->d_name, UBI_DEV_NAME_PATT"%s",
985 &dev_num, tmp_buf);
986 if (ret == 1) {
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;
992 }
993 }
994
995 if (!dirent && errno) {
996 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
997 goto out_close;
998 }
999
1000 if (closedir(sysfs_ubi)) {
1001 sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
1002 return -1;
1003 }
1004 if (info->lowest_dev_num == INT_MAX)
1005 info->lowest_dev_num = 0;
1006
1007 if (read_positive_int(lib->ubi_version, &info->version))
1008 return -1;
1009
1010 return 0;
1011
1012 out_close:
1013 closedir(sysfs_ubi);
1014 return -1;
1015 }
1016
1017 int ubi_mkvol(libubi_t desc, const char *node, struct ubi_mkvol_request *req)
1018 {
1019 int fd, ret;
1020 struct ubi_mkvol_req r;
1021 size_t n;
1022
1023 memset(&r, 0, sizeof(struct ubi_mkvol_req));
1024
1025 desc = desc;
1026 r.vol_id = req->vol_id;
1027 r.alignment = req->alignment;
1028 r.bytes = req->bytes;
1029 r.vol_type = req->vol_type;
1030
1031 n = strlen(req->name);
1032 if (n > UBI_MAX_VOLUME_NAME)
1033 return -1;
1034
1035 strncpy(r.name, req->name, UBI_MAX_VOLUME_NAME + 1);
1036 r.name_len = n;
1037
1038 desc = desc;
1039 fd = open(node, O_RDONLY);
1040 if (fd == -1) {
1041 sys_errmsg("cannot open \"%s\"", node);
1042 return -1;
1043 }
1044 ret = ioctl(fd, UBI_IOCMKVOL, &r);
1045 if (ret == -1) {
1046 close(fd);
1047 return ret;
1048 }
1049
1050 close(fd);
1051 req->vol_id = r.vol_id;
1052
1053 #ifdef UDEV_SETTLE_HACK
1054 // if (system("udevsettle") == -1)
1055 // return -1;
1056 usleep(100000);
1057 #endif
1058
1059 return 0;
1060 }
1061
1062 int ubi_rmvol(libubi_t desc, const char *node, int vol_id)
1063 {
1064 int fd, ret;
1065
1066 desc = desc;
1067 fd = open(node, O_RDONLY);
1068 if (fd == -1) {
1069 sys_errmsg("cannot open \"%s\"", node);
1070 return -1;
1071 }
1072
1073 ret = ioctl(fd, UBI_IOCRMVOL, &vol_id);
1074 if (ret == -1) {
1075 close(fd);
1076 return ret;
1077 }
1078
1079 close(fd);
1080
1081 #ifdef UDEV_SETTLE_HACK
1082 // if (system("udevsettle") == -1)
1083 // return -1;
1084 usleep(100000);
1085 #endif
1086
1087 return 0;
1088 }
1089
1090 int ubi_rnvols(libubi_t desc, const char *node, struct ubi_rnvol_req *rnvol)
1091 {
1092 int fd, ret;
1093
1094 desc = desc;
1095 fd = open(node, O_RDONLY);
1096 if (fd == -1)
1097 return -1;
1098
1099 ret = ioctl(fd, UBI_IOCRNVOL, rnvol);
1100 if (ret == -1) {
1101 close(fd);
1102 return ret;
1103 }
1104
1105 close(fd);
1106
1107 #ifdef UDEV_SETTLE_HACK
1108 // if (system("udevsettle") == -1)
1109 // return -1;
1110 usleep(100000);
1111 #endif
1112
1113 return 0;
1114 }
1115
1116 int ubi_rsvol(libubi_t desc, const char *node, int vol_id, long long bytes)
1117 {
1118 int fd, ret;
1119 struct ubi_rsvol_req req;
1120
1121 desc = desc;
1122 fd = open(node, O_RDONLY);
1123 if (fd == -1) {
1124 sys_errmsg("cannot open \"%s\"", node);
1125 return -1;
1126 }
1127 req.bytes = bytes;
1128 req.vol_id = vol_id;
1129
1130 ret = ioctl(fd, UBI_IOCRSVOL, &req);
1131 close(fd);
1132 return ret;
1133 }
1134
1135 int ubi_update_start(libubi_t desc, int fd, long long bytes)
1136 {
1137 desc = desc;
1138 if (ioctl(fd, UBI_IOCVOLUP, &bytes))
1139 return -1;
1140 return 0;
1141 }
1142
1143 int ubi_leb_change_start(libubi_t desc, int fd, int lnum, int bytes)
1144 {
1145 struct ubi_leb_change_req req;
1146
1147 desc = desc;
1148 memset(&req, 0, sizeof(struct ubi_leb_change_req));
1149 req.lnum = lnum;
1150 req.bytes = bytes;
1151 req.dtype = 3;
1152
1153 if (ioctl(fd, UBI_IOCEBCH, &req))
1154 return -1;
1155 return 0;
1156 }
1157
1158 int ubi_dev_present(libubi_t desc, int dev_num)
1159 {
1160 struct stat st;
1161 struct libubi *lib = (struct libubi *)desc;
1162 char file[strlen(lib->ubi_dev) + 50];
1163
1164 sprintf(file, lib->ubi_dev, dev_num);
1165 return !stat(file, &st);
1166 }
1167
1168 int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info)
1169 {
1170 DIR *sysfs_ubi;
1171 struct dirent *dirent;
1172 struct libubi *lib = (struct libubi *)desc;
1173
1174 memset(info, 0, sizeof(struct ubi_dev_info));
1175 info->dev_num = dev_num;
1176
1177 if (!ubi_dev_present(desc, dev_num))
1178 return -1;
1179
1180 sysfs_ubi = opendir(lib->sysfs_ubi);
1181 if (!sysfs_ubi)
1182 return -1;
1183
1184 info->lowest_vol_id = INT_MAX;
1185
1186 while (1) {
1187 int vol_id, ret, devno;
1188 char tmp_buf[256];
1189
1190 errno = 0;
1191 dirent = readdir(sysfs_ubi);
1192 if (!dirent)
1193 break;
1194
1195 if (strlen(dirent->d_name) >= 255) {
1196 errmsg("invalid entry in %s: \"%s\"",
1197 lib->sysfs_ubi, dirent->d_name);
1198 goto out_close;
1199 }
1200
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;
1208 }
1209 }
1210
1211 if (!dirent && errno) {
1212 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_ubi);
1213 goto out_close;
1214 }
1215
1216 if (closedir(sysfs_ubi)) {
1217 sys_errmsg("closedir failed on \"%s\"", lib->sysfs_ubi);
1218 return -1;
1219 }
1220 if (info->lowest_vol_id == INT_MAX)
1221 info->lowest_vol_id = 0;
1222
1223 if (dev_get_major(lib, dev_num, &info->major, &info->minor))
1224 return -1;
1225
1226 if (dev_read_int(lib->dev_mtd_num, dev_num, &info->mtd_num))
1227 return -1;
1228 if (dev_read_int(lib->dev_avail_ebs, dev_num, &info->avail_lebs))
1229 return -1;
1230 if (dev_read_int(lib->dev_total_ebs, dev_num, &info->total_lebs))
1231 return -1;
1232 if (dev_read_int(lib->dev_bad_count, dev_num, &info->bad_count))
1233 return -1;
1234 if (dev_read_int(lib->dev_eb_size, dev_num, &info->leb_size))
1235 return -1;
1236 if (dev_read_int(lib->dev_bad_rsvd, dev_num, &info->bad_rsvd))
1237 return -1;
1238 if (dev_read_ll(lib->dev_max_ec, dev_num, &info->max_ec))
1239 return -1;
1240 if (dev_read_int(lib->dev_max_vols, dev_num, &info->max_vol_count))
1241 return -1;
1242 if (dev_read_int(lib->dev_min_io_size, dev_num, &info->min_io_size))
1243 return -1;
1244
1245 info->avail_bytes = (long long)info->avail_lebs * info->leb_size;
1246 info->total_bytes = (long long)info->total_lebs * info->leb_size;
1247
1248 return 0;
1249
1250 out_close:
1251 closedir(sysfs_ubi);
1252 return -1;
1253 }
1254
1255 int ubi_get_dev_info(libubi_t desc, const char *node, struct ubi_dev_info *info)
1256 {
1257 int err, dev_num = 0;
1258 struct libubi *lib = (struct libubi *)desc;
1259
1260 err = ubi_probe_node(desc, node);
1261 if (err != 1) {
1262 if (err == 2)
1263 errno = ENODEV;
1264 return -1;
1265 }
1266
1267 if (dev_node2num(lib, node, &dev_num))
1268 return -1;
1269
1270 return ubi_get_dev_info1(desc, dev_num, info);
1271 }
1272
1273 int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id,
1274 struct ubi_vol_info *info)
1275 {
1276 int ret;
1277 struct libubi *lib = (struct libubi *)desc;
1278 char buf[50];
1279
1280 memset(info, 0, sizeof(struct ubi_vol_info));
1281 info->dev_num = dev_num;
1282 info->vol_id = vol_id;
1283
1284 if (vol_get_major(lib, dev_num, vol_id, &info->major, &info->minor))
1285 return -1;
1286
1287 ret = vol_read_data(lib->vol_type, dev_num, vol_id, buf, 50);
1288 if (ret < 0)
1289 return -1;
1290
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;
1295 else {
1296 errmsg("bad value at \"%s\"", buf);
1297 errno = EINVAL;
1298 return -1;
1299 }
1300
1301 ret = vol_read_int(lib->vol_alignment, dev_num, vol_id,
1302 &info->alignment);
1303 if (ret)
1304 return -1;
1305 ret = vol_read_ll(lib->vol_data_bytes, dev_num, vol_id,
1306 &info->data_bytes);
1307 if (ret)
1308 return -1;
1309 ret = vol_read_int(lib->vol_rsvd_ebs, dev_num, vol_id, &info->rsvd_lebs);
1310 if (ret)
1311 return -1;
1312 ret = vol_read_int(lib->vol_eb_size, dev_num, vol_id, &info->leb_size);
1313 if (ret)
1314 return -1;
1315 ret = vol_read_int(lib->vol_corrupted, dev_num, vol_id,
1316 &info->corrupted);
1317 if (ret)
1318 return -1;
1319 info->rsvd_bytes = (long long)info->leb_size * info->rsvd_lebs;
1320
1321 ret = vol_read_data(lib->vol_name, dev_num, vol_id, &info->name,
1322 UBI_VOL_NAME_MAX + 2);
1323 if (ret < 0)
1324 return -1;
1325
1326 info->name[ret - 1] = '\0';
1327 return 0;
1328 }
1329
1330 int ubi_get_vol_info(libubi_t desc, const char *node, struct ubi_vol_info *info)
1331 {
1332 int err, vol_id = 0, dev_num = 0;
1333 struct libubi *lib = (struct libubi *)desc;
1334
1335 err = ubi_probe_node(desc, node);
1336 if (err != 2) {
1337 if (err == 1)
1338 errno = ENODEV;
1339 return -1;
1340 }
1341
1342 if (vol_node2nums(lib, node, &dev_num, &vol_id))
1343 return -1;
1344
1345 return ubi_get_vol_info1(desc, dev_num, vol_id, info);
1346 }
1347
1348 int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
1349 struct ubi_vol_info *info)
1350 {
1351 int i, err;
1352 unsigned int nlen = strlen(name);
1353 struct ubi_dev_info dev_info;
1354
1355 if (nlen == 0) {
1356 errmsg("bad \"name\" input parameter");
1357 errno = EINVAL;
1358 return -1;
1359 }
1360
1361 err = ubi_get_dev_info1(desc, dev_num, &dev_info);
1362 if (err)
1363 return err;
1364
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);
1368 if (err == -1) {
1369 if (errno == ENOENT)
1370 continue;
1371 return -1;
1372 }
1373
1374 if (nlen == strlen(info->name) && !strcmp(name, info->name))
1375 return 0;
1376 }
1377
1378 errno = ENOENT;
1379 return -1;
1380 }
1381
1382 int ubi_set_property(int fd, uint8_t property, uint64_t value)
1383 {
1384 struct ubi_set_vol_prop_req r;
1385
1386 memset(&r, 0, sizeof(struct ubi_set_vol_prop_req));
1387 r.property = property;
1388 r.value = value;
1389
1390 return ioctl(fd, UBI_IOCSETVOLPROP, &r);
1391 }
1392
1393 int ubi_leb_unmap(int fd, int lnum)
1394 {
1395 return ioctl(fd, UBI_IOCEBUNMAP, &lnum);
1396 }
1397
1398 int ubi_is_mapped(int fd, int lnum)
1399 {
1400 return ioctl(fd, UBI_IOCEBISMAP, &lnum);
1401 }