2 * mtd - simple memory technology device manipulation tool
4 * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>,
5 * Copyright (C) 2005-2009 Felix Fietkau <nbd@openwrt.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License v2
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * The code is based on the linux-mtd examples.
31 #include <sys/ioctl.h>
32 #include <sys/syscall.h>
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/mount.h>
42 #include <sys/reboot.h>
43 #include <linux/reboot.h>
44 #include <mtd/mtd-user.h>
48 #include <libubox/md5.h>
51 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
53 static char *buf
= NULL
;
54 static char *imagefile
= NULL
;
55 static char *jffs2file
= NULL
, *jffs2dir
= JFFS2_DEFAULT_DIR
;
56 static int buflen
= 0;
61 int jffs2_skip_bytes
=0;
64 int mtd_open(const char *mtd
, bool block
)
70 int flags
= O_RDWR
| O_SYNC
;
73 snprintf(name
, sizeof(name
), "\"%s\"", mtd
);
74 if ((fp
= fopen("/proc/mtd", "r"))) {
75 while (fgets(dev
, sizeof(dev
), fp
)) {
76 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, name
)) {
77 snprintf(dev
, sizeof(dev
), "/dev/mtd%s/%d", (block
? "block" : ""), i
);
78 if ((ret
=open(dev
, flags
))<0) {
79 snprintf(dev
, sizeof(dev
), "/dev/mtd%s%d", (block
? "block" : ""), i
);
89 return open(mtd
, flags
);
92 int mtd_check_open(const char *mtd
)
94 struct mtd_info_user mtdInfo
;
97 fd
= mtd_open(mtd
, false);
99 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
103 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
104 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
108 mtdsize
= mtdInfo
.size
;
109 erasesize
= mtdInfo
.erasesize
;
110 mtdtype
= mtdInfo
.type
;
115 int mtd_block_is_bad(int fd
, int offset
)
120 if (mtdtype
== MTD_NANDFLASH
)
122 r
= ioctl(fd
, MEMGETBADBLOCK
, &o
);
125 fprintf(stderr
, "Failed to get erase block status\n");
132 int mtd_erase_block(int fd
, int offset
)
134 struct erase_info_user mtdEraseInfo
;
136 mtdEraseInfo
.start
= offset
;
137 mtdEraseInfo
.length
= erasesize
;
138 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
139 if (ioctl (fd
, MEMERASE
, &mtdEraseInfo
) < 0)
145 int mtd_write_buffer(int fd
, const char *buf
, int offset
, int length
)
147 lseek(fd
, offset
, SEEK_SET
);
148 write(fd
, buf
, length
);
154 image_check(int imagefd
, const char *mtd
)
158 ret
= trx_check(imagefd
, mtd
, buf
, &buflen
);
164 static int mtd_check(const char *mtd
)
170 if (strchr(mtd
, ':')) {
176 next
= strchr(mtd
, ':');
182 fd
= mtd_check_open(mtd
);
187 buf
= malloc(erasesize
);
200 mtd_unlock(const char *mtd
)
202 struct erase_info_user mtdLockInfo
;
207 if (strchr(mtd
, ':')) {
213 next
= strchr(mtd
, ':');
219 fd
= mtd_check_open(mtd
);
221 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
226 fprintf(stderr
, "Unlocking %s ...\n", mtd
);
228 mtdLockInfo
.start
= 0;
229 mtdLockInfo
.length
= mtdsize
;
230 ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
);
242 mtd_erase(const char *mtd
)
245 struct erase_info_user mtdEraseInfo
;
248 fprintf(stderr
, "Erasing %s ...\n", mtd
);
250 fd
= mtd_check_open(mtd
);
252 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
256 mtdEraseInfo
.length
= erasesize
;
258 for (mtdEraseInfo
.start
= 0;
259 mtdEraseInfo
.start
< mtdsize
;
260 mtdEraseInfo
.start
+= erasesize
) {
261 if (mtd_block_is_bad(fd
, mtdEraseInfo
.start
)) {
263 fprintf(stderr
, "\nSkipping bad block at 0x%x ", mtdEraseInfo
.start
);
265 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
266 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
))
267 fprintf(stderr
, "Failed to erase block on %s at 0x%x\n", mtd
, mtdEraseInfo
.start
);
277 mtd_dump(const char *mtd
, int part_offset
, int size
)
279 int ret
= 0, offset
= 0;
284 fprintf(stderr
, "Dumping %s ...\n", mtd
);
286 fd
= mtd_check_open(mtd
);
288 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
296 lseek(fd
, part_offset
, SEEK_SET
);
298 buf
= malloc(erasesize
);
303 int len
= (size
> erasesize
) ? (erasesize
) : (size
);
304 int rlen
= read(fd
, buf
, len
);
312 if (!rlen
|| rlen
!= len
)
314 if (mtd_block_is_bad(fd
, offset
)) {
315 fprintf(stderr
, "skipping bad block at 0x%08x\n", offset
);
329 mtd_verify(const char *mtd
, char *file
)
331 uint32_t f_md5
[4], m_md5
[4];
338 fprintf(stderr
, "Verifying %s against %s ...\n", mtd
, file
);
340 if (stat(file
, &s
) || md5sum(file
, f_md5
) < 0) {
341 fprintf(stderr
, "Failed to hash %s\n", file
);
345 fd
= mtd_check_open(mtd
);
347 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
354 int len
= (s
.st_size
> sizeof(buf
)) ? (sizeof(buf
)) : (s
.st_size
);
355 int rlen
= read(fd
, buf
, len
);
365 md5_hash(buf
, rlen
, &ctx
);
367 } while (s
.st_size
> 0);
369 md5_end(m_md5
, &ctx
);
371 fprintf(stderr
, "%08x%08x%08x%08x - %s\n", m_md5
[0], m_md5
[1], m_md5
[2], m_md5
[3], mtd
);
372 fprintf(stderr
, "%08x%08x%08x%08x - %s\n", f_md5
[0], f_md5
[1], f_md5
[2], f_md5
[3], file
);
374 ret
= memcmp(f_md5
, m_md5
, sizeof(m_md5
));
376 fprintf(stderr
, "Success\n");
378 fprintf(stderr
, "Failed\n");
386 indicate_writing(const char *mtd
)
389 fprintf(stderr
, "\nWriting from %s to %s ... ", imagefile
, mtd
);
392 fprintf(stderr
, " [ ]");
396 mtd_write(int imagefd
, const char *mtd
, char *fis_layout
, size_t part_offset
)
404 int jffs2_replaced
= 0;
405 int skip_bad_blocks
= 0;
408 static struct fis_part new_parts
[MAX_ARGS
];
409 static struct fis_part old_parts
[MAX_ARGS
];
410 int n_new
= 0, n_old
= 0;
413 const char *tmp
= mtd
;
417 memset(&old_parts
, 0, sizeof(old_parts
));
418 memset(&new_parts
, 0, sizeof(new_parts
));
421 next
= strchr(tmp
, ':');
423 next
= (char *) tmp
+ strlen(tmp
);
425 memcpy(old_parts
[n_old
].name
, tmp
, next
- tmp
);
431 for (word
= strtok_r(fis_layout
, ",", &brkt
);
433 word
= strtok_r(NULL
, ",", &brkt
)) {
435 tmp
= strtok(word
, ":");
436 strncpy((char *) new_parts
[n_new
].name
, tmp
, sizeof(new_parts
[n_new
].name
) - 1);
438 tmp
= strtok(NULL
, ":");
442 new_parts
[n_new
].size
= strtoul(tmp
, NULL
, 0);
444 tmp
= strtok(NULL
, ":");
448 new_parts
[n_new
].loadaddr
= strtoul(tmp
, NULL
, 16);
452 ret
= fis_validate(old_parts
, n_old
, new_parts
, n_new
);
454 fprintf(stderr
, "Failed to validate the new FIS partition table\n");
462 if (strchr(mtd
, ':')) {
470 next
= strchr(mtd
, ':');
476 fd
= mtd_check_open(mtd
);
478 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
481 if (part_offset
> 0) {
482 fprintf(stderr
, "Seeking on mtd device '%s' to: %zu\n", mtd
, part_offset
);
483 lseek(fd
, part_offset
, SEEK_SET
);
486 indicate_writing(mtd
);
490 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
491 while (buflen
< erasesize
) {
492 r
= read(imagefd
, buf
+ buflen
, erasesize
- buflen
);
494 if ((errno
== EINTR
) || (errno
== EAGAIN
))
511 if (buflen
< erasesize
) {
512 /* Pad block to eraseblock size */
513 memset(&buf
[buflen
], 0xff, erasesize
- buflen
);
521 indicate_writing(mtd
);
526 if (jffs2file
&& w
>= jffs2_skip_bytes
) {
527 if (memcmp(buf
, JFFS2_EOF
, sizeof(JFFS2_EOF
) - 1) == 0) {
529 fprintf(stderr
, "\b\b\b ");
531 fprintf(stderr
, "\nAppending jffs2 data from %s to %s...", jffs2file
, mtd
);
532 /* got an EOF marker - this is the place to add some jffs2 data */
533 skip
= mtd_replace_jffs2(mtd
, fd
, e
, jffs2file
);
536 /* don't add it again */
546 /* no EOF marker, make sure we figure out the last inode number
547 * before appending some data */
548 mtd_parse_jffs2data(buf
, jffs2dir
);
551 /* need to erase the next block before writing data to it */
554 while (w
+ buflen
> e
- skip_bad_blocks
) {
556 fprintf(stderr
, "\b\b\b[e]");
558 if (mtd_block_is_bad(fd
, e
)) {
560 fprintf(stderr
, "\nSkipping bad block at 0x%08zx ", e
);
562 skip_bad_blocks
+= erasesize
;
565 // Move the file pointer along over the bad block.
566 lseek(fd
, erasesize
, SEEK_CUR
);
570 if (mtd_erase_block(fd
, e
) < 0) {
573 write(fd
, buf
+ offset
, e
- w
);
580 fprintf(stderr
, "\b\b\b \n");
583 fprintf(stderr
, "Failed to erase block\n");
588 /* erase the chunk */
594 fprintf(stderr
, "\b\b\b[w]");
596 if ((result
= write(fd
, buf
+ offset
, buflen
)) < buflen
) {
598 fprintf(stderr
, "Error writing image.\n");
601 fprintf(stderr
, "Insufficient space.\n");
611 if (jffs2_replaced
&& trx_fixup
) {
616 fprintf(stderr
, "\b\b\b\b ");
619 fprintf(stderr
, "\n");
623 if (fis_remap(old_parts
, n_old
, new_parts
, n_new
) < 0)
624 fprintf(stderr
, "Failed to update the FIS partition table\n");
632 static void usage(void)
634 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
635 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
636 "mtd recognizes these commands:\n"
637 " unlock unlock the device\n"
638 " refresh refresh mtd partition\n"
639 " erase erase all data on device\n"
640 " verify <imagefile>|- verify <imagefile> (use - for stdin) to device\n"
641 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
642 " jffs2write <file> append <file> to the jffs2 partition on the device\n");
645 " resetbc <device> reset the uboot boot counter\n");
649 " fixtrx fix the checksum in a trx header on first boot\n");
653 " fixseama fix the checksum in a seama header on first boot\n");
656 "Following options are available:\n"
657 " -q quiet mode (once: no [w] on writing,\n"
658 " twice: no status messages)\n"
659 " -n write without first erasing the blocks\n"
660 " -r reboot after successful command\n"
661 " -f force write without trx checks\n"
662 " -e <device> erase <device> before executing the command\n"
663 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
664 " -j <name> integrate <file> into jffs2 data when writing an image\n"
665 " -s <number> skip the first n bytes when appending data to the jffs2 partiton, defaults to \"0\"\n"
666 " -p write beginning at partition offset\n"
667 " -l <length> the length of data that we want to dump\n");
670 " -o offset offset of the image header in the partition(for fixtrx)\n");
674 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
675 " alter the fis partition table to create new partitions replacing\n"
676 " the partitions provided as argument to the write command\n"
677 " (only valid together with the write command)\n"
680 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
681 " mtd -r write linux.trx linux\n\n");
685 static void do_reboot(void)
687 fprintf(stderr
, "Rebooting ...\n");
690 /* try regular reboot method first */
691 system("/sbin/reboot");
694 /* if we're still alive at this point, force the kernel to reboot */
695 syscall(SYS_reboot
,LINUX_REBOOT_MAGIC1
,LINUX_REBOOT_MAGIC2
,LINUX_REBOOT_CMD_RESTART
,NULL
);
698 int main (int argc
, char **argv
)
700 int ch
, i
, boot
, imagefd
= 0, force
, unlocked
;
701 char *erase
[MAX_ARGS
], *device
= NULL
;
702 char *fis_layout
= NULL
;
703 size_t offset
= 0, part_offset
= 0, dump_len
= 0;
723 while ((ch
= getopt(argc
, argv
,
727 "frnqe:d:s:j:p:o:l:")) != -1)
743 jffs2_skip_bytes
= strtoul(optarg
, 0, 0);
745 fprintf(stderr
, "-s: illegal numeric string\n");
754 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
765 part_offset
= strtoul(optarg
, 0, 0);
767 fprintf(stderr
, "-p: illegal numeric string\n");
773 dump_len
= strtoul(optarg
, 0, 0);
775 fprintf(stderr
, "-l: illegal numeric string\n");
781 offset
= strtoul(optarg
, 0, 0);
783 fprintf(stderr
, "-o: illegal numeric string\n");
802 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
805 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
808 } else if (((strcmp(argv
[0], "resetbc") == 0) && (argc
== 2)) && mtd_resetbc
) {
811 } else if (((strcmp(argv
[0], "fixtrx") == 0) && (argc
== 2)) && mtd_fixtrx
) {
814 } else if (((strcmp(argv
[0], "fixseama") == 0) && (argc
== 2)) && mtd_fixseama
) {
817 } else if ((strcmp(argv
[0], "verify") == 0) && (argc
== 3)) {
821 } else if ((strcmp(argv
[0], "dump") == 0) && (argc
== 2)) {
824 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
828 if (strcmp(argv
[1], "-") == 0) {
829 imagefile
= "<stdin>";
833 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
834 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
839 if (!mtd_check(device
)) {
840 fprintf(stderr
, "Can't open device for writing!\n");
843 /* check trx file before erasing or writing anything */
844 if (!image_check(imagefd
, device
) && !force
) {
845 fprintf(stderr
, "Image check failed.\n");
848 } else if ((strcmp(argv
[0], "jffs2write") == 0) && (argc
== 3)) {
849 cmd
= CMD_JFFS2WRITE
;
853 if (!mtd_check(device
)) {
854 fprintf(stderr
, "Can't open device for writing!\n");
865 while (erase
[i
] != NULL
) {
866 mtd_unlock(erase
[i
]);
868 if (strcmp(erase
[i
], device
) == 0)
879 mtd_verify(device
, imagefile
);
882 mtd_dump(device
, offset
, dump_len
);
892 mtd_write(imagefd
, device
, fis_layout
, part_offset
);
897 mtd_write_jffs2(device
, imagefile
, jffs2dir
);
901 mtd_fixtrx(device
, offset
);
909 mtd_fixseama(device
, 0);