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 */
51 static char *buf = NULL;
52 static char *imagefile = NULL;
53 static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
54 static int buflen = 0;
60 int mtd_open(const char *mtd, bool block)
66 int flags = O_RDWR | O_SYNC;
68 if ((fp = fopen("/proc/mtd", "r"))) {
69 while (fgets(dev, sizeof(dev), fp)) {
70 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
71 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
72 if ((ret=open(dev, flags))<0) {
73 snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
83 return open(mtd, flags);
86 int mtd_check_open(const char *mtd)
88 struct mtd_info_user mtdInfo;
91 fd = mtd_open(mtd, false);
93 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
97 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
98 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
102 mtdsize = mtdInfo.size;
103 erasesize = mtdInfo.erasesize;
108 int mtd_erase_block(int fd, int offset)
110 struct erase_info_user mtdEraseInfo;
112 mtdEraseInfo.start = offset;
113 mtdEraseInfo.length = erasesize;
114 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
115 if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
121 int mtd_write_buffer(int fd, const char *buf, int offset, int length)
123 lseek(fd, offset, SEEK_SET);
124 write(fd, buf, length);
130 image_check(int imagefd, const char *mtd)
134 ret = trx_check(imagefd, mtd, buf, &buflen);
139 static int mtd_check(const char *mtd)
145 if (strchr(mtd, ':')) {
151 next = strchr(mtd, ':');
157 fd = mtd_check_open(mtd);
162 buf = malloc(erasesize);
175 mtd_unlock(const char *mtd)
177 struct erase_info_user mtdLockInfo;
182 if (strchr(mtd, ':')) {
188 next = strchr(mtd, ':');
194 fd = mtd_check_open(mtd);
196 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
201 fprintf(stderr, "Unlocking %s ...\n", mtd);
203 mtdLockInfo.start = 0;
204 mtdLockInfo.length = mtdsize;
205 ioctl(fd, MEMUNLOCK, &mtdLockInfo);
217 mtd_erase(const char *mtd)
220 struct erase_info_user mtdEraseInfo;
223 fprintf(stderr, "Erasing %s ...\n", mtd);
225 fd = mtd_check_open(mtd);
227 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
231 mtdEraseInfo.length = erasesize;
233 for (mtdEraseInfo.start = 0;
234 mtdEraseInfo.start < mtdsize;
235 mtdEraseInfo.start += erasesize) {
237 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
238 if(ioctl(fd, MEMERASE, &mtdEraseInfo))
239 fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
248 mtd_refresh(const char *mtd)
253 fprintf(stderr, "Refreshing mtd partition %s ... ", mtd);
255 fd = mtd_check_open(mtd);
257 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
261 if (ioctl(fd, MTDREFRESH, NULL)) {
262 fprintf(stderr, "Failed to refresh the MTD device\n");
269 fprintf(stderr, "\n");
275 indicate_writing(const char *mtd)
278 fprintf(stderr, "\nWriting from %s to %s ... ", imagefile, mtd);
281 fprintf(stderr, " [ ]");
285 mtd_write(int imagefd, const char *mtd, char *fis_layout)
295 static struct fis_part new_parts[MAX_ARGS];
296 static struct fis_part old_parts[MAX_ARGS];
297 int n_new = 0, n_old = 0;
300 const char *tmp = mtd;
304 memset(&old_parts, 0, sizeof(old_parts));
305 memset(&new_parts, 0, sizeof(new_parts));
308 next = strchr(tmp, ':');
310 next = (char *) tmp + strlen(tmp);
312 memcpy(old_parts[n_old].name, tmp, next - tmp);
318 for (word = strtok_r(fis_layout, ",", &brkt);
320 word = strtok_r(NULL, ",", &brkt)) {
322 tmp = strtok(word, ":");
323 strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
325 tmp = strtok(NULL, ":");
329 new_parts[n_new].size = strtoul(tmp, NULL, 0);
331 tmp = strtok(NULL, ":");
335 new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
339 ret = fis_validate(old_parts, n_old, new_parts, n_new);
341 fprintf(stderr, "Failed to validate the new FIS partition table\n");
349 if (strchr(mtd, ':')) {
357 next = strchr(mtd, ':');
363 fd = mtd_check_open(mtd);
365 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
369 indicate_writing(mtd);
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))
398 indicate_writing(mtd);
404 if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
406 fprintf(stderr, "\b\b\b ");
408 fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtd);
409 /* got an EOF marker - this is the place to add some jffs2 data */
410 skip = mtd_replace_jffs2(mtd, fd, e, jffs2file);
419 /* no EOF marker, make sure we figure out the last inode number
420 * before appending some data */
421 mtd_parse_jffs2data(buf, jffs2dir);
424 /* need to erase the next block before writing data to it */
427 while (w + buflen > e) {
429 fprintf(stderr, "\b\b\b[e]");
432 if (mtd_erase_block(fd, e) < 0) {
435 write(fd, buf + offset, e - w);
442 fprintf(stderr, "\b\b\b \n");
445 fprintf(stderr, "Failed to erase block\n");
450 /* erase the chunk */
456 fprintf(stderr, "\b\b\b[w]");
458 if ((result = write(fd, buf + offset, buflen)) < buflen) {
460 fprintf(stderr, "Error writing image.\n");
463 fprintf(stderr, "Insufficient space.\n");
474 fprintf(stderr, "\b\b\b\b ");
478 fprintf(stderr, "\n");
482 if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
483 fprintf(stderr, "Failed to update the FIS partition table\n");
491 static void usage(void)
493 fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
494 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
495 "mtd recognizes these commands:\n"
496 " unlock unlock the device\n"
497 " refresh refresh mtd partition\n"
498 " erase erase all data on device\n"
499 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
500 " jffs2write <file> append <file> to the jffs2 partition on the device\n");
503 " fixtrx fix the checksum in a trx header on first boot\n");
506 "Following options are available:\n"
507 " -q quiet mode (once: no [w] on writing,\n"
508 " twice: no status messages)\n"
509 " -n write without first erasing the blocks\n"
510 " -r reboot after successful command\n"
511 " -f force write without trx checks\n"
512 " -e <device> erase <device> before executing the command\n"
513 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
514 " -j <name> integrate <file> into jffs2 data when writing an image\n");
517 " -o offset offset of the image header in the partition(for fixtrx)\n");
521 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
522 " alter the fis partition table to create new partitions replacing\n"
523 " the partitions provided as argument to the write command\n"
524 " (only valid together with the write command)\n"
527 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
528 " mtd -r write linux.trx linux\n\n");
532 static void do_reboot(void)
534 fprintf(stderr, "Rebooting ...\n");
537 /* try regular reboot method first */
538 system("/sbin/reboot");
541 /* if we're still alive at this point, force the kernel to reboot */
542 syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
545 int main (int argc, char **argv)
547 int ch, i, boot, imagefd = 0, force, unlocked;
548 char *erase[MAX_ARGS], *device = NULL;
549 char *fis_layout = NULL;
567 while ((ch = getopt(argc, argv,
571 "frnqe:d:j:o:")) != -1)
590 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
601 fprintf(stderr, "-o: Only for brcm47xx/brcm63xx\n");
605 offset = strtoul(optarg, 0, 0);
607 fprintf(stderr, "-o: illegal numeric string\n");
626 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
629 } else if ((strcmp(argv[0], "refresh") == 0) && (argc == 2)) {
632 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
635 } else if (((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) && mtd_fixtrx) {
638 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
642 if (strcmp(argv[1], "-") == 0) {
643 imagefile = "<stdin>";
647 if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
648 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
653 if (!mtd_check(device)) {
654 fprintf(stderr, "Can't open device for writing!\n");
657 /* check trx file before erasing or writing anything */
658 if (!image_check(imagefd, device) && !force) {
659 fprintf(stderr, "Image check failed.\n");
662 } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
663 cmd = CMD_JFFS2WRITE;
667 if (!mtd_check(device)) {
668 fprintf(stderr, "Can't open device for writing!\n");
679 while (erase[i] != NULL) {
680 mtd_unlock(erase[i]);
682 if (strcmp(erase[i], device) == 0)
700 mtd_write(imagefd, device, fis_layout);
705 mtd_write_jffs2(device, imagefile, jffs2dir);
712 mtd_fixtrx(device, offset);