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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * Author: Artem Bityutskiy
24 /* Imported from mtd-utils by dehrenberg */
33 #include <sys/types.h>
35 #include <sys/ioctl.h>
38 #include "../compiler/compiler.h"
40 #include <mtd/mtd-user.h>
43 #include "libmtd_int.h"
44 #include "libmtd_common.h"
47 * mkpath - compose full path from 2 given components.
48 * @path: the first component
49 * @name: the second component
51 * This function returns the resulting path in case of success and %NULL in
54 static char *mkpath(const char *path, const char *name)
57 size_t len1 = strlen(path);
58 size_t len2 = strlen(name);
60 n = xmalloc(len1 + len2 + 6);
62 memcpy(n, path, len1);
63 if (n[len1 - 1] != '/')
66 memcpy(n + len1, name, len2 + 1);
71 * read_data - read data from a file.
72 * @file: the file to read from
73 * @buf: the buffer to read to
74 * @buf_len: buffer length
76 * This function returns number of read bytes in case of success and %-1 in
77 * case of failure. Note, if the file contains more then @buf_len bytes of
78 * date, this function fails with %EINVAL error code.
80 static int read_data(const char *file, void *buf, int buf_len)
82 int fd, rd, tmp, tmp1;
84 fd = open(file, O_RDONLY | O_CLOEXEC);
88 rd = read(fd, buf, buf_len);
90 sys_errmsg("cannot read \"%s\"", file);
95 errmsg("contents of \"%s\" is too long", file);
100 ((char *)buf)[rd] = '\0';
102 /* Make sure all data is read */
103 tmp1 = read(fd, &tmp, 1);
105 sys_errmsg("cannot read \"%s\"", file);
109 errmsg("file \"%s\" contains too much data (> %d bytes)",
116 sys_errmsg("close failed on \"%s\"", file);
128 * read_major - read major and minor numbers from a file.
129 * @file: name of the file to read from
130 * @major: major number is returned here
131 * @minor: minor number is returned here
133 * This function returns % in case of success, and %-1 in case of failure.
135 static int read_major(const char *file, int *major, int *minor)
140 ret = read_data(file, buf, 50);
144 ret = sscanf(buf, "%d:%d\n", major, minor);
147 return errmsg("\"%s\" does not have major:minor format", file);
150 if (*major < 0 || *minor < 0) {
152 return errmsg("bad major:minor %d:%d in \"%s\"",
153 *major, *minor, file);
160 * dev_get_major - get major and minor numbers of an MTD device.
161 * @lib: libmtd descriptor
162 * @mtd_num: MTD device number
163 * @major: major number is returned here
164 * @minor: minor number is returned here
166 * This function returns zero in case of success and %-1 in case of failure.
168 static int dev_get_major(struct libmtd *lib, int mtd_num, int *major, int *minor)
170 char file[strlen(lib->mtd_dev) + 50];
172 sprintf(file, lib->mtd_dev, mtd_num);
173 return read_major(file, major, minor);
177 * dev_read_data - read data from an MTD device's sysfs file.
178 * @patt: file pattern to read from
179 * @mtd_num: MTD device number
180 * @buf: buffer to read to
181 * @buf_len: buffer length
183 * This function returns number of read bytes in case of success and %-1 in
186 static int dev_read_data(const char *patt, int mtd_num, void *buf, int buf_len)
188 char file[strlen(patt) + 100];
190 sprintf(file, patt, mtd_num);
191 return read_data(file, buf, buf_len);
195 * read_hex_ll - read a hex 'long long' value from a file.
196 * @file: the file to read from
197 * @value: the result is stored here
199 * This function reads file @file and interprets its contents as hexadecimal
200 * 'long long' integer. If this is not true, it fails with %EINVAL error code.
201 * Returns %0 in case of success and %-1 in case of failure.
203 static int read_hex_ll(const char *file, long long *value)
208 fd = open(file, O_RDONLY | O_CLOEXEC);
212 rd = read(fd, buf, sizeof(buf));
214 sys_errmsg("cannot read \"%s\"", file);
217 if (rd == sizeof(buf)) {
218 errmsg("contents of \"%s\" is too long", file);
224 if (sscanf(buf, "%llx\n", value) != 1) {
225 errmsg("cannot read integer from \"%s\"\n", file);
231 errmsg("negative value %lld in \"%s\"", *value, file);
237 return sys_errmsg("close failed on \"%s\"", file);
247 * read_pos_ll - read a positive 'long long' value from a file.
248 * @file: the file to read from
249 * @value: the result is stored here
251 * This function reads file @file and interprets its contents as a positive
252 * 'long long' integer. If this is not true, it fails with %EINVAL error code.
253 * Returns %0 in case of success and %-1 in case of failure.
255 static int read_pos_ll(const char *file, long long *value)
260 fd = open(file, O_RDONLY | O_CLOEXEC);
264 rd = read(fd, buf, 50);
266 sys_errmsg("cannot read \"%s\"", file);
270 errmsg("contents of \"%s\" is too long", file);
275 if (sscanf(buf, "%lld\n", value) != 1) {
276 errmsg("cannot read integer from \"%s\"\n", file);
282 errmsg("negative value %lld in \"%s\"", *value, file);
288 return sys_errmsg("close failed on \"%s\"", file);
298 * read_hex_int - read an 'int' value from a file.
299 * @file: the file to read from
300 * @value: the result is stored here
302 * This function is the same as 'read_pos_ll()', but it reads an 'int'
303 * value, not 'long long'.
305 static int read_hex_int(const char *file, int *value)
309 if (read_hex_ll(file, &res))
312 /* Make sure the value has correct range */
313 if (res > INT_MAX || res < INT_MIN) {
314 errmsg("value %lld read from file \"%s\" is out of range",
325 * read_pos_int - read a positive 'int' value from a file.
326 * @file: the file to read from
327 * @value: the result is stored here
329 * This function is the same as 'read_pos_ll()', but it reads an 'int'
330 * value, not 'long long'.
332 static int read_pos_int(const char *file, int *value)
336 if (read_pos_ll(file, &res))
339 /* Make sure the value is not too big */
341 errmsg("value %lld read from file \"%s\" is out of range",
352 * dev_read_hex_int - read an hex 'int' value from an MTD device sysfs file.
353 * @patt: file pattern to read from
354 * @mtd_num: MTD device number
355 * @value: the result is stored here
357 * This function returns %0 in case of success and %-1 in case of failure.
359 static int dev_read_hex_int(const char *patt, int mtd_num, int *value)
361 char file[strlen(patt) + 50];
363 sprintf(file, patt, mtd_num);
364 return read_hex_int(file, value);
368 * dev_read_pos_int - read a positive 'int' value from an MTD device sysfs file.
369 * @patt: file pattern to read from
370 * @mtd_num: MTD device number
371 * @value: the result is stored here
373 * This function returns %0 in case of success and %-1 in case of failure.
375 static int dev_read_pos_int(const char *patt, int mtd_num, int *value)
377 char file[strlen(patt) + 50];
379 sprintf(file, patt, mtd_num);
380 return read_pos_int(file, value);
384 * dev_read_pos_ll - read a positive 'long long' value from an MTD device sysfs file.
385 * @patt: file pattern to read from
386 * @mtd_num: MTD device number
387 * @value: the result is stored here
389 * This function returns %0 in case of success and %-1 in case of failure.
391 static int dev_read_pos_ll(const char *patt, int mtd_num, long long *value)
393 char file[strlen(patt) + 50];
395 sprintf(file, patt, mtd_num);
396 return read_pos_ll(file, value);
400 * type_str2int - convert MTD device type to integer.
401 * @str: MTD device type string to convert
403 * This function converts MTD device type string @str, read from sysfs, into an
406 static int type_str2int(const char *str)
408 if (!strcmp(str, "nand"))
409 return MTD_NANDFLASH;
410 if (!strcmp(str, "mlc-nand"))
411 return MTD_MLCNANDFLASH;
412 if (!strcmp(str, "nor"))
414 if (!strcmp(str, "rom"))
416 if (!strcmp(str, "absent"))
418 if (!strcmp(str, "dataflash"))
419 return MTD_DATAFLASH;
420 if (!strcmp(str, "ram"))
422 if (!strcmp(str, "ubi"))
423 return MTD_UBIVOLUME;
428 * dev_node2num - find UBI device number by its character device node.
429 * @lib: MTD library descriptor
430 * @node: name of the MTD device node
431 * @mtd_num: MTD device number is returned here
433 * This function returns %0 in case of success and %-1 in case of failure.
435 static int dev_node2num(struct libmtd *lib, const char *node, int *mtd_num)
439 struct mtd_info info;
442 return sys_errmsg("cannot get information about \"%s\"", node);
444 if (!S_ISCHR(st.st_mode)) {
445 errmsg("\"%s\" is not a character device", node);
450 mjr = major(st.st_rdev);
451 mnr = minor(st.st_rdev);
453 if (mtd_get_info((libmtd_t *)lib, &info))
456 for (i = info.lowest_mtd_num; i <= info.highest_mtd_num; i++) {
459 ret = dev_get_major(lib, i, &mjr1, &mnr1);
468 if (mjr1 == mjr && mnr1 == mnr) {
480 * sysfs_is_supported - check whether the MTD sub-system supports MTD.
481 * @lib: MTD library descriptor
483 * The Linux kernel MTD subsystem gained MTD support starting from kernel
484 * 2.6.30 and libmtd tries to use sysfs interface if possible, because the NAND
485 * sub-page size is available there (and not available at all in pre-sysfs
488 * Very old kernels did not have "/sys/class/mtd" directory. Not very old
489 * kernels (e.g., 2.6.29) did have "/sys/class/mtd/mtdX" directories, by there
490 * were no files there, e.g., the "name" file was not present. So all we can do
491 * is to check for a "/sys/class/mtd/mtdX/name" file. But this is not a
492 * reliable check, because if this is a new system with no MTD devices - we'll
493 * treat it as a pre-sysfs system.
495 static int sysfs_is_supported(struct libmtd *lib)
499 char file[strlen(lib->mtd_name) + 10];
501 sysfs_mtd = opendir(lib->sysfs_mtd);
503 if (errno == ENOENT) {
507 return sys_errmsg("cannot open \"%s\"", lib->sysfs_mtd);
511 * First of all find an "mtdX" directory. This is needed because there
512 * may be, for example, mtd1 but no mtd0.
517 struct dirent *dirent;
519 dirent = readdir(sysfs_mtd);
523 if (strlen(dirent->d_name) >= 255) {
524 errmsg("invalid entry in %s: \"%s\"",
525 lib->sysfs_mtd, dirent->d_name);
531 ret = sscanf(dirent->d_name, MTD_NAME_PATT"%s",
539 if (closedir(sysfs_mtd))
540 return sys_errmsg("closedir failed on \"%s\"", lib->sysfs_mtd);
543 /* No mtd device, treat this as pre-sysfs system */
546 sprintf(file, lib->mtd_name, num);
547 fd = open(file, O_RDONLY | O_CLOEXEC);
552 sys_errmsg("close failed on \"%s\"", file);
559 libmtd_t libmtd_open(void)
563 lib = xzalloc(sizeof(*lib));
565 lib->offs64_ioctls = OFFS64_IOCTLS_UNKNOWN;
567 lib->sysfs_mtd = mkpath("/sys", SYSFS_MTD);
571 lib->mtd = mkpath(lib->sysfs_mtd, MTD_NAME_PATT);
575 lib->mtd_name = mkpath(lib->mtd, MTD_NAME);
579 if (!sysfs_is_supported(lib)) {
581 free(lib->sysfs_mtd);
583 lib->mtd_name = lib->mtd = lib->sysfs_mtd = NULL;
587 lib->mtd_dev = mkpath(lib->mtd, MTD_DEV);
591 lib->mtd_type = mkpath(lib->mtd, MTD_TYPE);
595 lib->mtd_eb_size = mkpath(lib->mtd, MTD_EB_SIZE);
596 if (!lib->mtd_eb_size)
599 lib->mtd_size = mkpath(lib->mtd, MTD_SIZE);
603 lib->mtd_min_io_size = mkpath(lib->mtd, MTD_MIN_IO_SIZE);
604 if (!lib->mtd_min_io_size)
607 lib->mtd_subpage_size = mkpath(lib->mtd, MTD_SUBPAGE_SIZE);
608 if (!lib->mtd_subpage_size)
611 lib->mtd_oob_size = mkpath(lib->mtd, MTD_OOB_SIZE);
612 if (!lib->mtd_oob_size)
615 lib->mtd_region_cnt = mkpath(lib->mtd, MTD_REGION_CNT);
616 if (!lib->mtd_region_cnt)
619 lib->mtd_flags = mkpath(lib->mtd, MTD_FLAGS);
623 lib->sysfs_supported = 1;
627 libmtd_close((libmtd_t)lib);
631 void libmtd_close(libmtd_t desc)
633 struct libmtd *lib = (struct libmtd *)desc;
635 free(lib->mtd_flags);
636 free(lib->mtd_region_cnt);
637 free(lib->mtd_oob_size);
638 free(lib->mtd_subpage_size);
639 free(lib->mtd_min_io_size);
641 free(lib->mtd_eb_size);
646 free(lib->sysfs_mtd);
650 int mtd_dev_present(libmtd_t desc, int mtd_num) {
652 struct libmtd *lib = (struct libmtd *)desc;
654 if (!lib->sysfs_supported) {
655 return legacy_dev_present(mtd_num) == 1;
657 char file[strlen(lib->mtd) + 10];
659 sprintf(file, lib->mtd, mtd_num);
660 return !stat(file, &st);
664 int mtd_get_info(libmtd_t desc, struct mtd_info *info)
667 struct dirent *dirent;
668 struct libmtd *lib = (struct libmtd *)desc;
670 memset(info, 0, sizeof(struct mtd_info));
672 if (!lib->sysfs_supported)
673 return legacy_mtd_get_info(info);
675 info->sysfs_supported = 1;
678 * We have to scan the MTD sysfs directory to identify how many MTD
679 * devices are present.
681 sysfs_mtd = opendir(lib->sysfs_mtd);
683 if (errno == ENOENT) {
687 return sys_errmsg("cannot open \"%s\"", lib->sysfs_mtd);
690 info->lowest_mtd_num = INT_MAX;
696 dirent = readdir(sysfs_mtd);
700 if (strlen(dirent->d_name) >= 255) {
701 errmsg("invalid entry in %s: \"%s\"",
702 lib->sysfs_mtd, dirent->d_name);
707 ret = sscanf(dirent->d_name, MTD_NAME_PATT"%s",
710 info->mtd_dev_cnt += 1;
711 if (mtd_num > info->highest_mtd_num)
712 info->highest_mtd_num = mtd_num;
713 if (mtd_num < info->lowest_mtd_num)
714 info->lowest_mtd_num = mtd_num;
718 if (!dirent && errno) {
719 sys_errmsg("readdir failed on \"%s\"", lib->sysfs_mtd);
723 if (closedir(sysfs_mtd))
724 return sys_errmsg("closedir failed on \"%s\"", lib->sysfs_mtd);
726 if (info->lowest_mtd_num == INT_MAX)
727 info->lowest_mtd_num = 0;
736 int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd)
739 struct libmtd *lib = (struct libmtd *)desc;
741 memset(mtd, 0, sizeof(struct mtd_dev_info));
742 mtd->mtd_num = mtd_num;
744 if (!mtd_dev_present(desc, mtd_num)) {
747 } else if (!lib->sysfs_supported)
748 return legacy_get_dev_info1(mtd_num, mtd);
750 if (dev_get_major(lib, mtd_num, &mtd->major, &mtd->minor))
753 ret = dev_read_data(lib->mtd_name, mtd_num, &mtd->name,
757 ((char *)mtd->name)[ret - 1] = '\0';
759 ret = dev_read_data(lib->mtd_type, mtd_num, &mtd->type_str,
763 ((char *)mtd->type_str)[ret - 1] = '\0';
765 if (dev_read_pos_int(lib->mtd_eb_size, mtd_num, &mtd->eb_size))
767 if (dev_read_pos_ll(lib->mtd_size, mtd_num, &mtd->size))
769 if (dev_read_pos_int(lib->mtd_min_io_size, mtd_num, &mtd->min_io_size))
771 if (dev_read_pos_int(lib->mtd_subpage_size, mtd_num, &mtd->subpage_size))
773 if (dev_read_pos_int(lib->mtd_oob_size, mtd_num, &mtd->oob_size))
775 if (dev_read_pos_int(lib->mtd_region_cnt, mtd_num, &mtd->region_cnt))
777 if (dev_read_hex_int(lib->mtd_flags, mtd_num, &ret))
779 mtd->writable = !!(ret & MTD_WRITEABLE);
781 mtd->eb_cnt = mtd->size / mtd->eb_size;
782 mtd->type = type_str2int(mtd->type_str);
783 mtd->bb_allowed = !!(mtd->type == MTD_NANDFLASH ||
784 mtd->type == MTD_MLCNANDFLASH);
789 int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd)
792 struct libmtd *lib = (struct libmtd *)desc;
794 if (!lib->sysfs_supported)
795 return legacy_get_dev_info(node, mtd);
797 if (dev_node2num(lib, node, &mtd_num))
800 return mtd_get_dev_info1(desc, mtd_num, mtd);
803 static inline int mtd_ioctl_error(const struct mtd_dev_info *mtd, int eb,
806 return sys_errmsg("%s ioctl failed for eraseblock %d (mtd%d)",
807 sreq, eb, mtd->mtd_num);
810 static int mtd_valid_erase_block(const struct mtd_dev_info *mtd, int eb)
812 if (eb < 0 || eb >= mtd->eb_cnt) {
813 errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
814 eb, mtd->mtd_num, mtd->eb_cnt);
821 static int mtd_xlock(const struct mtd_dev_info *mtd, int fd, int eb, int req,
825 struct erase_info_user ei;
827 ret = mtd_valid_erase_block(mtd, eb);
831 ei.start = eb * mtd->eb_size;
832 ei.length = mtd->eb_size;
834 ret = ioctl(fd, req, &ei);
836 return mtd_ioctl_error(mtd, eb, sreq);
840 #define mtd_xlock(mtd, fd, eb, req) mtd_xlock(mtd, fd, eb, req, #req)
842 int mtd_lock(const struct mtd_dev_info *mtd, int fd, int eb)
844 return mtd_xlock(mtd, fd, eb, MEMLOCK);
847 int mtd_unlock(const struct mtd_dev_info *mtd, int fd, int eb)
849 return mtd_xlock(mtd, fd, eb, MEMUNLOCK);
852 int mtd_erase(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb)
855 struct libmtd *lib = (struct libmtd *)desc;
856 struct erase_info_user64 ei64;
857 struct erase_info_user ei;
859 ret = mtd_valid_erase_block(mtd, eb);
863 ei64.start = (__u64)eb * mtd->eb_size;
864 ei64.length = mtd->eb_size;
866 if (lib->offs64_ioctls == OFFS64_IOCTLS_SUPPORTED ||
867 lib->offs64_ioctls == OFFS64_IOCTLS_UNKNOWN) {
868 ret = ioctl(fd, MEMERASE64, &ei64);
872 if (errno != ENOTTY ||
873 lib->offs64_ioctls != OFFS64_IOCTLS_UNKNOWN)
874 return mtd_ioctl_error(mtd, eb, "MEMERASE64");
877 * MEMERASE64 support was added in kernel version 2.6.31, so
878 * probably we are working with older kernel and this ioctl is
881 lib->offs64_ioctls = OFFS64_IOCTLS_NOT_SUPPORTED;
884 if (ei64.start + ei64.length > 0xFFFFFFFF) {
885 errmsg("this system can address only %u eraseblocks",
886 0xFFFFFFFFU / mtd->eb_size);
891 ei.start = ei64.start;
892 ei.length = ei64.length;
893 ret = ioctl(fd, MEMERASE, &ei);
895 return mtd_ioctl_error(mtd, eb, "MEMERASE");
899 int mtd_regioninfo(int fd, int regidx, struct region_info_user *reginfo)
908 reginfo->regionindex = regidx;
910 ret = ioctl(fd, MEMGETREGIONINFO, reginfo);
912 return sys_errmsg("%s ioctl failed for erase region %d",
913 "MEMGETREGIONINFO", regidx);
918 int mtd_is_locked(const struct mtd_dev_info *mtd, int fd, int eb)
923 ei.start = eb * mtd->eb_size;
924 ei.length = mtd->eb_size;
926 ret = ioctl(fd, MEMISLOCKED, &ei);
928 if (errno != ENOTTY && errno != EOPNOTSUPP)
929 return mtd_ioctl_error(mtd, eb, "MEMISLOCKED");
937 /* Patterns to write to a physical eraseblock when torturing it */
938 static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
941 * check_pattern - check if buffer contains only a certain byte pattern.
942 * @buf: buffer to check
943 * @patt: the pattern to check
944 * @size: buffer size in bytes
946 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
947 * something else was also found.
949 static int check_pattern(const void *buf, uint8_t patt, int size)
953 for (i = 0; i < size; i++)
954 if (((const uint8_t *)buf)[i] != patt)
959 int mtd_torture(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb)
961 int err, i, patt_count;
964 normsg("run torture test for PEB %d", eb);
965 patt_count = FIO_ARRAY_SIZE(patterns);
967 buf = xmalloc(mtd->eb_size);
969 for (i = 0; i < patt_count; i++) {
970 err = mtd_erase(desc, mtd, fd, eb);
974 /* Make sure the PEB contains only 0xFF bytes */
975 err = mtd_read(mtd, fd, eb, 0, buf, mtd->eb_size);
979 err = check_pattern(buf, 0xFF, mtd->eb_size);
981 errmsg("erased PEB %d, but a non-0xFF byte found", eb);
986 /* Write a pattern and check it */
987 memset(buf, patterns[i], mtd->eb_size);
988 err = mtd_write(desc, mtd, fd, eb, 0, buf, mtd->eb_size, NULL,
993 memset(buf, ~patterns[i], mtd->eb_size);
994 err = mtd_read(mtd, fd, eb, 0, buf, mtd->eb_size);
998 err = check_pattern(buf, patterns[i], mtd->eb_size);
1000 errmsg("pattern %x checking failed for PEB %d",
1007 normsg("PEB %d passed torture test, do not mark it a bad", eb);
1014 int mtd_is_bad(const struct mtd_dev_info *mtd, int fd, int eb)
1019 ret = mtd_valid_erase_block(mtd, eb);
1023 if (!mtd->bb_allowed)
1026 seek = (loff_t)eb * mtd->eb_size;
1027 ret = ioctl(fd, MEMGETBADBLOCK, &seek);
1029 return mtd_ioctl_error(mtd, eb, "MEMGETBADBLOCK");
1033 int mtd_mark_bad(const struct mtd_dev_info *mtd, int fd, int eb)
1038 if (!mtd->bb_allowed) {
1043 ret = mtd_valid_erase_block(mtd, eb);
1047 seek = (loff_t)eb * mtd->eb_size;
1048 ret = ioctl(fd, MEMSETBADBLOCK, &seek);
1050 return mtd_ioctl_error(mtd, eb, "MEMSETBADBLOCK");
1054 int mtd_read(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
1060 ret = mtd_valid_erase_block(mtd, eb);
1064 if (offs < 0 || offs + len > mtd->eb_size) {
1065 errmsg("bad offset %d or length %d, mtd%d eraseblock size is %d",
1066 offs, len, mtd->mtd_num, mtd->eb_size);
1071 /* Seek to the beginning of the eraseblock */
1072 seek = (off_t)eb * mtd->eb_size + offs;
1073 if (lseek(fd, seek, SEEK_SET) != seek)
1074 return sys_errmsg("cannot seek mtd%d to offset %"PRIdoff_t,
1075 mtd->mtd_num, seek);
1078 ret = read(fd, buf, len);
1080 return sys_errmsg("cannot read %d bytes from mtd%d (eraseblock %d, offset %d)",
1081 len, mtd->mtd_num, eb, offs);
1088 static int legacy_auto_oob_layout(const struct mtd_dev_info *mtd, int fd,
1089 int ooblen, void *oob) {
1090 struct nand_oobinfo old_oobinfo;
1094 /* Read the current oob info */
1095 if (ioctl(fd, MEMGETOOBSEL, &old_oobinfo))
1096 return sys_errmsg("MEMGETOOBSEL failed");
1098 tmp_buf = malloc(ooblen);
1099 memcpy(tmp_buf, oob, ooblen);
1102 * We use autoplacement and have the oobinfo with the autoplacement
1103 * information from the kernel available
1105 if (old_oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
1106 int i, tags_pos = 0;
1107 for (i = 0; old_oobinfo.oobfree[i][1]; i++) {
1108 /* Set the reserved bytes to 0xff */
1109 start = old_oobinfo.oobfree[i][0];
1110 len = old_oobinfo.oobfree[i][1];
1111 memcpy(oob + start, tmp_buf + tags_pos, len);
1115 /* Set at least the ecc byte positions to 0xff */
1116 start = old_oobinfo.eccbytes;
1117 len = mtd->oob_size - start;
1118 memcpy(oob + start, tmp_buf + start, len);
1125 int mtd_write(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb,
1126 int offs, void *data, int len, void *oob, int ooblen,
1131 struct mtd_write_req ops;
1133 ret = mtd_valid_erase_block(mtd, eb);
1137 if (offs < 0 || offs + len > mtd->eb_size) {
1138 errmsg("bad offset %d or length %d, mtd%d eraseblock size is %d",
1139 offs, len, mtd->mtd_num, mtd->eb_size);
1143 if (offs % mtd->subpage_size) {
1144 errmsg("write offset %d is not aligned to mtd%d min. I/O size %d",
1145 offs, mtd->mtd_num, mtd->subpage_size);
1149 if (len % mtd->subpage_size) {
1150 errmsg("write length %d is not aligned to mtd%d min. I/O size %d",
1151 len, mtd->mtd_num, mtd->subpage_size);
1156 /* Calculate seek address */
1157 seek = (off_t)eb * mtd->eb_size + offs;
1162 ops.ooblen = ooblen;
1163 ops.usr_data = (uint64_t)(unsigned long)data;
1164 ops.usr_oob = (uint64_t)(unsigned long)oob;
1167 ret = ioctl(fd, MEMWRITE, &ops);
1170 else if (errno != ENOTTY && errno != EOPNOTSUPP)
1171 return mtd_ioctl_error(mtd, eb, "MEMWRITE");
1173 /* Fall back to old OOB ioctl() if necessary */
1174 if (mode == MTD_OPS_AUTO_OOB)
1175 if (legacy_auto_oob_layout(mtd, fd, ooblen, oob))
1177 if (mtd_write_oob(desc, mtd, fd, seek, ooblen, oob) < 0)
1178 return sys_errmsg("cannot write to OOB");
1181 /* Seek to the beginning of the eraseblock */
1182 if (lseek(fd, seek, SEEK_SET) != seek)
1183 return sys_errmsg("cannot seek mtd%d to offset %"PRIdoff_t,
1184 mtd->mtd_num, seek);
1185 ret = write(fd, data, len);
1187 return sys_errmsg("cannot write %d bytes to mtd%d "
1188 "(eraseblock %d, offset %d)",
1189 len, mtd->mtd_num, eb, offs);
1195 static int do_oob_op(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
1196 uint64_t start, uint64_t length, void *data, unsigned int cmd64,
1200 struct mtd_oob_buf64 oob64;
1201 struct mtd_oob_buf oob;
1202 unsigned long long max_offs;
1203 const char *cmd64_str, *cmd_str;
1204 struct libmtd *lib = (struct libmtd *)desc;
1206 if (cmd64 == MEMREADOOB64) {
1207 cmd64_str = "MEMREADOOB64";
1208 cmd_str = "MEMREADOOB";
1210 cmd64_str = "MEMWRITEOOB64";
1211 cmd_str = "MEMWRITEOOB";
1214 max_offs = (unsigned long long)mtd->eb_cnt * mtd->eb_size;
1215 if (start >= max_offs) {
1216 errmsg("bad page address %" PRIu64 ", mtd%d has %d eraseblocks (%llu bytes)",
1217 start, mtd->mtd_num, mtd->eb_cnt, max_offs);
1222 oob_offs = start & (mtd->min_io_size - 1);
1223 if (oob_offs + length > mtd->oob_size || length == 0) {
1224 errmsg("Cannot write %" PRIu64 " OOB bytes to address %" PRIu64 " (OOB offset %u) - mtd%d OOB size is only %d bytes",
1225 length, start, oob_offs, mtd->mtd_num, mtd->oob_size);
1230 oob64.start = start;
1231 oob64.length = length;
1232 oob64.usr_ptr = (uint64_t)(unsigned long)data;
1234 if (lib->offs64_ioctls == OFFS64_IOCTLS_SUPPORTED ||
1235 lib->offs64_ioctls == OFFS64_IOCTLS_UNKNOWN) {
1236 ret = ioctl(fd, cmd64, &oob64);
1240 if (errno != ENOTTY ||
1241 lib->offs64_ioctls != OFFS64_IOCTLS_UNKNOWN) {
1242 sys_errmsg("%s ioctl failed for mtd%d, offset %" PRIu64 " (eraseblock %" PRIu64 ")",
1243 cmd64_str, mtd->mtd_num, start, start / mtd->eb_size);
1247 * MEMREADOOB64/MEMWRITEOOB64 support was added in kernel
1248 * version 2.6.31, so probably we are working with older kernel
1249 * and these ioctls are not supported.
1251 lib->offs64_ioctls = OFFS64_IOCTLS_NOT_SUPPORTED;
1254 if (oob64.start > 0xFFFFFFFFULL) {
1255 errmsg("this system can address only up to address %lu",
1261 oob.start = oob64.start;
1262 oob.length = oob64.length;
1265 ret = ioctl(fd, cmd, &oob);
1267 sys_errmsg("%s ioctl failed for mtd%d, offset %" PRIu64 " (eraseblock %" PRIu64 ")",
1268 cmd_str, mtd->mtd_num, start, start / mtd->eb_size);
1272 int mtd_read_oob(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
1273 uint64_t start, uint64_t length, void *data)
1275 return do_oob_op(desc, mtd, fd, start, length, data,
1276 MEMREADOOB64, MEMREADOOB);
1279 int mtd_write_oob(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
1280 uint64_t start, uint64_t length, void *data)
1282 return do_oob_op(desc, mtd, fd, start, length, data,
1283 MEMWRITEOOB64, MEMWRITEOOB);
1286 int mtd_write_img(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
1287 const char *img_name)
1289 int tmp, ret, in_fd, len, written = 0;
1294 ret = mtd_valid_erase_block(mtd, eb);
1298 if (offs < 0 || offs >= mtd->eb_size) {
1299 errmsg("bad offset %d, mtd%d eraseblock size is %d",
1300 offs, mtd->mtd_num, mtd->eb_size);
1304 if (offs % mtd->subpage_size) {
1305 errmsg("write offset %d is not aligned to mtd%d min. I/O size %d",
1306 offs, mtd->mtd_num, mtd->subpage_size);
1311 in_fd = open(img_name, O_RDONLY | O_CLOEXEC);
1313 return sys_errmsg("cannot open \"%s\"", img_name);
1315 if (fstat(in_fd, &st)) {
1316 sys_errmsg("cannot stat %s", img_name);
1321 if (len % mtd->subpage_size) {
1322 errmsg("size of \"%s\" is %d byte, which is not aligned to "
1323 "mtd%d min. I/O size %d", img_name, len, mtd->mtd_num,
1328 tmp = (offs + len + mtd->eb_size - 1) / mtd->eb_size;
1329 if (eb + tmp > mtd->eb_cnt) {
1330 errmsg("\"%s\" image size is %d bytes, mtd%d size is %d "
1331 "eraseblocks, the image does not fit if we write it "
1332 "starting from eraseblock %d, offset %d",
1333 img_name, len, mtd->mtd_num, mtd->eb_cnt, eb, offs);
1338 /* Seek to the beginning of the eraseblock */
1339 seek = (off_t)eb * mtd->eb_size + offs;
1340 if (lseek(fd, seek, SEEK_SET) != seek) {
1341 sys_errmsg("cannot seek mtd%d to offset %"PRIdoff_t,
1342 mtd->mtd_num, seek);
1346 buf = xmalloc(mtd->eb_size);
1348 while (written < len) {
1352 ret = read(in_fd, buf, mtd->eb_size - offs - rd);
1354 sys_errmsg("cannot read \"%s\"", img_name);
1358 } while (ret && rd < mtd->eb_size - offs);
1360 ret = write(fd, buf, rd);
1362 sys_errmsg("cannot write %d bytes to mtd%d (eraseblock %d, offset %d)",
1363 len, mtd->mtd_num, eb, offs);
1383 int mtd_probe_node(libmtd_t desc, const char *node)
1386 struct mtd_info info;
1388 struct libmtd *lib = (struct libmtd *)desc;
1390 if (stat(node, &st))
1391 return sys_errmsg("cannot get information about \"%s\"", node);
1393 if (!S_ISCHR(st.st_mode)) {
1394 errmsg("\"%s\" is not a character device", node);
1399 mjr = major(st.st_rdev);
1400 mnr = minor(st.st_rdev);
1402 if (mtd_get_info((libmtd_t *)lib, &info))
1405 if (!lib->sysfs_supported)
1408 for (i = info.lowest_mtd_num; i <= info.highest_mtd_num; i++) {
1409 int mjr1, mnr1, ret;
1411 ret = dev_get_major(lib, i, &mjr1, &mnr1);
1413 if (errno == ENOENT)
1420 if (mjr1 == mjr && mnr1 == mnr)