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.
30 #include <sys/ioctl.h>
31 #include <sys/syscall.h>
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/mount.h>
41 #include <sys/reboot.h>
42 #include <linux/reboot.h>
43 #include <mtd/mtd-user.h>
47 #include <libubox/md5.h>
50 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
52 static char *buf
= NULL
;
53 static char *imagefile
= NULL
;
54 static char *jffs2file
= NULL
, *jffs2dir
= JFFS2_DEFAULT_DIR
;
55 static int buflen
= 0;
60 int jffs2_skip_bytes
=0;
63 int mtd_open(const char *mtd
, bool block
)
69 int flags
= O_RDWR
| O_SYNC
;
72 snprintf(name
, sizeof(name
), "\"%s\"", mtd
);
73 if ((fp
= fopen("/proc/mtd", "r"))) {
74 while (fgets(dev
, sizeof(dev
), fp
)) {
75 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, name
)) {
76 snprintf(dev
, sizeof(dev
), "/dev/mtd%s/%d", (block
? "block" : ""), i
);
77 if ((ret
=open(dev
, flags
))<0) {
78 snprintf(dev
, sizeof(dev
), "/dev/mtd%s%d", (block
? "block" : ""), i
);
88 return open(mtd
, flags
);
91 int mtd_check_open(const char *mtd
)
93 struct mtd_info_user mtdInfo
;
96 fd
= mtd_open(mtd
, false);
98 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
102 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
103 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
107 mtdsize
= mtdInfo
.size
;
108 erasesize
= mtdInfo
.erasesize
;
109 mtdtype
= mtdInfo
.type
;
114 int mtd_block_is_bad(int fd
, int offset
)
119 if (mtdtype
== MTD_NANDFLASH
)
121 r
= ioctl(fd
, MEMGETBADBLOCK
, &o
);
124 fprintf(stderr
, "Failed to get erase block status\n");
131 int mtd_erase_block(int fd
, int offset
)
133 struct erase_info_user mtdEraseInfo
;
135 mtdEraseInfo
.start
= offset
;
136 mtdEraseInfo
.length
= erasesize
;
137 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
138 if (ioctl (fd
, MEMERASE
, &mtdEraseInfo
) < 0)
144 int mtd_write_buffer(int fd
, const char *buf
, int offset
, int length
)
146 lseek(fd
, offset
, SEEK_SET
);
147 write(fd
, buf
, length
);
153 image_check(int imagefd
, const char *mtd
)
157 ret
= trx_check(imagefd
, mtd
, buf
, &buflen
);
163 static int mtd_check(const char *mtd
)
169 if (strchr(mtd
, ':')) {
175 next
= strchr(mtd
, ':');
181 fd
= mtd_check_open(mtd
);
186 buf
= malloc(erasesize
);
199 mtd_unlock(const char *mtd
)
201 struct erase_info_user mtdLockInfo
;
206 if (strchr(mtd
, ':')) {
212 next
= strchr(mtd
, ':');
218 fd
= mtd_check_open(mtd
);
220 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
225 fprintf(stderr
, "Unlocking %s ...\n", mtd
);
227 mtdLockInfo
.start
= 0;
228 mtdLockInfo
.length
= mtdsize
;
229 ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
);
241 mtd_erase(const char *mtd
)
244 struct erase_info_user mtdEraseInfo
;
247 fprintf(stderr
, "Erasing %s ...\n", mtd
);
249 fd
= mtd_check_open(mtd
);
251 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
255 mtdEraseInfo
.length
= erasesize
;
257 for (mtdEraseInfo
.start
= 0;
258 mtdEraseInfo
.start
< mtdsize
;
259 mtdEraseInfo
.start
+= erasesize
) {
260 if (mtd_block_is_bad(fd
, mtdEraseInfo
.start
)) {
262 fprintf(stderr
, "\nSkipping bad block at 0x%x ", mtdEraseInfo
.start
);
264 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
265 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
))
266 fprintf(stderr
, "Failed to erase block on %s at 0x%x\n", mtd
, mtdEraseInfo
.start
);
276 mtd_verify(const char *mtd
, char *file
)
278 uint32_t f_md5
[4], m_md5
[4];
285 fprintf(stderr
, "Verifying %s against %s ...\n", mtd
, file
);
287 if (stat(file
, &s
) || md5sum(file
, f_md5
)) {
288 fprintf(stderr
, "Failed to hash %s\n", file
);
292 fd
= mtd_check_open(mtd
);
294 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
301 int len
= (s
.st_size
> sizeof(buf
)) ? (sizeof(buf
)) : (s
.st_size
);
302 int rlen
= read(fd
, buf
, len
);
312 md5_hash(buf
, rlen
, &ctx
);
314 } while (s
.st_size
> 0);
316 md5_end(m_md5
, &ctx
);
318 fprintf(stderr
, "%08x%08x%08x%08x - %s\n", m_md5
[0], m_md5
[1], m_md5
[2], m_md5
[3], mtd
);
319 fprintf(stderr
, "%08x%08x%08x%08x - %s\n", f_md5
[0], f_md5
[1], f_md5
[2], f_md5
[3], file
);
321 ret
= memcmp(f_md5
, m_md5
, sizeof(m_md5
));
323 fprintf(stderr
, "Success\n");
325 fprintf(stderr
, "Failed\n");
333 indicate_writing(const char *mtd
)
336 fprintf(stderr
, "\nWriting from %s to %s ... ", imagefile
, mtd
);
339 fprintf(stderr
, " [ ]");
343 mtd_write(int imagefd
, const char *mtd
, char *fis_layout
, size_t part_offset
)
351 int jffs2_replaced
= 0;
352 int skip_bad_blocks
= 0;
355 static struct fis_part new_parts
[MAX_ARGS
];
356 static struct fis_part old_parts
[MAX_ARGS
];
357 int n_new
= 0, n_old
= 0;
360 const char *tmp
= mtd
;
364 memset(&old_parts
, 0, sizeof(old_parts
));
365 memset(&new_parts
, 0, sizeof(new_parts
));
368 next
= strchr(tmp
, ':');
370 next
= (char *) tmp
+ strlen(tmp
);
372 memcpy(old_parts
[n_old
].name
, tmp
, next
- tmp
);
378 for (word
= strtok_r(fis_layout
, ",", &brkt
);
380 word
= strtok_r(NULL
, ",", &brkt
)) {
382 tmp
= strtok(word
, ":");
383 strncpy((char *) new_parts
[n_new
].name
, tmp
, sizeof(new_parts
[n_new
].name
) - 1);
385 tmp
= strtok(NULL
, ":");
389 new_parts
[n_new
].size
= strtoul(tmp
, NULL
, 0);
391 tmp
= strtok(NULL
, ":");
395 new_parts
[n_new
].loadaddr
= strtoul(tmp
, NULL
, 16);
399 ret
= fis_validate(old_parts
, n_old
, new_parts
, n_new
);
401 fprintf(stderr
, "Failed to validate the new FIS partition table\n");
409 if (strchr(mtd
, ':')) {
417 next
= strchr(mtd
, ':');
423 fd
= mtd_check_open(mtd
);
425 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
428 if (part_offset
> 0) {
429 fprintf(stderr
, "Seeking on mtd device '%s' to: %zu\n", mtd
, part_offset
);
430 lseek(fd
, part_offset
, SEEK_SET
);
433 indicate_writing(mtd
);
437 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
438 while (buflen
< erasesize
) {
439 r
= read(imagefd
, buf
+ buflen
, erasesize
- buflen
);
441 if ((errno
== EINTR
) || (errno
== EAGAIN
))
458 if (buflen
< erasesize
) {
459 /* Pad block to eraseblock size */
460 memset(&buf
[buflen
], 0xff, erasesize
- buflen
);
468 indicate_writing(mtd
);
473 if (jffs2file
&& w
>= jffs2_skip_bytes
) {
474 if (memcmp(buf
, JFFS2_EOF
, sizeof(JFFS2_EOF
) - 1) == 0) {
476 fprintf(stderr
, "\b\b\b ");
478 fprintf(stderr
, "\nAppending jffs2 data from %s to %s...", jffs2file
, mtd
);
479 /* got an EOF marker - this is the place to add some jffs2 data */
480 skip
= mtd_replace_jffs2(mtd
, fd
, e
, jffs2file
);
483 /* don't add it again */
493 /* no EOF marker, make sure we figure out the last inode number
494 * before appending some data */
495 mtd_parse_jffs2data(buf
, jffs2dir
);
498 /* need to erase the next block before writing data to it */
501 while (w
+ buflen
> e
- skip_bad_blocks
) {
503 fprintf(stderr
, "\b\b\b[e]");
505 if (mtd_block_is_bad(fd
, e
)) {
507 fprintf(stderr
, "\nSkipping bad block at 0x%08x ", e
);
509 skip_bad_blocks
+= erasesize
;
512 // Move the file pointer along over the bad block.
513 lseek(fd
, erasesize
, SEEK_CUR
);
517 if (mtd_erase_block(fd
, e
) < 0) {
520 write(fd
, buf
+ offset
, e
- w
);
527 fprintf(stderr
, "\b\b\b \n");
530 fprintf(stderr
, "Failed to erase block\n");
535 /* erase the chunk */
541 fprintf(stderr
, "\b\b\b[w]");
543 if ((result
= write(fd
, buf
+ offset
, buflen
)) < buflen
) {
545 fprintf(stderr
, "Error writing image.\n");
548 fprintf(stderr
, "Insufficient space.\n");
558 if (jffs2_replaced
&& trx_fixup
) {
563 fprintf(stderr
, "\b\b\b\b ");
567 fprintf(stderr
, "\n");
571 if (fis_remap(old_parts
, n_old
, new_parts
, n_new
) < 0)
572 fprintf(stderr
, "Failed to update the FIS partition table\n");
580 static void usage(void)
582 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
583 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
584 "mtd recognizes these commands:\n"
585 " unlock unlock the device\n"
586 " refresh refresh mtd partition\n"
587 " erase erase all data on device\n"
588 " verify <imagefile>|- verify <imagefile> (use - for stdin) to device\n"
589 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
590 " jffs2write <file> append <file> to the jffs2 partition on the device\n");
593 " fixtrx fix the checksum in a trx header on first boot\n");
597 " fixseama fix the checksum in a seama header on first boot\n");
600 "Following options are available:\n"
601 " -q quiet mode (once: no [w] on writing,\n"
602 " twice: no status messages)\n"
603 " -n write without first erasing the blocks\n"
604 " -r reboot after successful command\n"
605 " -f force write without trx checks\n"
606 " -e <device> erase <device> before executing the command\n"
607 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
608 " -j <name> integrate <file> into jffs2 data when writing an image\n"
609 " -s <number> skip the first n bytes when appending data to the jffs2 partiton, defaults to \"0\"\n"
610 " -p write beginning at partition offset\n");
613 " -o offset offset of the image header in the partition(for fixtrx)\n");
617 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
618 " alter the fis partition table to create new partitions replacing\n"
619 " the partitions provided as argument to the write command\n"
620 " (only valid together with the write command)\n"
623 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
624 " mtd -r write linux.trx linux\n\n");
628 static void do_reboot(void)
630 fprintf(stderr
, "Rebooting ...\n");
633 /* try regular reboot method first */
634 system("/sbin/reboot");
637 /* if we're still alive at this point, force the kernel to reboot */
638 syscall(SYS_reboot
,LINUX_REBOOT_MAGIC1
,LINUX_REBOOT_MAGIC2
,LINUX_REBOOT_CMD_RESTART
,NULL
);
641 int main (int argc
, char **argv
)
643 int ch
, i
, boot
, imagefd
= 0, force
, unlocked
;
644 char *erase
[MAX_ARGS
], *device
= NULL
;
645 char *fis_layout
= NULL
;
646 size_t offset
= 0, part_offset
= 0;
664 while ((ch
= getopt(argc
, argv
,
668 "frnqe:d:s:j:p:o:")) != -1)
684 jffs2_skip_bytes
= strtoul(optarg
, 0, 0);
686 fprintf(stderr
, "-s: illegal numeric string\n");
695 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
706 part_offset
= strtoul(optarg
, 0, 0);
708 fprintf(stderr
, "-p: illegal numeric string\n");
714 fprintf(stderr
, "-o: is not available on this platform\n");
718 offset
= strtoul(optarg
, 0, 0);
720 fprintf(stderr
, "-o: illegal numeric string\n");
739 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
742 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
745 } else if (((strcmp(argv
[0], "fixtrx") == 0) && (argc
== 2)) && mtd_fixtrx
) {
748 } else if (((strcmp(argv
[0], "fixseama") == 0) && (argc
== 2)) && mtd_fixseama
) {
751 } else if ((strcmp(argv
[0], "verify") == 0) && (argc
== 3)) {
755 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
759 if (strcmp(argv
[1], "-") == 0) {
760 imagefile
= "<stdin>";
764 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
765 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
770 if (!mtd_check(device
)) {
771 fprintf(stderr
, "Can't open device for writing!\n");
774 /* check trx file before erasing or writing anything */
775 if (!image_check(imagefd
, device
) && !force
) {
776 fprintf(stderr
, "Image check failed.\n");
779 } else if ((strcmp(argv
[0], "jffs2write") == 0) && (argc
== 3)) {
780 cmd
= CMD_JFFS2WRITE
;
784 if (!mtd_check(device
)) {
785 fprintf(stderr
, "Can't open device for writing!\n");
796 while (erase
[i
] != NULL
) {
797 mtd_unlock(erase
[i
]);
799 if (strcmp(erase
[i
], device
) == 0)
810 mtd_verify(device
, imagefile
);
820 mtd_write(imagefd
, device
, fis_layout
, part_offset
);
825 mtd_write_jffs2(device
, imagefile
, jffs2dir
);
829 mtd_fixtrx(device
, offset
);
833 mtd_fixseama(device
, 0);