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