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);
144 static int mtd_check(const char *mtd)
150 if (strchr(mtd, ':')) {
156 next = strchr(mtd, ':');
162 fd = mtd_check_open(mtd);
167 buf = malloc(erasesize);
180 mtd_unlock(const char *mtd)
182 struct erase_info_user mtdLockInfo;
187 if (strchr(mtd, ':')) {
193 next = strchr(mtd, ':');
199 fd = mtd_check_open(mtd);
201 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
206 fprintf(stderr, "Unlocking %s ...\n", mtd);
208 mtdLockInfo.start = 0;
209 mtdLockInfo.length = mtdsize;
210 ioctl(fd, MEMUNLOCK, &mtdLockInfo);
222 mtd_erase(const char *mtd)
225 struct erase_info_user mtdEraseInfo;
228 fprintf(stderr, "Erasing %s ...\n", mtd);
230 fd = mtd_check_open(mtd);
232 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
236 mtdEraseInfo.length = erasesize;
238 for (mtdEraseInfo.start = 0;
239 mtdEraseInfo.start < mtdsize;
240 mtdEraseInfo.start += erasesize) {
242 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
243 if(ioctl(fd, MEMERASE, &mtdEraseInfo))
244 fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
253 mtd_refresh(const char *mtd)
258 fprintf(stderr, "Refreshing mtd partition %s ... ", mtd);
260 fd = mtd_check_open(mtd);
262 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
266 if (ioctl(fd, MTDREFRESH, NULL)) {
267 fprintf(stderr, "Failed to refresh the MTD device\n");
274 fprintf(stderr, "\n");
280 indicate_writing(const char *mtd)
283 fprintf(stderr, "\nWriting from %s to %s ... ", imagefile, mtd);
286 fprintf(stderr, " [ ]");
290 mtd_write(int imagefd, const char *mtd, char *fis_layout, size_t part_offset)
298 int jffs2_replaced = 0;
301 static struct fis_part new_parts[MAX_ARGS];
302 static struct fis_part old_parts[MAX_ARGS];
303 int n_new = 0, n_old = 0;
306 const char *tmp = mtd;
310 memset(&old_parts, 0, sizeof(old_parts));
311 memset(&new_parts, 0, sizeof(new_parts));
314 next = strchr(tmp, ':');
316 next = (char *) tmp + strlen(tmp);
318 memcpy(old_parts[n_old].name, tmp, next - tmp);
324 for (word = strtok_r(fis_layout, ",", &brkt);
326 word = strtok_r(NULL, ",", &brkt)) {
328 tmp = strtok(word, ":");
329 strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
331 tmp = strtok(NULL, ":");
335 new_parts[n_new].size = strtoul(tmp, NULL, 0);
337 tmp = strtok(NULL, ":");
341 new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
345 ret = fis_validate(old_parts, n_old, new_parts, n_new);
347 fprintf(stderr, "Failed to validate the new FIS partition table\n");
355 if (strchr(mtd, ':')) {
363 next = strchr(mtd, ':');
369 fd = mtd_check_open(mtd);
371 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
375 if (part_offset > 0) {
376 fprintf(stderr, "Seeking on mtd device '%s' to: %u\n", mtd, part_offset);
377 lseek(fd, part_offset, SEEK_SET);
380 indicate_writing(mtd);
384 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
385 while (buflen < erasesize) {
386 r = read(imagefd, buf + buflen, erasesize - buflen);
388 if ((errno == EINTR) || (errno == EAGAIN))
409 indicate_writing(mtd);
415 if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
417 fprintf(stderr, "\b\b\b ");
419 fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtd);
420 /* got an EOF marker - this is the place to add some jffs2 data */
421 skip = mtd_replace_jffs2(mtd, fd, e, jffs2file);
424 /* don't add it again */
434 /* no EOF marker, make sure we figure out the last inode number
435 * before appending some data */
436 mtd_parse_jffs2data(buf, jffs2dir);
439 /* need to erase the next block before writing data to it */
442 while (w + buflen > e) {
444 fprintf(stderr, "\b\b\b[e]");
447 if (mtd_erase_block(fd, e) < 0) {
450 write(fd, buf + offset, e - w);
457 fprintf(stderr, "\b\b\b \n");
460 fprintf(stderr, "Failed to erase block\n");
465 /* erase the chunk */
471 fprintf(stderr, "\b\b\b[w]");
473 if ((result = write(fd, buf + offset, buflen)) < buflen) {
475 fprintf(stderr, "Error writing image.\n");
478 fprintf(stderr, "Insufficient space.\n");
488 if (jffs2_replaced && trx_fixup) {
493 fprintf(stderr, "\b\b\b\b ");
497 fprintf(stderr, "\n");
501 if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
502 fprintf(stderr, "Failed to update the FIS partition table\n");
510 static void usage(void)
512 fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
513 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
514 "mtd recognizes these commands:\n"
515 " unlock unlock the device\n"
516 " refresh refresh mtd partition\n"
517 " erase erase all data on device\n"
518 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
519 " jffs2write <file> append <file> to the jffs2 partition on the device\n");
522 " fixtrx fix the checksum in a trx header on first boot\n");
526 " fixseama fix the checksum in a seama header on first boot\n");
529 "Following options are available:\n"
530 " -q quiet mode (once: no [w] on writing,\n"
531 " twice: no status messages)\n"
532 " -n write without first erasing the blocks\n"
533 " -r reboot after successful command\n"
534 " -f force write without trx checks\n"
535 " -e <device> erase <device> before executing the command\n"
536 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
537 " -j <name> integrate <file> into jffs2 data when writing an image\n"
538 " -p write beginning at partition offset\n");
541 " -o offset offset of the image header in the partition(for fixtrx)\n");
545 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
546 " alter the fis partition table to create new partitions replacing\n"
547 " the partitions provided as argument to the write command\n"
548 " (only valid together with the write command)\n"
551 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
552 " mtd -r write linux.trx linux\n\n");
556 static void do_reboot(void)
558 fprintf(stderr, "Rebooting ...\n");
561 /* try regular reboot method first */
562 system("/sbin/reboot");
565 /* if we're still alive at this point, force the kernel to reboot */
566 syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
569 int main (int argc, char **argv)
571 int ch, i, boot, imagefd = 0, force, unlocked;
572 char *erase[MAX_ARGS], *device = NULL;
573 char *fis_layout = NULL;
574 size_t offset = 0, part_offset = 0;
592 while ((ch = getopt(argc, argv,
596 "frnqe:d:j:p:o:")) != -1)
615 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
626 part_offset = strtoul(optarg, 0, 0);
628 fprintf(stderr, "-p: illegal numeric string\n");
634 fprintf(stderr, "-o: is not available on this platform\n");
638 offset = strtoul(optarg, 0, 0);
640 fprintf(stderr, "-o: illegal numeric string\n");
659 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
662 } else if ((strcmp(argv[0], "refresh") == 0) && (argc == 2)) {
665 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
668 } else if (((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) && mtd_fixtrx) {
671 } else if (((strcmp(argv[0], "fixseama") == 0) && (argc == 2)) && mtd_fixseama) {
674 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
678 if (strcmp(argv[1], "-") == 0) {
679 imagefile = "<stdin>";
683 if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
684 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
689 if (!mtd_check(device)) {
690 fprintf(stderr, "Can't open device for writing!\n");
693 /* check trx file before erasing or writing anything */
694 if (!image_check(imagefd, device) && !force) {
695 fprintf(stderr, "Image check failed.\n");
698 } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
699 cmd = CMD_JFFS2WRITE;
703 if (!mtd_check(device)) {
704 fprintf(stderr, "Can't open device for writing!\n");
715 while (erase[i] != NULL) {
716 mtd_unlock(erase[i]);
718 if (strcmp(erase[i], device) == 0)
736 mtd_write(imagefd, device, fis_layout, part_offset);
741 mtd_write_jffs2(device, imagefile, jffs2dir);
748 mtd_fixtrx(device, offset);
752 mtd_fixseama(device, 0);