#include #include #include #include #include #include #include #include int mtd_open(const char *file, mode_t mode) { int fd = open(file, mode); if (fd == -1) { printf("Error opening file %s: %s\n", file, strerror(errno)); return -1; } return fd; } int mtd_read(int fd, void *buf, size_t count) { int sz = read(fd, buf, count); if (sz == -1) { printf("Error reading fd %d: %s\n", fd, strerror(errno)); } return sz; } int mtd_write(int fd, void *buf, size_t count) { int sz = write(fd, buf, count); if (sz == -1) { printf("Error reading fd %d: %s\n", fd, strerror(errno)); } return sz; } int mtd_seek(int fd, off_t offset) { int ret = lseek(fd, offset, SEEK_SET); if (ret == -1) { printf("Error closing fd %d: %s\n", fd, strerror(errno)); } return ret; } int mtd_erase(int fd, off_t offset) { int ret = ioctl(fd, MEMERASE, &offset); if (ret == -1) { printf("Error erasing fd %d: %s\n", fd, strerror(errno)); } return ret; } int mtd_close(int fd) { int ret = close(fd); if (ret) { printf("Error closing fd %d: %s\n", fd, strerror(errno)); } return ret; } int main(int argc, char *argv[]) { int retValue; int i; int readSz1 = 0x4000; int readSz2 = 0x10000; char buffer1[readSz1]; char buffer2[readSz2]; char * file1 = "/dev/mtd6"; char * file2 = "/dev/mtd7"; for (i=0; i < 1000; i++) { int ret, sz1, sz2, fd1, fd2; fd1 = mtd_open(file1, O_RDONLY); ret = mtd_seek(fd1, 0); sz1 = mtd_read(fd1, buffer1, readSz1); ret = mtd_close(fd1); fd2 = mtd_open(file2, O_RDONLY); ret = mtd_seek(fd2, 0); sz2 = mtd_read(fd2, buffer2, readSz1); ret = mtd_close(fd2); fd1 = mtd_open(file1, O_RDWR); fd2 = mtd_open(file2, O_RDWR); sz1 = mtd_read(fd2, buffer2, readSz2); ret = mtd_seek(fd2, 0); ret = mtd_erase(fd2, 0); sz1 = mtd_write(fd2, buffer2, readSz2); ret = mtd_seek(fd1, 4); sz2 = mtd_write(fd1, "\0", 1); ret = mtd_close(fd1); ret = mtd_close(fd2); if (!(i%50)) printf("mtd, loop i=%d \n", i); } return 0; }