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>
49 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
52 uint32_t magic
; /* "HDR0" */
53 uint32_t len
; /* Length of file including header */
54 uint32_t crc32
; /* 32-bit CRC from flag_version to end of file */
55 uint32_t flag_version
; /* 0:15 flags, 16:31 version */
56 uint32_t offsets
[3]; /* Offsets of partitions from start of header */
59 static char *buf
= NULL
;
60 static char *imagefile
= NULL
;
61 static char *jffs2file
= NULL
, *jffs2dir
= JFFS2_DEFAULT_DIR
;
62 static int buflen
= 0;
67 int mtd_open(const char *mtd
, bool block
)
73 int flags
= O_RDWR
| O_SYNC
;
75 if ((fp
= fopen("/proc/mtd", "r"))) {
76 while (fgets(dev
, sizeof(dev
), fp
)) {
77 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
78 snprintf(dev
, sizeof(dev
), "/dev/mtd%s/%d", (block
? "block" : ""), i
);
79 if ((ret
=open(dev
, flags
))<0) {
80 snprintf(dev
, sizeof(dev
), "/dev/mtd%s%d", (block
? "block" : ""), i
);
90 return open(mtd
, flags
);
93 int mtd_check_open(const char *mtd
)
95 struct mtd_info_user mtdInfo
;
98 fd
= mtd_open(mtd
, false);
100 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
104 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
105 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
109 mtdsize
= mtdInfo
.size
;
110 erasesize
= mtdInfo
.erasesize
;
115 int mtd_erase_block(int fd
, int offset
)
117 struct erase_info_user mtdEraseInfo
;
119 mtdEraseInfo
.start
= offset
;
120 mtdEraseInfo
.length
= erasesize
;
121 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
122 if (ioctl (fd
, MEMERASE
, &mtdEraseInfo
) < 0)
128 int mtd_write_buffer(int fd
, const char *buf
, int offset
, int length
)
130 lseek(fd
, offset
, SEEK_SET
);
131 write(fd
, buf
, length
);
137 image_check(int imagefd
, const char *mtd
)
141 ret
= trx_check(imagefd
, mtd
, buf
, &buflen
);
146 static int mtd_check(const char *mtd
)
152 if (strchr(mtd
, ':')) {
158 next
= strchr(mtd
, ':');
164 fd
= mtd_check_open(mtd
);
169 buf
= malloc(erasesize
);
182 mtd_unlock(const char *mtd
)
184 struct erase_info_user mtdLockInfo
;
189 if (strchr(mtd
, ':')) {
195 next
= strchr(mtd
, ':');
201 fd
= mtd_check_open(mtd
);
203 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
208 fprintf(stderr
, "Unlocking %s ...\n", mtd
);
210 mtdLockInfo
.start
= 0;
211 mtdLockInfo
.length
= mtdsize
;
212 ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
);
224 mtd_erase(const char *mtd
)
227 struct erase_info_user mtdEraseInfo
;
230 fprintf(stderr
, "Erasing %s ...\n", mtd
);
232 fd
= mtd_check_open(mtd
);
234 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
238 mtdEraseInfo
.length
= erasesize
;
240 for (mtdEraseInfo
.start
= 0;
241 mtdEraseInfo
.start
< mtdsize
;
242 mtdEraseInfo
.start
+= erasesize
) {
244 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
245 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
))
246 fprintf(stderr
, "Failed to erase block on %s at 0x%x\n", mtd
, mtdEraseInfo
.start
);
255 mtd_refresh(const char *mtd
)
260 fprintf(stderr
, "Refreshing mtd partition %s ... ", mtd
);
262 fd
= mtd_check_open(mtd
);
264 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
268 if (ioctl(fd
, MTDREFRESH
, NULL
)) {
269 fprintf(stderr
, "Failed to refresh the MTD device\n");
276 fprintf(stderr
, "\n");
282 mtd_write(int imagefd
, const char *mtd
, char *fis_layout
)
291 static struct fis_part new_parts
[MAX_ARGS
];
292 static struct fis_part old_parts
[MAX_ARGS
];
293 int n_new
= 0, n_old
= 0;
296 const char *tmp
= mtd
;
300 memset(&old_parts
, 0, sizeof(old_parts
));
301 memset(&new_parts
, 0, sizeof(new_parts
));
304 next
= strchr(tmp
, ':');
306 next
= (char *) tmp
+ strlen(tmp
);
308 memcpy(old_parts
[n_old
].name
, tmp
, next
- tmp
);
314 for (word
= strtok_r(fis_layout
, ",", &brkt
);
316 word
= strtok_r(NULL
, ",", &brkt
)) {
318 tmp
= strtok(word
, ":");
319 strncpy((char *) new_parts
[n_new
].name
, tmp
, sizeof(new_parts
[n_new
].name
) - 1);
321 tmp
= strtok(NULL
, ":");
325 new_parts
[n_new
].size
= strtoul(tmp
, NULL
, 0);
327 tmp
= strtok(NULL
, ":");
331 new_parts
[n_new
].loadaddr
= strtoul(tmp
, NULL
, 16);
335 ret
= fis_validate(old_parts
, n_old
, new_parts
, n_new
);
337 fprintf(stderr
, "Failed to validate the new FIS partition table\n");
345 if (strchr(mtd
, ':')) {
353 next
= strchr(mtd
, ':');
359 fd
= mtd_check_open(mtd
);
361 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
366 fprintf(stderr
, "Writing from %s to %s ... ", imagefile
, mtd
);
370 fprintf(stderr
, " [ ]");
373 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
374 while (buflen
< erasesize
) {
375 r
= read(imagefd
, buf
+ buflen
, erasesize
- buflen
);
377 if ((errno
== EINTR
) || (errno
== EAGAIN
))
395 if (memcmp(buf
, JFFS2_EOF
, sizeof(JFFS2_EOF
) - 1) == 0) {
397 fprintf(stderr
, "\b\b\b ");
399 fprintf(stderr
, "\nAppending jffs2 data from %s to %s...", jffs2file
, mtd
);
400 /* got an EOF marker - this is the place to add some jffs2 data */
401 mtd_replace_jffs2(mtd
, fd
, e
, jffs2file
);
404 /* no EOF marker, make sure we figure out the last inode number
405 * before appending some data */
406 mtd_parse_jffs2data(buf
, jffs2dir
);
409 /* need to erase the next block before writing data to it */
410 while (w
+ buflen
> e
) {
412 fprintf(stderr
, "\b\b\b[e]");
415 if (mtd_erase_block(fd
, e
) < 0) {
418 write(fd
, buf
+ offset
, e
- w
);
425 fprintf(stderr
, "\b\b\b \n");
428 fprintf(stderr
, "Failed to erase block\n");
433 /* erase the chunk */
438 fprintf(stderr
, "\b\b\b[w]");
440 if ((result
= write(fd
, buf
+ offset
, buflen
)) < buflen
) {
442 fprintf(stderr
, "Error writing image.\n");
445 fprintf(stderr
, "Insufficient space.\n");
456 fprintf(stderr
, "\b\b\b\b ");
460 fprintf(stderr
, "\n");
464 if (fis_remap(old_parts
, n_old
, new_parts
, n_new
) < 0)
465 fprintf(stderr
, "Failed to update the FIS partition table\n");
473 static void usage(void)
475 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
476 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
477 "mtd recognizes these commands:\n"
478 " unlock unlock the device\n"
479 " refresh refresh mtd partition\n"
480 " erase erase all data on device\n"
481 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
482 " jffs2write <file> append <file> to the jffs2 partition on the device\n"
483 "Following options are available:\n"
484 " -q quiet mode (once: no [w] on writing,\n"
485 " twice: no status messages)\n"
486 " -r reboot after successful command\n"
487 " -f force write without trx checks\n"
488 " -e <device> erase <device> before executing the command\n"
489 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
490 " -j <name> integrate <file> into jffs2 data when writing an image\n"
492 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
493 " alter the fis partition table to create new partitions replacing\n"
494 " the partitions provided as argument to the write command\n"
495 " (only valid together with the write command)\n"
498 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
499 " mtd -r write linux.trx linux\n\n");
503 static void do_reboot(void)
505 fprintf(stderr
, "Rebooting ...\n");
508 /* try regular reboot method first */
509 system("/sbin/reboot");
512 /* if we're still alive at this point, force the kernel to reboot */
513 syscall(SYS_reboot
,LINUX_REBOOT_MAGIC1
,LINUX_REBOOT_MAGIC2
,LINUX_REBOOT_CMD_RESTART
,NULL
);
516 int main (int argc
, char **argv
)
518 int ch
, i
, boot
, imagefd
= 0, force
, unlocked
;
519 char *erase
[MAX_ARGS
], *device
= NULL
;
520 char *fis_layout
= NULL
;
535 while ((ch
= getopt(argc
, argv
,
555 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
579 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
582 } else if ((strcmp(argv
[0], "refresh") == 0) && (argc
== 2)) {
585 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
588 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
592 if (strcmp(argv
[1], "-") == 0) {
593 imagefile
= "<stdin>";
597 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
598 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
603 if (!mtd_check(device
)) {
604 fprintf(stderr
, "Can't open device for writing!\n");
607 /* check trx file before erasing or writing anything */
608 if (!image_check(imagefd
, device
) && !force
) {
609 fprintf(stderr
, "Image check failed.\n");
612 } else if ((strcmp(argv
[0], "jffs2write") == 0) && (argc
== 3)) {
613 cmd
= CMD_JFFS2WRITE
;
617 if (!mtd_check(device
)) {
618 fprintf(stderr
, "Can't open device for writing!\n");
629 while (erase
[i
] != NULL
) {
630 mtd_unlock(erase
[i
]);
632 if (strcmp(erase
[i
], device
) == 0)
650 mtd_write(imagefd
, device
, fis_layout
);
655 mtd_write_jffs2(device
, imagefile
, jffs2dir
);