2 * Copyright (c) International Business Machines Corp., 2006
3 * Copyright (C) 2009 Nokia Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * Author: Artem Bityutskiy
24 /* Imported from mtd-utils by dehrenberg */
33 #include <sys/types.h>
35 #include <sys/ioctl.h>
38 #include <mtd/mtd-user.h>
41 #include "libmtd_int.h"
42 #include "libmtd_common.h"
45 * mkpath - compose full path from 2 given components.
46 * @path: the first component
47 * @name: the second component
49 * This function returns the resulting path in case of success and %NULL in
52 static char *mkpath(const char *path, const char *name)
55 size_t len1 = strlen(path);
56 size_t len2 = strlen(name);
58 n = xmalloc(len1 + len2 + 6);
60 memcpy(n, path, len1);
61 if (n[len1 - 1] != '/')
64 memcpy(n + len1, name, len2 + 1);
69 * read_data - read data from a file.
70 * @file: the file to read from
71 * @buf: the buffer to read to
72 * @buf_len: buffer length
74 * This function returns number of read bytes in case of success and %-1 in
75 * case of failure. Note, if the file contains more then @buf_len bytes of
76 * date, this function fails with %EINVAL error code.
78 static int read_data(const char *file, void *buf, int buf_len)
80 int fd, rd, tmp, tmp1;
82 fd = open(file, O_RDONLY | O_CLOEXEC);
86 rd = read(fd, buf, buf_len);
88 sys_errmsg("cannot read \"%s\"", file);
93 errmsg("contents of \"%s\" is too long", file);
98 ((char *)buf)[rd] = '\0';
100 /* Make sure all data is read */
101 tmp1 = read(fd, &tmp, 1);
103 sys_errmsg("cannot read \"%s\"", file);
107 errmsg("file \"%s\" contains too much data (> %d bytes)",
114 sys_errmsg("close failed on \"%s\"", file);
126 * read_major - read major and minor numbers from a file.
127 * @file: name of the file to read from
128 * @major: major number is returned here
129 * @minor: minor number is returned here
131 * This function returns % in case of success, and %-1 in case of failure.
133 static int read_major(const char *file, int *major, int *minor)
138 ret = read_data(file, buf, 50);
142 ret = sscanf(buf, "%d:%d\n", major, minor);
145 return errmsg("\"%s\" does not have major:minor format", file);
148 if (*major < 0 || *minor < 0) {
150 return errmsg("bad major:minor %d:%d in \"%s\"",
151 *major, *minor, file);
158 * dev_get_major - get major and minor numbers of an MTD device.
159 * @lib: libmtd descriptor
160 * @mtd_num: MTD device number
161 * @major: major number is returned here
162 * @minor: minor number is returned here
164 * This function returns zero in case of success and %-1 in case of failure.
166 static int dev_get_major(struct libmtd *lib, int mtd_num, int *major, int *minor)
168 char file[strlen(lib->mtd_dev) + 50];
170 sprintf(file, lib->mtd_dev, mtd_num);
171 return read_major(file, major, minor);
175 * dev_read_data - read data from an MTD device's sysfs file.
176 * @patt: file pattern to read from
177 * @mtd_num: MTD device number
178 * @buf: buffer to read to
179 * @buf_len: buffer length
181 * This function returns number of read bytes in case of success and %-1 in
184 static int dev_read_data(const char *patt, int mtd_num, void *buf, int buf_len)
186 char file[strlen(patt) + 100];
188 sprintf(file, patt, mtd_num);
189 return read_data(file, buf, buf_len);
193 * read_hex_ll - read a hex 'long long' value from a file.
194 * @file: the file to read from
195 * @value: the result is stored here
197 * This function reads file @file and interprets its contents as hexadecimal
198 * 'long long' integer. If this is not true, it fails with %EINVAL error code.
199 * Returns %0 in case of success and %-1 in case of failure.
201 static int read_hex_ll(const char *file, long long *value)
206 fd = open(file, O_RDONLY | O_CLOEXEC);
210 rd = read(fd, buf, sizeof(buf));
212 sys_errmsg("cannot read \"%s\"", file);
215 if (rd == sizeof(buf)) {
216 errmsg("contents of \"%s\" is too long", file);
222 if (sscanf(buf, "%llx\n", value) != 1) {
223 errmsg("cannot read integer from \"%s\"\n", file);
229 errmsg("negative value %lld in \"%s\"", *value, file);
235 return sys_errmsg("close failed on \"%s\"", file);
245 * read_pos_ll - read a positive 'long long' value from a file.
246 * @file: the file to read from
247 * @value: the result is stored here
249 * This function reads file @file and interprets its contents as a positive
250 * 'long long' integer. If this is not true, it fails with %EINVAL error code.
251 * Returns %0 in case of success and %-1 in case of failure.
253 static int read_pos_ll(const char *file, long long *value)
258 fd = open(file, O_RDONLY | O_CLOEXEC);
262 rd = read(fd, buf, 50);
264 sys_errmsg("cannot read \"%s\"", file);
268 errmsg("contents of \"%s\" is too long", file);
273 if (sscanf(buf, "%lld\n", value) != 1) {
274 errmsg("cannot read integer from \"%s\"\n", file);
280 errmsg("negative value %lld in \"%s\"", *value, file);
286 return sys_errmsg("close failed on \"%s\"", file);
296 * read_hex_int - read an 'int' value from a file.
297 * @file: the file to read from
298 * @value: the result is stored here
300 * This function is the same as 'read_pos_ll()', but it reads an 'int'
301 * value, not 'long long'.
303 static int read_hex_int(const char *file, int *value)
307 if (read_hex_ll(file, &res))
310 /* Make sure the value has correct range */
311 if (res > INT_MAX || res < INT_MIN) {
312 errmsg("value %lld read from file \"%s\" is out of range",
323 * read_pos_int - read a positive 'int' value from a file.
324 * @file: the file to read from
325 * @value: the result is stored here
327 * This function is the same as 'read_pos_ll()', but it reads an 'int'
328 * value, not 'long long'.
330 static int read_pos_int(const char *file, int *value)
334 if (read_pos_ll(file, &res))
337 /* Make sure the value is not too big */
339 errmsg("value %lld read from file \"%s\" is out of range",
350 * dev_read_hex_int - read an hex 'int' value from an MTD device sysfs file.
351 * @patt: file pattern to read from
352 * @mtd_num: MTD device number
353 * @value: the result is stored here
355 * This function returns %0 in case of success and %-1 in case of failure.
357 static int dev_read_hex_int(const char *patt, int mtd_num, int *value)
359 char file[strlen(patt) + 50];
361 sprintf(file, patt, mtd_num);
362 return read_hex_int(file, value);
366 * dev_read_pos_int - read a positive 'int' value from an MTD device sysfs file.
367 * @patt: file pattern to read from
368 * @mtd_num: MTD device number
369 * @value: the result is stored here
371 * This function returns %0 in case of success and %-1 in case of failure.
373 static int dev_read_pos_int(const char *patt, int mtd_num, int *value)
375 char file[strlen(patt) + 50];
377 sprintf(file, patt, mtd_num);
378 return read_pos_int(file, value);
382 * dev_read_pos_ll - read a positive 'long long' value from an MTD device sysfs file.
383 * @patt: file pattern to read from
384 * @mtd_num: MTD device number
385 * @value: the result is stored here
387 * This function returns %0 in case of success and %-1 in case of failure.
389 static int dev_read_pos_ll(const char *patt, int mtd_num, long long *value)
391 char file[strlen(patt) + 50];
393 sprintf(file, patt, mtd_num);
394 return read_pos_ll(file, value);
398 * type_str2int - convert MTD device type to integer.
399 * @str: MTD device type string to convert
401 * This function converts MTD device type string @str, read from sysfs, into an
404 static int type_str2int(const char *str)
406 if (!strcmp(str, "nand"))
407 return MTD_NANDFLASH;
408 if (!strcmp(str, "mlc-nand"))
409 return MTD_MLCNANDFLASH;
410 if (!strcmp(str, "nor"))
412 if (!strcmp(str, "rom"))
414 if (!strcmp(str, "absent"))
416 if (!strcmp(str, "dataflash"))
417 return MTD_DATAFLASH;
418 if (!strcmp(str, "ram"))
420 if (!strcmp(str, "ubi"))
421 return MTD_UBIVOLUME;
426 * dev_node2num - find UBI device number by its character device node.
427 * @lib: MTD library descriptor
428 * @node: name of the MTD device node
429 * @mtd_num: MTD device number is returned here
431 * This function returns %0 in case of success and %-1 in case of failure.
433 static int dev_node2num(struct libmtd *lib, const char *node, int *mtd_num)
437 struct mtd_info info;
440 return sys_errmsg("cannot get information about \"%s\"", node);
442 if (!S_ISCHR(st.st_mode)) {
443 errmsg("\"%s\" is not a character device", node);
448 mjr = major(st.st_rdev);
449 mnr = minor(st.st_rdev);
451 if (mtd_get_info((libmtd_t *)lib, &info))
454 for (i = info.lowest_mtd_num; i <= info.highest_mtd_num; i++) {
457 ret = dev_get_major(lib, i, &mjr1, &mnr1);
466 if (mjr1 == mjr && mnr1 == mnr) {
478 * sysfs_is_supported - check whether the MTD sub-system supports MTD.
479 * @lib: MTD library descriptor
481 * The Linux kernel MTD subsystem gained MTD support starting from kernel
482 * 2.6.30 and libmtd tries to use sysfs interface if possible, because the NAND
483 * sub-page size is available there (and not available at all in pre-sysfs
486 * Very old kernels did not have "/sys/class/mtd" directory. Not very old
487 * kernels (e.g., 2.6.29) did have "/sys/class/mtd/mtdX" directories, by there
488 * were no files there, e.g., the "name" file was not present. So all we can do
489 * is to check for a "/sys/class/mtd/mtdX/name" file. But this is not a
490 * reliable check, because if this is a new system with no MTD devices - we'll
491 * treat it as a pre-sysfs system.
493 static int sysfs_is_supported(struct libmtd *lib)
497 char file[strlen(lib->mtd_name) + 10];
499 sysfs_mtd = opendir(lib->sysfs_mtd);
501 if (errno == ENOENT) {
505 return sys_errmsg("cannot open \"%s\"", lib->sysfs_mtd);
509 * First of all find an "mtdX" directory. This is needed because there
510 * may be, for example, mtd1 but no mtd0.
515 struct dirent *dirent;
517 dirent = readdir(sysfs_mtd);
521 if (strlen(dirent->d_name) >= 255) {
522 errmsg("invalid entry in %s: \"%s\"",
523 lib->sysfs_mtd, dirent->d_name);
529 ret = sscanf(dirent->d_name, MTD_NAME_PATT"%s",
537 if (closedir(sysfs_mtd))
538 return sys_errmsg("closedir failed on \"%s\"", lib->sysfs_mtd);
541 /* No mtd device, treat this as pre-sysfs system */
544 sprintf(file, lib->mtd_name, num);
545 fd = open(file, O_RDONLY | O_CLOEXEC);
550 sys_errmsg("close failed on \"%s\"", file);
557 libmtd_t libmtd_open(void)
561 lib = xzalloc(sizeof(*lib));
563 lib->offs64_ioctls = OFFS64_IOCTLS_UNKNOWN;
565 lib->sysfs_mtd = mkpath("/sys", SYSFS_MTD);
569 lib->mtd = mkpath(lib->sysfs_mtd, MTD_NAME_PATT);
573 lib->mtd_name = mkpath(lib->mtd, MTD_NAME);
577 if (!sysfs_is_supported(lib)) {
579 free(lib->sysfs_mtd);
581 lib->mtd_name = lib->mtd = lib->sysfs_mtd = NULL;
585 lib->mtd_dev = mkpath(lib->mtd, MTD_DEV);
589 lib->mtd_type = mkpath(lib->mtd, MTD_TYPE);
593 lib->mtd_eb_size = mkpath(lib->mtd, MTD_EB_SIZE);
594 if (!lib->mtd_eb_size)
597 lib->mtd_size = mkpath(lib->mtd, MTD_SIZE);
601 lib->mtd_min_io_size = mkpath(lib->mtd, MTD_MIN_IO_SIZE);
602 if (!lib->mtd_min_io_size)
605 lib->mtd_subpage_size = mkpath(lib->mtd, MTD_SUBPAGE_SIZE);
606 if (!lib->mtd_subpage_size)
609 lib->mtd_oob_size = mkpath(lib->mtd, MTD_OOB_SIZE);
610 if (!lib->mtd_oob_size)
613 lib->mtd_region_cnt = mkpath(lib->mtd, MTD_REGION_CNT);
614 if (!lib->mtd_region_cnt)
617 lib->mtd_flags = mkpath(lib->mtd, MTD_FLAGS);
621 lib->sysfs_supported = 1;
625 libmtd_close((libmtd_t)lib);
629 void libmtd_close(libmtd_t desc)
631 struct libmtd *lib = (struct libmtd *)desc;
633 free(lib->mtd_flags);
634 free(lib->mtd_region_cnt);
635 free(lib->mtd_oob_size);
636 free(lib->mtd_subpage_size);
637 free(lib->mtd_min_io_size);
639 free(lib->mtd_eb_size);
644 free(lib->sysfs_mtd);
648 int mtd_dev_present(libmtd_t desc, int mtd_num) {
650 struct libmtd *lib = (struct libmtd *)desc;
652 if (!lib->sysfs_supported) {
653 return legacy_dev_present(mtd_num) == 1;
655 char file[strlen(lib->mtd) + 10];
657 sprintf(file, lib->mtd, mtd_num);
658 return !stat(file, &st);
662 int mtd_get_info(libmtd_t desc, struct mtd_info *info)
665 struct dirent *dirent;
666 struct libmtd *lib = (struct libmtd *)desc;
668 memset(info, 0, sizeof(struct mtd_info));
670 if (!lib->sysfs_supported)
671 return legacy_mtd_get_info(info);
673 info->sysfs_supported = 1;
676 * We have to scan the MTD sysfs directory to identify how many MTD
677 * devices are present.
679 sysfs_mtd = opendir(lib->sysfs_mtd);
681 if (errno == ENOENT) {
685 return sys_errmsg("cannot open \"%s\"", lib->sysfs_mtd);
688 info->lowest_mtd_num = INT_MAX;
694 dirent = readdir(sysfs_mtd);
698 if (strlen(dirent->d_name) >= 255) {
699 errmsg("invalid entry in %s: \"%s\"",
700 lib->sysfs_mtd, dirent->d_name);
705 ret = sscanf(dirent->d_name, MTD_NAME_PATT"%s",
708 info->mtd_dev_cnt += 1;
709 if (mtd_num > info->highest_mtd_num)
710 info->highest_mtd_num = mtd_num;
711 if (mtd_num < info->lowest_mtd_num)
712 info->lowest_mtd_num = mtd_num;
716 if (!dirent && errno) {
717 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_mtd);
721 if (closedir(sysfs_mtd))
722 return sys_errmsg("closedir failed on \"%s\"", lib->sysfs_mtd);
724 if (info->lowest_mtd_num == INT_MAX)
725 info->lowest_mtd_num = 0;
734 int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd)
737 struct libmtd *lib = (struct libmtd *)desc;
739 memset(mtd, 0, sizeof(struct mtd_dev_info));
740 mtd->mtd_num = mtd_num;
742 if (!mtd_dev_present(desc, mtd_num)) {
745 } else if (!lib->sysfs_supported)
746 return legacy_get_dev_info1(mtd_num, mtd);
748 if (dev_get_major(lib, mtd_num, &mtd->major, &mtd->minor))
751 ret = dev_read_data(lib->mtd_name, mtd_num, &mtd->name,
755 ((char *)mtd->name)[ret - 1] = '\0';
757 ret = dev_read_data(lib->mtd_type, mtd_num, &mtd->type_str,
761 ((char *)mtd->type_str)[ret - 1] = '\0';
763 if (dev_read_pos_int(lib->mtd_eb_size, mtd_num, &mtd->eb_size))
765 if (dev_read_pos_ll(lib->mtd_size, mtd_num, &mtd->size))
767 if (dev_read_pos_int(lib->mtd_min_io_size, mtd_num, &mtd->min_io_size))
769 if (dev_read_pos_int(lib->mtd_subpage_size, mtd_num, &mtd->subpage_size))
771 if (dev_read_pos_int(lib->mtd_oob_size, mtd_num, &mtd->oob_size))
773 if (dev_read_pos_int(lib->mtd_region_cnt, mtd_num, &mtd->region_cnt))
775 if (dev_read_hex_int(lib->mtd_flags, mtd_num, &ret))
777 mtd->writable = !!(ret & MTD_WRITEABLE);
779 mtd->eb_cnt = mtd->size / mtd->eb_size;
780 mtd->type = type_str2int(mtd->type_str);
781 mtd->bb_allowed = !!(mtd->type == MTD_NANDFLASH ||
782 mtd->type == MTD_MLCNANDFLASH);
787 int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd)
790 struct libmtd *lib = (struct libmtd *)desc;
792 if (!lib->sysfs_supported)
793 return legacy_get_dev_info(node, mtd);
795 if (dev_node2num(lib, node, &mtd_num))
798 return mtd_get_dev_info1(desc, mtd_num, mtd);
801 static inline int mtd_ioctl_error(const struct mtd_dev_info *mtd, int eb,
804 return sys_errmsg("%s ioctl failed for eraseblock %d (mtd%d)",
805 sreq, eb, mtd->mtd_num);
808 static int mtd_valid_erase_block(const struct mtd_dev_info *mtd, int eb)
810 if (eb < 0 || eb >= mtd->eb_cnt) {
811 errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
812 eb, mtd->mtd_num, mtd->eb_cnt);
819 static int mtd_xlock(const struct mtd_dev_info *mtd, int fd, int eb, int req,
823 struct erase_info_user ei;
825 ret = mtd_valid_erase_block(mtd, eb);
829 ei.start = eb * mtd->eb_size;
830 ei.length = mtd->eb_size;
832 ret = ioctl(fd, req, &ei);
834 return mtd_ioctl_error(mtd, eb, sreq);
838 #define mtd_xlock(mtd, fd, eb, req) mtd_xlock(mtd, fd, eb, req, #req)
840 int mtd_lock(const struct mtd_dev_info *mtd, int fd, int eb)
842 return mtd_xlock(mtd, fd, eb, MEMLOCK);
845 int mtd_unlock(const struct mtd_dev_info *mtd, int fd, int eb)
847 return mtd_xlock(mtd, fd, eb, MEMUNLOCK);
850 int mtd_erase(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb)
853 struct libmtd *lib = (struct libmtd *)desc;
854 struct erase_info_user64 ei64;
855 struct erase_info_user ei;
857 ret = mtd_valid_erase_block(mtd, eb);
861 ei64.start = (__u64)eb * mtd->eb_size;
862 ei64.length = mtd->eb_size;
864 if (lib->offs64_ioctls == OFFS64_IOCTLS_SUPPORTED ||
865 lib->offs64_ioctls == OFFS64_IOCTLS_UNKNOWN) {
866 ret = ioctl(fd, MEMERASE64, &ei64);
870 if (errno != ENOTTY ||
871 lib->offs64_ioctls != OFFS64_IOCTLS_UNKNOWN)
872 return mtd_ioctl_error(mtd, eb, "MEMERASE64");
875 * MEMERASE64 support was added in kernel version 2.6.31, so
876 * probably we are working with older kernel and this ioctl is
879 lib->offs64_ioctls = OFFS64_IOCTLS_NOT_SUPPORTED;
882 if (ei64.start + ei64.length > 0xFFFFFFFF) {
883 errmsg("this system can address only %u eraseblocks",
884 0xFFFFFFFFU / mtd->eb_size);
889 ei.start = ei64.start;
890 ei.length = ei64.length;
891 ret = ioctl(fd, MEMERASE, &ei);
893 return mtd_ioctl_error(mtd, eb, "MEMERASE");
897 int mtd_regioninfo(int fd, int regidx, struct region_info_user *reginfo)
906 reginfo->regionindex = regidx;
908 ret = ioctl(fd, MEMGETREGIONINFO, reginfo);
910 return sys_errmsg("%s ioctl failed for erase region %d",
911 "MEMGETREGIONINFO", regidx);
916 int mtd_is_locked(const struct mtd_dev_info *mtd, int fd, int eb)
921 ei.start = eb * mtd->eb_size;
922 ei.length = mtd->eb_size;
924 ret = ioctl(fd, MEMISLOCKED, &ei);
926 if (errno != ENOTTY && errno != EOPNOTSUPP)
927 return mtd_ioctl_error(mtd, eb, "MEMISLOCKED");
935 /* Patterns to write to a physical eraseblock when torturing it */
936 static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
939 * check_pattern - check if buffer contains only a certain byte pattern.
940 * @buf: buffer to check
941 * @patt: the pattern to check
942 * @size: buffer size in bytes
944 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
945 * something else was also found.
947 static int check_pattern(const void *buf, uint8_t patt, int size)
951 for (i = 0; i < size; i++)
952 if (((const uint8_t *)buf)[i] != patt)
957 int mtd_torture(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb)
959 int err, i, patt_count;
962 normsg("run torture test for PEB %d", eb);
963 patt_count = ARRAY_SIZE(patterns);
965 buf = xmalloc(mtd->eb_size);
967 for (i = 0; i < patt_count; i++) {
968 err = mtd_erase(desc, mtd, fd, eb);
972 /* Make sure the PEB contains only 0xFF bytes */
973 err = mtd_read(mtd, fd, eb, 0, buf, mtd->eb_size);
977 err = check_pattern(buf, 0xFF, mtd->eb_size);
979 errmsg("erased PEB %d, but a non-0xFF byte found", eb);
984 /* Write a pattern and check it */
985 memset(buf, patterns[i], mtd->eb_size);
986 err = mtd_write(desc, mtd, fd, eb, 0, buf, mtd->eb_size, NULL,
991 memset(buf, ~patterns[i], mtd->eb_size);
992 err = mtd_read(mtd, fd, eb, 0, buf, mtd->eb_size);
996 err = check_pattern(buf, patterns[i], mtd->eb_size);
998 errmsg("pattern %x checking failed for PEB %d",
1006 normsg("PEB %d passed torture test, do not mark it a bad", eb);
1013 int mtd_is_bad(const struct mtd_dev_info *mtd, int fd, int eb)
1018 ret = mtd_valid_erase_block(mtd, eb);
1022 if (!mtd->bb_allowed)
1025 seek = (loff_t)eb * mtd->eb_size;
1026 ret = ioctl(fd, MEMGETBADBLOCK, &seek);
1028 return mtd_ioctl_error(mtd, eb, "MEMGETBADBLOCK");
1032 int mtd_mark_bad(const struct mtd_dev_info *mtd, int fd, int eb)
1037 if (!mtd->bb_allowed) {
1042 ret = mtd_valid_erase_block(mtd, eb);
1046 seek = (loff_t)eb * mtd->eb_size;
1047 ret = ioctl(fd, MEMSETBADBLOCK, &seek);
1049 return mtd_ioctl_error(mtd, eb, "MEMSETBADBLOCK");
1053 int mtd_read(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
1059 ret = mtd_valid_erase_block(mtd, eb);
1063 if (offs < 0 || offs + len > mtd->eb_size) {
1064 errmsg("bad offset %d or length %d, mtd%d eraseblock size is %d",
1065 offs, len, mtd->mtd_num, mtd->eb_size);
1070 /* Seek to the beginning of the eraseblock */
1071 seek = (off_t)eb * mtd->eb_size + offs;
1072 if (lseek(fd, seek, SEEK_SET) != seek)
1073 return sys_errmsg("cannot seek mtd%d to offset %"PRIdoff_t,
1074 mtd->mtd_num, seek);
1077 ret = read(fd, buf, len);
1079 return sys_errmsg("cannot read %d bytes from mtd%d (eraseblock %d, offset %d)",
1080 len, mtd->mtd_num, eb, offs);
1087 static int legacy_auto_oob_layout(const struct mtd_dev_info *mtd, int fd,
1088 int ooblen, void *oob) {
1089 struct nand_oobinfo old_oobinfo;
1093 /* Read the current oob info */
1094 if (ioctl(fd, MEMGETOOBSEL, &old_oobinfo))
1095 return sys_errmsg("MEMGETOOBSEL failed");
1097 tmp_buf = malloc(ooblen);
1098 memcpy(tmp_buf, oob, ooblen);
1101 * We use autoplacement and have the oobinfo with the autoplacement
1102 * information from the kernel available
1104 if (old_oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
1105 int i, tags_pos = 0;
1106 for (i = 0; old_oobinfo.oobfree[i][1]; i++) {
1107 /* Set the reserved bytes to 0xff */
1108 start = old_oobinfo.oobfree[i][0];
1109 len = old_oobinfo.oobfree[i][1];
1110 memcpy(oob + start, tmp_buf + tags_pos, len);
1114 /* Set at least the ecc byte positions to 0xff */
1115 start = old_oobinfo.eccbytes;
1116 len = mtd->oob_size - start;
1117 memcpy(oob + start, tmp_buf + start, len);
1124 int mtd_write(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb,
1125 int offs, void *data, int len, void *oob, int ooblen,
1130 struct mtd_write_req ops;
1132 ret = mtd_valid_erase_block(mtd, eb);
1136 if (offs < 0 || offs + len > mtd->eb_size) {
1137 errmsg("bad offset %d or length %d, mtd%d eraseblock size is %d",
1138 offs, len, mtd->mtd_num, mtd->eb_size);
1142 if (offs % mtd->subpage_size) {
1143 errmsg("write offset %d is not aligned to mtd%d min. I/O size %d",
1144 offs, mtd->mtd_num, mtd->subpage_size);
1148 if (len % mtd->subpage_size) {
1149 errmsg("write length %d is not aligned to mtd%d min. I/O size %d",
1150 len, mtd->mtd_num, mtd->subpage_size);
1155 /* Calculate seek address */
1156 seek = (off_t)eb * mtd->eb_size + offs;
1161 ops.ooblen = ooblen;
1162 ops.usr_data = (uint64_t)(unsigned long)data;
1163 ops.usr_oob = (uint64_t)(unsigned long)oob;
1166 ret = ioctl(fd, MEMWRITE, &ops);
1169 else if (errno != ENOTTY && errno != EOPNOTSUPP)
1170 return mtd_ioctl_error(mtd, eb, "MEMWRITE");
1172 /* Fall back to old OOB ioctl() if necessary */
1173 if (mode == MTD_OPS_AUTO_OOB)
1174 if (legacy_auto_oob_layout(mtd, fd, ooblen, oob))
1176 if (mtd_write_oob(desc, mtd, fd, seek, ooblen, oob) < 0)
1177 return sys_errmsg("cannot write to OOB");
1180 /* Seek to the beginning of the eraseblock */
1181 if (lseek(fd, seek, SEEK_SET) != seek)
1182 return sys_errmsg("cannot seek mtd%d to offset %"PRIdoff_t,
1183 mtd->mtd_num, seek);
1184 ret = write(fd, data, len);
1186 return sys_errmsg("cannot write %d bytes to mtd%d "
1187 "(eraseblock %d, offset %d)",
1188 len, mtd->mtd_num, eb, offs);
1194 static int do_oob_op(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
1195 uint64_t start, uint64_t length, void *data, unsigned int cmd64,
1199 struct mtd_oob_buf64 oob64;
1200 struct mtd_oob_buf oob;
1201 unsigned long long max_offs;
1202 const char *cmd64_str, *cmd_str;
1203 struct libmtd *lib = (struct libmtd *)desc;
1205 if (cmd64 == MEMREADOOB64) {
1206 cmd64_str = "MEMREADOOB64";
1207 cmd_str = "MEMREADOOB";
1209 cmd64_str = "MEMWRITEOOB64";
1210 cmd_str = "MEMWRITEOOB";
1213 max_offs = (unsigned long long)mtd->eb_cnt * mtd->eb_size;
1214 if (start >= max_offs) {
1215 errmsg("bad page address %" PRIu64 ", mtd%d has %d eraseblocks (%llu bytes)",
1216 start, mtd->mtd_num, mtd->eb_cnt, max_offs);
1221 oob_offs = start & (mtd->min_io_size - 1);
1222 if (oob_offs + length > mtd->oob_size || length == 0) {
1223 errmsg("Cannot write %" PRIu64 " OOB bytes to address %" PRIu64 " (OOB offset %u) - mtd%d OOB size is only %d bytes",
1224 length, start, oob_offs, mtd->mtd_num, mtd->oob_size);
1229 oob64.start = start;
1230 oob64.length = length;
1231 oob64.usr_ptr = (uint64_t)(unsigned long)data;
1233 if (lib->offs64_ioctls == OFFS64_IOCTLS_SUPPORTED ||
1234 lib->offs64_ioctls == OFFS64_IOCTLS_UNKNOWN) {
1235 ret = ioctl(fd, cmd64, &oob64);
1239 if (errno != ENOTTY ||
1240 lib->offs64_ioctls != OFFS64_IOCTLS_UNKNOWN) {
1241 sys_errmsg("%s ioctl failed for mtd%d, offset %" PRIu64 " (eraseblock %" PRIu64 ")",
1242 cmd64_str, mtd->mtd_num, start, start / mtd->eb_size);
1246 * MEMREADOOB64/MEMWRITEOOB64 support was added in kernel
1247 * version 2.6.31, so probably we are working with older kernel
1248 * and these ioctls are not supported.
1250 lib->offs64_ioctls = OFFS64_IOCTLS_NOT_SUPPORTED;
1253 if (oob64.start > 0xFFFFFFFFULL) {
1254 errmsg("this system can address only up to address %lu",
1260 oob.start = oob64.start;
1261 oob.length = oob64.length;
1264 ret = ioctl(fd, cmd, &oob);
1266 sys_errmsg("%s ioctl failed for mtd%d, offset %" PRIu64 " (eraseblock %" PRIu64 ")",
1267 cmd_str, mtd->mtd_num, start, start / mtd->eb_size);
1271 int mtd_read_oob(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
1272 uint64_t start, uint64_t length, void *data)
1274 return do_oob_op(desc, mtd, fd, start, length, data,
1275 MEMREADOOB64, MEMREADOOB);
1278 int mtd_write_oob(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
1279 uint64_t start, uint64_t length, void *data)
1281 return do_oob_op(desc, mtd, fd, start, length, data,
1282 MEMWRITEOOB64, MEMWRITEOOB);
1285 int mtd_write_img(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
1286 const char *img_name)
1288 int tmp, ret, in_fd, len, written = 0;
1293 ret = mtd_valid_erase_block(mtd, eb);
1297 if (offs < 0 || offs >= mtd->eb_size) {
1298 errmsg("bad offset %d, mtd%d eraseblock size is %d",
1299 offs, mtd->mtd_num, mtd->eb_size);
1303 if (offs % mtd->subpage_size) {
1304 errmsg("write offset %d is not aligned to mtd%d min. I/O size %d",
1305 offs, mtd->mtd_num, mtd->subpage_size);
1310 in_fd = open(img_name, O_RDONLY | O_CLOEXEC);
1312 return sys_errmsg("cannot open \"%s\"", img_name);
1314 if (fstat(in_fd, &st)) {
1315 sys_errmsg("cannot stat %s", img_name);
1320 if (len % mtd->subpage_size) {
1321 errmsg("size of \"%s\" is %d byte, which is not aligned to "
1322 "mtd%d min. I/O size %d", img_name, len, mtd->mtd_num,
1327 tmp = (offs + len + mtd->eb_size - 1) / mtd->eb_size;
1328 if (eb + tmp > mtd->eb_cnt) {
1329 errmsg("\"%s\" image size is %d bytes, mtd%d size is %d "
1330 "eraseblocks, the image does not fit if we write it "
1331 "starting from eraseblock %d, offset %d",
1332 img_name, len, mtd->mtd_num, mtd->eb_cnt, eb, offs);
1337 /* Seek to the beginning of the eraseblock */
1338 seek = (off_t)eb * mtd->eb_size + offs;
1339 if (lseek(fd, seek, SEEK_SET) != seek) {
1340 sys_errmsg("cannot seek mtd%d to offset %"PRIdoff_t,
1341 mtd->mtd_num, seek);
1345 buf = xmalloc(mtd->eb_size);
1347 while (written < len) {
1351 ret = read(in_fd, buf, mtd->eb_size - offs - rd);
1353 sys_errmsg("cannot read \"%s\"", img_name);
1357 } while (ret && rd < mtd->eb_size - offs);
1359 ret = write(fd, buf, rd);
1361 sys_errmsg("cannot write %d bytes to mtd%d (eraseblock %d, offset %d)",
1362 len, mtd->mtd_num, eb, offs);
1382 int mtd_probe_node(libmtd_t desc, const char *node)
1385 struct mtd_info info;
1387 struct libmtd *lib = (struct libmtd *)desc;
1389 if (stat(node, &st))
1390 return sys_errmsg("cannot get information about \"%s\"", node);
1392 if (!S_ISCHR(st.st_mode)) {
1393 errmsg("\"%s\" is not a character device", node);
1398 mjr = major(st.st_rdev);
1399 mnr = minor(st.st_rdev);
1401 if (mtd_get_info((libmtd_t *)lib, &info))
1404 if (!lib->sysfs_supported)
1407 for (i = info.lowest_mtd_num; i <= info.highest_mtd_num; i++) {
1408 int mjr1, mnr1, ret;
1410 ret = dev_get_major(lib, i, &mjr1, &mnr1);
1412 if (errno == ENOENT)
1419 if (mjr1 == mjr && mnr1 == mnr)