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>
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>
49 #define MTDREFRESH _IO('M', 50)
53 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
55 static char *buf
= NULL
;
56 static char *imagefile
= NULL
;
57 static char *jffs2file
= NULL
, *jffs2dir
= JFFS2_DEFAULT_DIR
;
58 static int buflen
= 0;
64 int mtd_open(const char *mtd
, bool block
)
70 int flags
= O_RDWR
| O_SYNC
;
72 if ((fp
= fopen("/proc/mtd", "r"))) {
73 while (fgets(dev
, sizeof(dev
), fp
)) {
74 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
75 snprintf(dev
, sizeof(dev
), "/dev/mtd%s/%d", (block
? "block" : ""), i
);
76 if ((ret
=open(dev
, flags
))<0) {
77 snprintf(dev
, sizeof(dev
), "/dev/mtd%s%d", (block
? "block" : ""), i
);
87 return open(mtd
, flags
);
90 int mtd_check_open(const char *mtd
)
92 struct mtd_info_user mtdInfo
;
95 fd
= mtd_open(mtd
, false);
97 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
101 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
102 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
106 mtdsize
= mtdInfo
.size
;
107 erasesize
= mtdInfo
.erasesize
;
112 int mtd_erase_block(int fd
, int offset
)
114 struct erase_info_user mtdEraseInfo
;
116 mtdEraseInfo
.start
= offset
;
117 mtdEraseInfo
.length
= erasesize
;
118 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
119 if (ioctl (fd
, MEMERASE
, &mtdEraseInfo
) < 0)
125 int mtd_write_buffer(int fd
, const char *buf
, int offset
, int length
)
127 lseek(fd
, offset
, SEEK_SET
);
128 write(fd
, buf
, length
);
134 image_check(int imagefd
, const char *mtd
)
138 ret
= trx_check(imagefd
, mtd
, buf
, &buflen
);
143 static int mtd_check(const char *mtd
)
149 if (strchr(mtd
, ':')) {
155 next
= strchr(mtd
, ':');
161 fd
= mtd_check_open(mtd
);
166 buf
= malloc(erasesize
);
179 mtd_unlock(const char *mtd
)
181 struct erase_info_user mtdLockInfo
;
186 if (strchr(mtd
, ':')) {
192 next
= strchr(mtd
, ':');
198 fd
= mtd_check_open(mtd
);
200 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
205 fprintf(stderr
, "Unlocking %s ...\n", mtd
);
207 mtdLockInfo
.start
= 0;
208 mtdLockInfo
.length
= mtdsize
;
209 ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
);
221 mtd_erase(const char *mtd
)
224 struct erase_info_user mtdEraseInfo
;
227 fprintf(stderr
, "Erasing %s ...\n", mtd
);
229 fd
= mtd_check_open(mtd
);
231 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
235 mtdEraseInfo
.length
= erasesize
;
237 for (mtdEraseInfo
.start
= 0;
238 mtdEraseInfo
.start
< mtdsize
;
239 mtdEraseInfo
.start
+= erasesize
) {
241 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
242 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
))
243 fprintf(stderr
, "Failed to erase block on %s at 0x%x\n", mtd
, mtdEraseInfo
.start
);
252 mtd_refresh(const char *mtd
)
257 fprintf(stderr
, "Refreshing mtd partition %s ... ", mtd
);
259 fd
= mtd_check_open(mtd
);
261 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
265 if (ioctl(fd
, MTDREFRESH
, NULL
)) {
266 fprintf(stderr
, "Failed to refresh the MTD device\n");
273 fprintf(stderr
, "\n");
279 indicate_writing(const char *mtd
)
282 fprintf(stderr
, "\nWriting from %s to %s ... ", imagefile
, mtd
);
285 fprintf(stderr
, " [ ]");
289 mtd_write(int imagefd
, const char *mtd
, char *fis_layout
)
299 static struct fis_part new_parts
[MAX_ARGS
];
300 static struct fis_part old_parts
[MAX_ARGS
];
301 int n_new
= 0, n_old
= 0;
304 const char *tmp
= mtd
;
308 memset(&old_parts
, 0, sizeof(old_parts
));
309 memset(&new_parts
, 0, sizeof(new_parts
));
312 next
= strchr(tmp
, ':');
314 next
= (char *) tmp
+ strlen(tmp
);
316 memcpy(old_parts
[n_old
].name
, tmp
, next
- tmp
);
322 for (word
= strtok_r(fis_layout
, ",", &brkt
);
324 word
= strtok_r(NULL
, ",", &brkt
)) {
326 tmp
= strtok(word
, ":");
327 strncpy((char *) new_parts
[n_new
].name
, tmp
, sizeof(new_parts
[n_new
].name
) - 1);
329 tmp
= strtok(NULL
, ":");
333 new_parts
[n_new
].size
= strtoul(tmp
, NULL
, 0);
335 tmp
= strtok(NULL
, ":");
339 new_parts
[n_new
].loadaddr
= strtoul(tmp
, NULL
, 16);
343 ret
= fis_validate(old_parts
, n_old
, new_parts
, n_new
);
345 fprintf(stderr
, "Failed to validate the new FIS partition table\n");
353 if (strchr(mtd
, ':')) {
361 next
= strchr(mtd
, ':');
367 fd
= mtd_check_open(mtd
);
369 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
373 indicate_writing(mtd
);
377 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
378 while (buflen
< erasesize
) {
379 r
= read(imagefd
, buf
+ buflen
, erasesize
- buflen
);
381 if ((errno
== EINTR
) || (errno
== EAGAIN
))
402 indicate_writing(mtd
);
408 if (memcmp(buf
, JFFS2_EOF
, sizeof(JFFS2_EOF
) - 1) == 0) {
410 fprintf(stderr
, "\b\b\b ");
412 fprintf(stderr
, "\nAppending jffs2 data from %s to %s...", jffs2file
, mtd
);
413 /* got an EOF marker - this is the place to add some jffs2 data */
414 skip
= mtd_replace_jffs2(mtd
, fd
, e
, jffs2file
);
416 /* don't add it again */
426 /* no EOF marker, make sure we figure out the last inode number
427 * before appending some data */
428 mtd_parse_jffs2data(buf
, jffs2dir
);
431 /* need to erase the next block before writing data to it */
434 while (w
+ buflen
> e
) {
436 fprintf(stderr
, "\b\b\b[e]");
439 if (mtd_erase_block(fd
, e
) < 0) {
442 write(fd
, buf
+ offset
, e
- w
);
449 fprintf(stderr
, "\b\b\b \n");
452 fprintf(stderr
, "Failed to erase block\n");
457 /* erase the chunk */
463 fprintf(stderr
, "\b\b\b[w]");
465 if ((result
= write(fd
, buf
+ offset
, buflen
)) < buflen
) {
467 fprintf(stderr
, "Error writing image.\n");
470 fprintf(stderr
, "Insufficient space.\n");
481 fprintf(stderr
, "\b\b\b\b ");
485 fprintf(stderr
, "\n");
489 if (fis_remap(old_parts
, n_old
, new_parts
, n_new
) < 0)
490 fprintf(stderr
, "Failed to update the FIS partition table\n");
498 static void usage(void)
500 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
501 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
502 "mtd recognizes these commands:\n"
503 " unlock unlock the device\n"
504 " refresh refresh mtd partition\n"
505 " erase erase all data on device\n"
506 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
507 " jffs2write <file> append <file> to the jffs2 partition on the device\n");
510 " fixtrx fix the checksum in a trx header on first boot\n");
513 "Following options are available:\n"
514 " -q quiet mode (once: no [w] on writing,\n"
515 " twice: no status messages)\n"
516 " -n write without first erasing the blocks\n"
517 " -r reboot after successful command\n"
518 " -f force write without trx checks\n"
519 " -e <device> erase <device> before executing the command\n"
520 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
521 " -j <name> integrate <file> into jffs2 data when writing an image\n");
524 " -o offset offset of the image header in the partition(for fixtrx)\n");
528 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
529 " alter the fis partition table to create new partitions replacing\n"
530 " the partitions provided as argument to the write command\n"
531 " (only valid together with the write command)\n"
534 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
535 " mtd -r write linux.trx linux\n\n");
539 static void do_reboot(void)
541 fprintf(stderr
, "Rebooting ...\n");
544 /* try regular reboot method first */
545 system("/sbin/reboot");
548 /* if we're still alive at this point, force the kernel to reboot */
549 syscall(SYS_reboot
,LINUX_REBOOT_MAGIC1
,LINUX_REBOOT_MAGIC2
,LINUX_REBOOT_CMD_RESTART
,NULL
);
552 int main (int argc
, char **argv
)
554 int ch
, i
, boot
, imagefd
= 0, force
, unlocked
;
555 char *erase
[MAX_ARGS
], *device
= NULL
;
556 char *fis_layout
= NULL
;
574 while ((ch
= getopt(argc
, argv
,
578 "frnqe:d:j:o:")) != -1)
597 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
608 fprintf(stderr
, "-o: is not available on this platform\n");
612 offset
= strtoul(optarg
, 0, 0);
614 fprintf(stderr
, "-o: illegal numeric string\n");
633 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
636 } else if ((strcmp(argv
[0], "refresh") == 0) && (argc
== 2)) {
639 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
642 } else if (((strcmp(argv
[0], "fixtrx") == 0) && (argc
== 2)) && mtd_fixtrx
) {
645 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
649 if (strcmp(argv
[1], "-") == 0) {
650 imagefile
= "<stdin>";
654 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
655 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
660 if (!mtd_check(device
)) {
661 fprintf(stderr
, "Can't open device for writing!\n");
664 /* check trx file before erasing or writing anything */
665 if (!image_check(imagefd
, device
) && !force
) {
666 fprintf(stderr
, "Image check failed.\n");
669 } else if ((strcmp(argv
[0], "jffs2write") == 0) && (argc
== 3)) {
670 cmd
= CMD_JFFS2WRITE
;
674 if (!mtd_check(device
)) {
675 fprintf(stderr
, "Can't open device for writing!\n");
686 while (erase
[i
] != NULL
) {
687 mtd_unlock(erase
[i
]);
689 if (strcmp(erase
[i
], device
) == 0)
707 mtd_write(imagefd
, device
, fis_layout
);
712 mtd_write_jffs2(device
, imagefile
, jffs2dir
);
719 mtd_fixtrx(device
, offset
);