mtd: add ability to dump a mtd device
[openwrt.git] / package / system / mtd / src / mtd.c
1 /*
2  * mtd - simple memory technology device manipulation tool
3  *
4  * Copyright (C) 2005      Waldemar Brodkorb <wbx@dass-it.de>,
5  * Copyright (C) 2005-2009 Felix Fietkau <nbd@openwrt.org>
6  *
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.
10  *
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.
15  *
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.
19  *
20  *
21  * The code is based on the linux-mtd examples.
22  */
23
24 #define _GNU_SOURCE
25 #include <limits.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <signal.h>
31 #include <sys/ioctl.h>
32 #include <sys/syscall.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <time.h>
36 #include <string.h>
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/mount.h>
41 #include <sys/stat.h>
42 #include <sys/reboot.h>
43 #include <linux/reboot.h>
44 #include <mtd/mtd-user.h>
45 #include "fis.h"
46 #include "mtd.h"
47
48 #include <libubox/md5.h>
49
50 #define MAX_ARGS 8
51 #define JFFS2_DEFAULT_DIR       "" /* directory name without /, empty means root dir */
52
53 static char *buf = NULL;
54 static char *imagefile = NULL;
55 static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
56 static int buflen = 0;
57 int quiet;
58 int no_erase;
59 int mtdsize = 0;
60 int erasesize = 0;
61 int jffs2_skip_bytes=0;
62 int mtdtype = 0;
63
64 int mtd_open(const char *mtd, bool block)
65 {
66         FILE *fp;
67         char dev[PATH_MAX];
68         int i;
69         int ret;
70         int flags = O_RDWR | O_SYNC;
71         char name[PATH_MAX];
72
73         snprintf(name, sizeof(name), "\"%s\"", mtd);
74         if ((fp = fopen("/proc/mtd", "r"))) {
75                 while (fgets(dev, sizeof(dev), fp)) {
76                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, name)) {
77                                 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
78                                 if ((ret=open(dev, flags))<0) {
79                                         snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
80                                         ret=open(dev, flags);
81                                 }
82                                 fclose(fp);
83                                 return ret;
84                         }
85                 }
86                 fclose(fp);
87         }
88
89         return open(mtd, flags);
90 }
91
92 int mtd_check_open(const char *mtd)
93 {
94         struct mtd_info_user mtdInfo;
95         int fd;
96
97         fd = mtd_open(mtd, false);
98         if(fd < 0) {
99                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
100                 return -1;
101         }
102
103         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
104                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
105                 close(fd);
106                 return -1;
107         }
108         mtdsize = mtdInfo.size;
109         erasesize = mtdInfo.erasesize;
110         mtdtype = mtdInfo.type;
111
112         return fd;
113 }
114
115 int mtd_block_is_bad(int fd, int offset)
116 {
117         int r = 0;
118         loff_t o = offset;
119
120         if (mtdtype == MTD_NANDFLASH)
121         {
122                 r = ioctl(fd, MEMGETBADBLOCK, &o);
123                 if (r < 0)
124                 {
125                         fprintf(stderr, "Failed to get erase block status\n");
126                         exit(1);
127                 }
128         }
129         return r;
130 }
131
132 int mtd_erase_block(int fd, int offset)
133 {
134         struct erase_info_user mtdEraseInfo;
135
136         mtdEraseInfo.start = offset;
137         mtdEraseInfo.length = erasesize;
138         ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
139         if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
140                 return -1;
141
142         return 0;
143 }
144
145 int mtd_write_buffer(int fd, const char *buf, int offset, int length)
146 {
147         lseek(fd, offset, SEEK_SET);
148         write(fd, buf, length);
149         return 0;
150 }
151
152
153 static int
154 image_check(int imagefd, const char *mtd)
155 {
156         int ret = 1;
157         if (trx_check) {
158           ret = trx_check(imagefd, mtd, buf, &buflen);
159         }
160
161         return ret;
162 }
163
164 static int mtd_check(const char *mtd)
165 {
166         char *next = NULL;
167         char *str = NULL;
168         int fd;
169
170         if (strchr(mtd, ':')) {
171                 str = strdup(mtd);
172                 mtd = str;
173         }
174
175         do {
176                 next = strchr(mtd, ':');
177                 if (next) {
178                         *next = 0;
179                         next++;
180                 }
181
182                 fd = mtd_check_open(mtd);
183                 if (fd < 0)
184                         return 0;
185
186                 if (!buf)
187                         buf = malloc(erasesize);
188
189                 close(fd);
190                 mtd = next;
191         } while (next);
192
193         if (str)
194                 free(str);
195
196         return 1;
197 }
198
199 static int
200 mtd_unlock(const char *mtd)
201 {
202         struct erase_info_user mtdLockInfo;
203         char *next = NULL;
204         char *str = NULL;
205         int fd;
206
207         if (strchr(mtd, ':')) {
208                 str = strdup(mtd);
209                 mtd = str;
210         }
211
212         do {
213                 next = strchr(mtd, ':');
214                 if (next) {
215                         *next = 0;
216                         next++;
217                 }
218
219                 fd = mtd_check_open(mtd);
220                 if(fd < 0) {
221                         fprintf(stderr, "Could not open mtd device: %s\n", mtd);
222                         exit(1);
223                 }
224
225                 if (quiet < 2)
226                         fprintf(stderr, "Unlocking %s ...\n", mtd);
227
228                 mtdLockInfo.start = 0;
229                 mtdLockInfo.length = mtdsize;
230                 ioctl(fd, MEMUNLOCK, &mtdLockInfo);
231                 close(fd);
232                 mtd = next;
233         } while (next);
234
235         if (str)
236                 free(str);
237
238         return 0;
239 }
240
241 static int
242 mtd_erase(const char *mtd)
243 {
244         int fd;
245         struct erase_info_user mtdEraseInfo;
246
247         if (quiet < 2)
248                 fprintf(stderr, "Erasing %s ...\n", mtd);
249
250         fd = mtd_check_open(mtd);
251         if(fd < 0) {
252                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
253                 exit(1);
254         }
255
256         mtdEraseInfo.length = erasesize;
257
258         for (mtdEraseInfo.start = 0;
259                  mtdEraseInfo.start < mtdsize;
260                  mtdEraseInfo.start += erasesize) {
261                 if (mtd_block_is_bad(fd, mtdEraseInfo.start)) {
262                         if (!quiet)
263                                 fprintf(stderr, "\nSkipping bad block at 0x%x   ", mtdEraseInfo.start);
264                 } else {
265                         ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
266                         if(ioctl(fd, MEMERASE, &mtdEraseInfo))
267                                 fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
268                 }
269         }
270
271         close(fd);
272         return 0;
273
274 }
275
276 static int
277 mtd_dump(const char *mtd, int size)
278 {
279         int ret = 0;
280         int fd;
281
282         if (quiet < 2)
283                 fprintf(stderr, "Dumping %s ...\n", mtd);
284
285         fd = mtd_check_open(mtd);
286         if(fd < 0) {
287                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
288                 return -1;
289         }
290
291         do {
292                 char buf[256];
293                 int len = (size > sizeof(buf)) ? (sizeof(buf)) : (size);
294                 int rlen = read(fd, buf, len);
295
296                 if (rlen < 0) {
297                         if (errno == EINTR)
298                                 continue;
299                         ret = -1;
300                         goto out;
301                 }
302                 if (!rlen)
303                         break;
304                 write(1, buf, rlen);
305         } while (size > 0);
306
307 out:
308         close(fd);
309         return ret;
310 }
311
312 static int
313 mtd_verify(const char *mtd, char *file)
314 {
315         uint32_t f_md5[4], m_md5[4];
316         struct stat s;
317         md5_ctx_t ctx;
318         int ret = 0;
319         int fd;
320
321         if (quiet < 2)
322                 fprintf(stderr, "Verifying %s against %s ...\n", mtd, file);
323
324         if (stat(file, &s) || md5sum(file, f_md5)) {
325                 fprintf(stderr, "Failed to hash %s\n", file);
326                 return -1;
327         }
328
329         fd = mtd_check_open(mtd);
330         if(fd < 0) {
331                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
332                 return -1;
333         }
334
335         md5_begin(&ctx);
336         do {
337                 char buf[256];
338                 int len = (s.st_size > sizeof(buf)) ? (sizeof(buf)) : (s.st_size);
339                 int rlen = read(fd, buf, len);
340
341                 if (rlen < 0) {
342                         if (errno == EINTR)
343                                 continue;
344                         ret = -1;
345                         goto out;
346                 }
347                 if (!rlen)
348                         break;
349                 md5_hash(buf, rlen, &ctx);
350                 s.st_size -= rlen;
351         } while (s.st_size > 0);
352
353         md5_end(m_md5, &ctx);
354
355         fprintf(stderr, "%08x%08x%08x%08x - %s\n", m_md5[0], m_md5[1], m_md5[2], m_md5[3], mtd);
356         fprintf(stderr, "%08x%08x%08x%08x - %s\n", f_md5[0], f_md5[1], f_md5[2], f_md5[3], file);
357
358         ret = memcmp(f_md5, m_md5, sizeof(m_md5));
359         if (!ret)
360                 fprintf(stderr, "Success\n");
361         else
362                 fprintf(stderr, "Failed\n");
363
364 out:
365         close(fd);
366         return ret;
367 }
368
369 static void
370 indicate_writing(const char *mtd)
371 {
372         if (quiet < 2)
373                 fprintf(stderr, "\nWriting from %s to %s ... ", imagefile, mtd);
374
375         if (!quiet)
376                 fprintf(stderr, " [ ]");
377 }
378
379 static int
380 mtd_write(int imagefd, const char *mtd, char *fis_layout, size_t part_offset)
381 {
382         char *next = NULL;
383         char *str = NULL;
384         int fd, result;
385         ssize_t r, w, e;
386         ssize_t skip = 0;
387         uint32_t offset = 0;
388         int jffs2_replaced = 0;
389         int skip_bad_blocks = 0;
390
391 #ifdef FIS_SUPPORT
392         static struct fis_part new_parts[MAX_ARGS];
393         static struct fis_part old_parts[MAX_ARGS];
394         int n_new = 0, n_old = 0;
395
396         if (fis_layout) {
397                 const char *tmp = mtd;
398                 char *word, *brkt;
399                 int ret;
400
401                 memset(&old_parts, 0, sizeof(old_parts));
402                 memset(&new_parts, 0, sizeof(new_parts));
403
404                 do {
405                         next = strchr(tmp, ':');
406                         if (!next)
407                                 next = (char *) tmp + strlen(tmp);
408
409                         memcpy(old_parts[n_old].name, tmp, next - tmp);
410
411                         n_old++;
412                         tmp = next + 1;
413                 } while(*next);
414
415                 for (word = strtok_r(fis_layout, ",", &brkt);
416                      word;
417                          word = strtok_r(NULL, ",", &brkt)) {
418
419                         tmp = strtok(word, ":");
420                         strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
421
422                         tmp = strtok(NULL, ":");
423                         if (!tmp)
424                                 goto next;
425
426                         new_parts[n_new].size = strtoul(tmp, NULL, 0);
427
428                         tmp = strtok(NULL, ":");
429                         if (!tmp)
430                                 goto next;
431
432                         new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
433 next:
434                         n_new++;
435                 }
436                 ret = fis_validate(old_parts, n_old, new_parts, n_new);
437                 if (ret < 0) {
438                         fprintf(stderr, "Failed to validate the new FIS partition table\n");
439                         exit(1);
440                 }
441                 if (ret == 0)
442                         fis_layout = NULL;
443         }
444 #endif
445
446         if (strchr(mtd, ':')) {
447                 str = strdup(mtd);
448                 mtd = str;
449         }
450
451         r = 0;
452
453 resume:
454         next = strchr(mtd, ':');
455         if (next) {
456                 *next = 0;
457                 next++;
458         }
459
460         fd = mtd_check_open(mtd);
461         if(fd < 0) {
462                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
463                 exit(1);
464         }
465         if (part_offset > 0) {
466                 fprintf(stderr, "Seeking on mtd device '%s' to: %zu\n", mtd, part_offset);
467                 lseek(fd, part_offset, SEEK_SET);
468         }
469
470         indicate_writing(mtd);
471
472         w = e = 0;
473         for (;;) {
474                 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
475                 while (buflen < erasesize) {
476                         r = read(imagefd, buf + buflen, erasesize - buflen);
477                         if (r < 0) {
478                                 if ((errno == EINTR) || (errno == EAGAIN))
479                                         continue;
480                                 else {
481                                         perror("read");
482                                         break;
483                                 }
484                         }
485
486                         if (r == 0)
487                                 break;
488
489                         buflen += r;
490                 }
491
492                 if (buflen == 0)
493                         break;
494
495                 if (buflen < erasesize) {
496                         /* Pad block to eraseblock size */
497                         memset(&buf[buflen], 0xff, erasesize - buflen);
498                         buflen = erasesize;
499                 }
500
501                 if (skip > 0) {
502                         skip -= buflen;
503                         buflen = 0;
504                         if (skip <= 0)
505                                 indicate_writing(mtd);
506
507                         continue;
508                 }
509
510                 if (jffs2file && w >= jffs2_skip_bytes) {
511                         if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
512                                 if (!quiet)
513                                         fprintf(stderr, "\b\b\b   ");
514                                 if (quiet < 2)
515                                         fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtd);
516                                 /* got an EOF marker - this is the place to add some jffs2 data */
517                                 skip = mtd_replace_jffs2(mtd, fd, e, jffs2file);
518                                 jffs2_replaced = 1;
519
520                                 /* don't add it again */
521                                 jffs2file = NULL;
522
523                                 w += skip;
524                                 e += skip;
525                                 skip -= buflen;
526                                 buflen = 0;
527                                 offset = 0;
528                                 continue;
529                         }
530                         /* no EOF marker, make sure we figure out the last inode number
531                          * before appending some data */
532                         mtd_parse_jffs2data(buf, jffs2dir);
533                 }
534
535                 /* need to erase the next block before writing data to it */
536                 if(!no_erase)
537                 {
538                         while (w + buflen > e - skip_bad_blocks) {
539                                 if (!quiet)
540                                         fprintf(stderr, "\b\b\b[e]");
541
542                                 if (mtd_block_is_bad(fd, e)) {
543                                         if (!quiet)
544                                                 fprintf(stderr, "\nSkipping bad block at 0x%08zx   ", e);
545
546                                         skip_bad_blocks += erasesize;
547                                         e += erasesize;
548
549                                         // Move the file pointer along over the bad block.
550                                         lseek(fd, erasesize, SEEK_CUR);
551                                         continue;
552                                 }
553
554                                 if (mtd_erase_block(fd, e) < 0) {
555                                         if (next) {
556                                                 if (w < e) {
557                                                         write(fd, buf + offset, e - w);
558                                                         offset = e - w;
559                                                 }
560                                                 w = 0;
561                                                 e = 0;
562                                                 close(fd);
563                                                 mtd = next;
564                                                 fprintf(stderr, "\b\b\b   \n");
565                                                 goto resume;
566                                         } else {
567                                                 fprintf(stderr, "Failed to erase block\n");
568                                                 exit(1);
569                                         }
570                                 }
571
572                                 /* erase the chunk */
573                                 e += erasesize;
574                         }
575                 }
576
577                 if (!quiet)
578                         fprintf(stderr, "\b\b\b[w]");
579
580                 if ((result = write(fd, buf + offset, buflen)) < buflen) {
581                         if (result < 0) {
582                                 fprintf(stderr, "Error writing image.\n");
583                                 exit(1);
584                         } else {
585                                 fprintf(stderr, "Insufficient space.\n");
586                                 exit(1);
587                         }
588                 }
589                 w += buflen;
590
591                 buflen = 0;
592                 offset = 0;
593         }
594
595         if (jffs2_replaced && trx_fixup) {
596                 trx_fixup(fd, mtd);
597         }
598
599         if (!quiet)
600                 fprintf(stderr, "\b\b\b\b    ");
601
602         if (quiet < 2)
603                 fprintf(stderr, "\n");
604
605 #ifdef FIS_SUPPORT
606         if (fis_layout) {
607                 if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
608                         fprintf(stderr, "Failed to update the FIS partition table\n");
609         }
610 #endif
611
612         close(fd);
613         return 0;
614 }
615
616 static void usage(void)
617 {
618         fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
619         "The device is in the format of mtdX (eg: mtd4) or its label.\n"
620         "mtd recognizes these commands:\n"
621         "        unlock                  unlock the device\n"
622         "        refresh                 refresh mtd partition\n"
623         "        erase                   erase all data on device\n"
624         "        verify <imagefile>|-    verify <imagefile> (use - for stdin) to device\n"
625         "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
626         "        jffs2write <file>       append <file> to the jffs2 partition on the device\n");
627         if (mtd_fixtrx) {
628             fprintf(stderr,
629         "        fixtrx                  fix the checksum in a trx header on first boot\n");
630         }
631         if (mtd_fixseama) {
632             fprintf(stderr,
633         "        fixseama                fix the checksum in a seama header on first boot\n");
634         }
635         fprintf(stderr,
636         "Following options are available:\n"
637         "        -q                      quiet mode (once: no [w] on writing,\n"
638         "                                           twice: no status messages)\n"
639         "        -n                      write without first erasing the blocks\n"
640         "        -r                      reboot after successful command\n"
641         "        -f                      force write without trx checks\n"
642         "        -e <device>             erase <device> before executing the command\n"
643         "        -d <name>               directory for jffs2write, defaults to \"tmp\"\n"
644         "        -j <name>               integrate <file> into jffs2 data when writing an image\n"
645         "        -s <number>             skip the first n bytes when appending data to the jffs2 partiton, defaults to \"0\"\n"
646         "        -p                      write beginning at partition offset\n"
647         "        -l <length>             the length of data that we want to dump\n");
648         if (mtd_fixtrx) {
649             fprintf(stderr,
650         "        -o offset               offset of the image header in the partition(for fixtrx)\n");
651         }
652         fprintf(stderr,
653 #ifdef FIS_SUPPORT
654         "        -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
655         "                                alter the fis partition table to create new partitions replacing\n"
656         "                                the partitions provided as argument to the write command\n"
657         "                                (only valid together with the write command)\n"
658 #endif
659         "\n"
660         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
661         "         mtd -r write linux.trx linux\n\n");
662         exit(1);
663 }
664
665 static void do_reboot(void)
666 {
667         fprintf(stderr, "Rebooting ...\n");
668         fflush(stderr);
669
670         /* try regular reboot method first */
671         system("/sbin/reboot");
672         sleep(2);
673
674         /* if we're still alive at this point, force the kernel to reboot */
675         syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
676 }
677
678 int main (int argc, char **argv)
679 {
680         int ch, i, boot, imagefd = 0, force, unlocked;
681         char *erase[MAX_ARGS], *device = NULL;
682         char *fis_layout = NULL;
683         size_t offset = 0, part_offset = 0, dump_len = 0;
684         enum {
685                 CMD_ERASE,
686                 CMD_WRITE,
687                 CMD_UNLOCK,
688                 CMD_JFFS2WRITE,
689                 CMD_FIXTRX,
690                 CMD_FIXSEAMA,
691                 CMD_VERIFY,
692                 CMD_DUMP,
693         } cmd = -1;
694
695         erase[0] = NULL;
696         boot = 0;
697         force = 0;
698         buflen = 0;
699         quiet = 0;
700         no_erase = 0;
701
702         while ((ch = getopt(argc, argv,
703 #ifdef FIS_SUPPORT
704                         "F:"
705 #endif
706                         "frnqe:d:s:j:p:o:l:")) != -1)
707                 switch (ch) {
708                         case 'f':
709                                 force = 1;
710                                 break;
711                         case 'r':
712                                 boot = 1;
713                                 break;
714                         case 'n':
715                                 no_erase = 1;
716                                 break;
717                         case 'j':
718                                 jffs2file = optarg;
719                                 break;
720                         case 's':
721                                 errno = 0;
722                                 jffs2_skip_bytes = strtoul(optarg, 0, 0);
723                                 if (errno) {
724                                                 fprintf(stderr, "-s: illegal numeric string\n");
725                                                 usage();
726                                 }
727                                 break;
728                         case 'q':
729                                 quiet++;
730                                 break;
731                         case 'e':
732                                 i = 0;
733                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
734                                         i++;
735
736                                 erase[i++] = optarg;
737                                 erase[i] = NULL;
738                                 break;
739                         case 'd':
740                                 jffs2dir = optarg;
741                                 break;
742                         case 'p':
743                                 errno = 0;
744                                 part_offset = strtoul(optarg, 0, 0);
745                                 if (errno) {
746                                         fprintf(stderr, "-p: illegal numeric string\n");
747                                         usage();
748                                 }
749                                 break;
750                         case 'l':
751                                 errno = 0;
752                                 dump_len = strtoul(optarg, 0, 0);
753                                 if (errno) {
754                                         fprintf(stderr, "-l: illegal numeric string\n");
755                                         usage();
756                                 }
757                                 break;
758                         case 'o':
759                                 if (!mtd_fixtrx) {
760                                         fprintf(stderr, "-o: is not available on this platform\n");
761                                         usage();
762                                 }
763                                 errno = 0;
764                                 offset = strtoul(optarg, 0, 0);
765                                 if (errno) {
766                                         fprintf(stderr, "-o: illegal numeric string\n");
767                                         usage();
768                                 }
769                                 break;
770 #ifdef FIS_SUPPORT
771                         case 'F':
772                                 fis_layout = optarg;
773                                 break;
774 #endif
775                         case '?':
776                         default:
777                                 usage();
778                 }
779         argc -= optind;
780         argv += optind;
781
782         if (argc < 2)
783                 usage();
784
785         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
786                 cmd = CMD_UNLOCK;
787                 device = argv[1];
788         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
789                 cmd = CMD_ERASE;
790                 device = argv[1];
791         } else if (((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) && mtd_fixtrx) {
792                 cmd = CMD_FIXTRX;
793                 device = argv[1];
794         } else if (((strcmp(argv[0], "fixseama") == 0) && (argc == 2)) && mtd_fixseama) {
795                 cmd = CMD_FIXSEAMA;
796                 device = argv[1];
797         } else if ((strcmp(argv[0], "verify") == 0) && (argc == 3)) {
798                 cmd = CMD_VERIFY;
799                 imagefile = argv[1];
800                 device = argv[2];
801         } else if ((strcmp(argv[0], "dump") == 0) && (argc == 2)) {
802                 cmd = CMD_DUMP;
803                 device = argv[1];
804         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
805                 cmd = CMD_WRITE;
806                 device = argv[2];
807
808                 if (strcmp(argv[1], "-") == 0) {
809                         imagefile = "<stdin>";
810                         imagefd = 0;
811                 } else {
812                         imagefile = argv[1];
813                         if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
814                                 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
815                                 exit(1);
816                         }
817                 }
818
819                 if (!mtd_check(device)) {
820                         fprintf(stderr, "Can't open device for writing!\n");
821                         exit(1);
822                 }
823                 /* check trx file before erasing or writing anything */
824                 if (!image_check(imagefd, device) && !force) {
825                         fprintf(stderr, "Image check failed.\n");
826                         exit(1);
827                 }
828         } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
829                 cmd = CMD_JFFS2WRITE;
830                 device = argv[2];
831
832                 imagefile = argv[1];
833                 if (!mtd_check(device)) {
834                         fprintf(stderr, "Can't open device for writing!\n");
835                         exit(1);
836                 }
837         } else {
838                 usage();
839         }
840
841         sync();
842
843         i = 0;
844         unlocked = 0;
845         while (erase[i] != NULL) {
846                 mtd_unlock(erase[i]);
847                 mtd_erase(erase[i]);
848                 if (strcmp(erase[i], device) == 0)
849                         unlocked = 1;
850                 i++;
851         }
852
853         switch (cmd) {
854                 case CMD_UNLOCK:
855                         if (!unlocked)
856                                 mtd_unlock(device);
857                         break;
858                 case CMD_VERIFY:
859                         mtd_verify(device, imagefile);
860                         break;
861                 case CMD_DUMP:
862                         mtd_dump(device, dump_len);
863                         break;
864                 case CMD_ERASE:
865                         if (!unlocked)
866                                 mtd_unlock(device);
867                         mtd_erase(device);
868                         break;
869                 case CMD_WRITE:
870                         if (!unlocked)
871                                 mtd_unlock(device);
872                         mtd_write(imagefd, device, fis_layout, part_offset);
873                         break;
874                 case CMD_JFFS2WRITE:
875                         if (!unlocked)
876                                 mtd_unlock(device);
877                         mtd_write_jffs2(device, imagefile, jffs2dir);
878                         break;
879                 case CMD_FIXTRX:
880                     if (mtd_fixtrx) {
881                             mtd_fixtrx(device, offset);
882             }
883                 case CMD_FIXSEAMA:
884                         if (mtd_fixseama)
885                             mtd_fixseama(device, 0);
886                         break;
887         }
888
889         sync();
890
891         if (boot)
892                 do_reboot();
893
894         return 0;
895 }