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>
48 #define MTDREFRESH _IO('M', 50)
52 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
54 static char *buf = NULL;
55 static char *imagefile = NULL;
56 static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
57 static int buflen = 0;
63 int mtd_open(const char *mtd, bool block)
69 int flags = O_RDWR | O_SYNC;
71 if ((fp = fopen("/proc/mtd", "r"))) {
72 while (fgets(dev, sizeof(dev), fp)) {
73 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
74 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
75 if ((ret=open(dev, flags))<0) {
76 snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
86 return open(mtd, flags);
89 int mtd_check_open(const char *mtd)
91 struct mtd_info_user mtdInfo;
94 fd = mtd_open(mtd, false);
96 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
100 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
101 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
105 mtdsize = mtdInfo.size;
106 erasesize = mtdInfo.erasesize;
111 int mtd_erase_block(int fd, int offset)
113 struct erase_info_user mtdEraseInfo;
115 mtdEraseInfo.start = offset;
116 mtdEraseInfo.length = erasesize;
117 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
118 if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
124 int mtd_write_buffer(int fd, const char *buf, int offset, int length)
126 lseek(fd, offset, SEEK_SET);
127 write(fd, buf, length);
133 image_check(int imagefd, const char *mtd)
137 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, size_t part_offset)
297 int jffs2_replaced = 0;
300 static struct fis_part new_parts[MAX_ARGS];
301 static struct fis_part old_parts[MAX_ARGS];
302 int n_new = 0, n_old = 0;
305 const char *tmp = mtd;
309 memset(&old_parts, 0, sizeof(old_parts));
310 memset(&new_parts, 0, sizeof(new_parts));
313 next = strchr(tmp, ':');
315 next = (char *) tmp + strlen(tmp);
317 memcpy(old_parts[n_old].name, tmp, next - tmp);
323 for (word = strtok_r(fis_layout, ",", &brkt);
325 word = strtok_r(NULL, ",", &brkt)) {
327 tmp = strtok(word, ":");
328 strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
330 tmp = strtok(NULL, ":");
334 new_parts[n_new].size = strtoul(tmp, NULL, 0);
336 tmp = strtok(NULL, ":");
340 new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
344 ret = fis_validate(old_parts, n_old, new_parts, n_new);
346 fprintf(stderr, "Failed to validate the new FIS partition table\n");
354 if (strchr(mtd, ':')) {
362 next = strchr(mtd, ':');
368 fd = mtd_check_open(mtd);
370 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
374 if (part_offset > 0) {
375 fprintf(stderr, "Seeking on mtd device '%s' to: %u\n", mtd, part_offset);
376 lseek(fd, part_offset, SEEK_SET);
379 indicate_writing(mtd);
383 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
384 while (buflen < erasesize) {
385 r = read(imagefd, buf + buflen, erasesize - buflen);
387 if ((errno == EINTR) || (errno == EAGAIN))
408 indicate_writing(mtd);
414 if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
416 fprintf(stderr, "\b\b\b ");
418 fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtd);
419 /* got an EOF marker - this is the place to add some jffs2 data */
420 skip = mtd_replace_jffs2(mtd, fd, e, jffs2file);
423 /* don't add it again */
433 /* no EOF marker, make sure we figure out the last inode number
434 * before appending some data */
435 mtd_parse_jffs2data(buf, jffs2dir);
438 /* need to erase the next block before writing data to it */
441 while (w + buflen > e) {
443 fprintf(stderr, "\b\b\b[e]");
446 if (mtd_erase_block(fd, e) < 0) {
449 write(fd, buf + offset, e - w);
456 fprintf(stderr, "\b\b\b \n");
459 fprintf(stderr, "Failed to erase block\n");
464 /* erase the chunk */
470 fprintf(stderr, "\b\b\b[w]");
472 if ((result = write(fd, buf + offset, buflen)) < buflen) {
474 fprintf(stderr, "Error writing image.\n");
477 fprintf(stderr, "Insufficient space.\n");
487 if (jffs2_replaced && trx_fixup) {
492 fprintf(stderr, "\b\b\b\b ");
496 fprintf(stderr, "\n");
500 if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
501 fprintf(stderr, "Failed to update the FIS partition table\n");
509 static void usage(void)
511 fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
512 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
513 "mtd recognizes these commands:\n"
514 " unlock unlock the device\n"
515 " refresh refresh mtd partition\n"
516 " erase erase all data on device\n"
517 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
518 " jffs2write <file> append <file> to the jffs2 partition on the device\n");
521 " fixtrx fix the checksum in a trx header on first boot\n");
525 " fixseama fix the checksum in a seama header on first boot\n");
528 "Following options are available:\n"
529 " -q quiet mode (once: no [w] on writing,\n"
530 " twice: no status messages)\n"
531 " -n write without first erasing the blocks\n"
532 " -r reboot after successful command\n"
533 " -f force write without trx checks\n"
534 " -e <device> erase <device> before executing the command\n"
535 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
536 " -j <name> integrate <file> into jffs2 data when writing an image\n"
537 " -p write beginning at partition offset\n");
540 " -o offset offset of the image header in the partition(for fixtrx)\n");
544 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
545 " alter the fis partition table to create new partitions replacing\n"
546 " the partitions provided as argument to the write command\n"
547 " (only valid together with the write command)\n"
550 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
551 " mtd -r write linux.trx linux\n\n");
555 static void do_reboot(void)
557 fprintf(stderr, "Rebooting ...\n");
560 /* try regular reboot method first */
561 system("/sbin/reboot");
564 /* if we're still alive at this point, force the kernel to reboot */
565 syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
568 int main (int argc, char **argv)
570 int ch, i, boot, imagefd = 0, force, unlocked;
571 char *erase[MAX_ARGS], *device = NULL;
572 char *fis_layout = NULL;
573 size_t offset = 0, part_offset = 0;
591 while ((ch = getopt(argc, argv,
595 "frnqe:d:j:p:o:")) != -1)
614 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
625 part_offset = strtoul(optarg, 0, 0);
627 fprintf(stderr, "-p: illegal numeric string\n");
633 fprintf(stderr, "-o: is not available on this platform\n");
637 offset = strtoul(optarg, 0, 0);
639 fprintf(stderr, "-o: illegal numeric string\n");
658 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
661 } else if ((strcmp(argv[0], "refresh") == 0) && (argc == 2)) {
664 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
667 } else if (((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) && mtd_fixtrx) {
670 } else if (((strcmp(argv[0], "fixseama") == 0) && (argc == 2)) && mtd_fixseama) {
673 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
677 if (strcmp(argv[1], "-") == 0) {
678 imagefile = "<stdin>";
682 if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
683 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
688 if (!mtd_check(device)) {
689 fprintf(stderr, "Can't open device for writing!\n");
692 /* check trx file before erasing or writing anything */
693 if (!image_check(imagefd, device) && !force) {
694 fprintf(stderr, "Image check failed.\n");
697 } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
698 cmd = CMD_JFFS2WRITE;
702 if (!mtd_check(device)) {
703 fprintf(stderr, "Can't open device for writing!\n");
714 while (erase[i] != NULL) {
715 mtd_unlock(erase[i]);
717 if (strcmp(erase[i], device) == 0)
735 mtd_write(imagefd, device, fis_layout, part_offset);
740 mtd_write_jffs2(device, imagefile, jffs2dir);
747 mtd_fixtrx(device, offset);
751 mtd_fixseama(device, 0);