2 * mtd - simple memory technology device manipulation tool
4 * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>,
5 * 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
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The code is based on the linux-mtd examples.
31 #include <sys/ioctl.h>
32 #include <sys/syscall.h>
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/mount.h>
43 #include <sys/reboot.h>
44 #include <linux/reboot.h>
50 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
53 uint32_t magic; /* "HDR0" */
54 uint32_t len; /* Length of file including header */
55 uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
56 uint32_t flag_version; /* 0:15 flags, 16:31 version */
57 uint32_t offsets[3]; /* Offsets of partitions from start of header */
60 static char *buf = NULL;
61 static char *imagefile = NULL;
62 static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
63 static int buflen = 0;
68 int mtd_open(const char *mtd, bool block)
74 int flags = O_RDWR | O_SYNC;
76 if ((fp = fopen("/proc/mtd", "r"))) {
77 while (fgets(dev, sizeof(dev), fp)) {
78 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
79 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
80 if ((ret=open(dev, flags))<0) {
81 snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
91 return open(mtd, flags);
94 int mtd_check_open(const char *mtd)
96 struct mtd_info_user mtdInfo;
99 fd = mtd_open(mtd, false);
101 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
105 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
106 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
110 mtdsize = mtdInfo.size;
111 erasesize = mtdInfo.erasesize;
116 int mtd_erase_block(int fd, int offset)
118 struct erase_info_user mtdEraseInfo;
120 mtdEraseInfo.start = offset;
121 mtdEraseInfo.length = erasesize;
122 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
123 if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
129 int mtd_write_buffer(int fd, const char *buf, int offset, int length)
131 lseek(fd, offset, SEEK_SET);
132 write(fd, buf, length);
138 image_check(int imagefd, const char *mtd)
142 ret = trx_check(imagefd, mtd, buf, &buflen);
147 static int mtd_check(const char *mtd)
153 if (strchr(mtd, ':')) {
159 next = strchr(mtd, ':');
165 fd = mtd_check_open(mtd);
170 buf = malloc(erasesize);
183 mtd_unlock(const char *mtd)
185 struct erase_info_user mtdLockInfo;
190 if (strchr(mtd, ':')) {
196 next = strchr(mtd, ':');
202 fd = mtd_check_open(mtd);
204 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
209 fprintf(stderr, "Unlocking %s ...\n", mtd);
211 mtdLockInfo.start = 0;
212 mtdLockInfo.length = mtdsize;
213 ioctl(fd, MEMUNLOCK, &mtdLockInfo);
225 mtd_erase(const char *mtd)
228 struct erase_info_user mtdEraseInfo;
231 fprintf(stderr, "Erasing %s ...\n", mtd);
233 fd = mtd_check_open(mtd);
235 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
239 mtdEraseInfo.length = erasesize;
241 for (mtdEraseInfo.start = 0;
242 mtdEraseInfo.start < mtdsize;
243 mtdEraseInfo.start += erasesize) {
245 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
246 if(ioctl(fd, MEMERASE, &mtdEraseInfo))
247 fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
256 mtd_refresh(const char *mtd)
261 fprintf(stderr, "Refreshing mtd partition %s ... ", mtd);
263 fd = mtd_check_open(mtd);
265 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
269 if (ioctl(fd, MTDREFRESH, NULL)) {
270 fprintf(stderr, "Failed to refresh the MTD device\n");
277 fprintf(stderr, "\n");
283 mtd_write(int imagefd, const char *mtd, char *fis_layout)
292 static struct fis_part new_parts[MAX_ARGS];
293 static struct fis_part old_parts[MAX_ARGS];
294 int n_new = 0, n_old = 0;
297 const char *tmp = mtd;
301 memset(&old_parts, 0, sizeof(old_parts));
302 memset(&new_parts, 0, sizeof(new_parts));
305 next = strchr(tmp, ':');
307 next = (char *) tmp + strlen(tmp);
309 memcpy(old_parts[n_old].name, tmp, next - tmp);
315 for (word = strtok_r(fis_layout, ",", &brkt);
317 word = strtok_r(NULL, ",", &brkt)) {
319 tmp = strtok(word, ":");
320 strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
322 tmp = strtok(NULL, ":");
326 new_parts[n_new].size = strtoul(tmp, NULL, 0);
328 tmp = strtok(NULL, ":");
332 new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
336 ret = fis_validate(old_parts, n_old, new_parts, n_new);
338 fprintf(stderr, "Failed to validate the new FIS partition table\n");
346 if (strchr(mtd, ':')) {
354 next = strchr(mtd, ':');
360 fd = mtd_check_open(mtd);
362 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
367 fprintf(stderr, "Writing from %s to %s ... ", imagefile, mtd);
371 fprintf(stderr, " [ ]");
374 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
375 while (buflen < erasesize) {
376 r = read(imagefd, buf + buflen, erasesize - buflen);
378 if ((errno == EINTR) || (errno == EAGAIN))
396 if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
398 fprintf(stderr, "\b\b\b ");
400 fprintf(stderr, "\nAppending jffs2 data to from %s to %s...", jffs2file, mtd);
401 /* got an EOF marker - this is the place to add some jffs2 data */
402 mtd_replace_jffs2(mtd, fd, e, jffs2file);
405 /* no EOF marker, make sure we figure out the last inode number
406 * before appending some data */
407 mtd_parse_jffs2data(buf, jffs2dir);
410 /* need to erase the next block before writing data to it */
411 while (w + buflen > e) {
413 fprintf(stderr, "\b\b\b[e]");
416 if (mtd_erase_block(fd, e) < 0) {
419 write(fd, buf + offset, e - w);
426 fprintf(stderr, "\b\b\b \n");
429 fprintf(stderr, "Failed to erase block\n");
434 /* erase the chunk */
439 fprintf(stderr, "\b\b\b[w]");
441 if ((result = write(fd, buf + offset, buflen)) < buflen) {
443 fprintf(stderr, "Error writing image.\n");
446 fprintf(stderr, "Insufficient space.\n");
457 fprintf(stderr, "\b\b\b\b ");
461 fprintf(stderr, "\n");
465 if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
466 fprintf(stderr, "Failed to update the FIS partition table\n");
474 static void usage(void)
476 fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
477 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
478 "mtd recognizes these commands:\n"
479 " unlock unlock the device\n"
480 " refresh refresh mtd partition\n"
481 " erase erase all data on device\n"
482 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
483 " jffs2write <file> append <file> to the jffs2 partition on the device\n"
484 "Following options are available:\n"
485 " -q quiet mode (once: no [w] on writing,\n"
486 " twice: no status messages)\n"
487 " -r reboot after successful command\n"
488 " -f force write without trx checks\n"
489 " -e <device> erase <device> before executing the command\n"
490 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
491 " -j <name> integrate <file> into jffs2 data when writing an image\n"
493 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
494 " alter the fis partition table to create new partitions replacing\n"
495 " the partitions provided as argument to the write command\n"
496 " (only valid together with the write command)\n"
499 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
500 " mtd -r write linux.trx linux\n\n");
504 static void do_reboot(void)
506 fprintf(stderr, "Rebooting ...\n");
509 /* try regular reboot method first */
510 system("/sbin/reboot");
513 /* if we're still alive at this point, force the kernel to reboot */
514 syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
517 int main (int argc, char **argv)
519 int ch, i, boot, imagefd = 0, force, unlocked;
520 char *erase[MAX_ARGS], *device = NULL;
521 char *fis_layout = NULL;
536 while ((ch = getopt(argc, argv,
556 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
580 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
583 } else if ((strcmp(argv[0], "refresh") == 0) && (argc == 2)) {
586 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
589 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
593 if (strcmp(argv[1], "-") == 0) {
594 imagefile = "<stdin>";
598 if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
599 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
604 if (!mtd_check(device)) {
605 fprintf(stderr, "Can't open device for writing!\n");
608 /* check trx file before erasing or writing anything */
609 if (!image_check(imagefd, device) && !force) {
610 fprintf(stderr, "Image check failed.\n");
613 } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
614 cmd = CMD_JFFS2WRITE;
618 if (!mtd_check(device)) {
619 fprintf(stderr, "Can't open device for writing!\n");
630 while (erase[i] != NULL) {
631 mtd_unlock(erase[i]);
633 if (strcmp(erase[i], device) == 0)
651 mtd_write(imagefd, device, fis_layout);
656 mtd_write_jffs2(device, imagefile, jffs2dir);