mtd: fix 2 compiler warnings
[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_verify(const char *mtd, char *file)
278 {
279         uint32_t f_md5[4], m_md5[4];
280         struct stat s;
281         md5_ctx_t ctx;
282         int ret = 0;
283         int fd;
284
285         if (quiet < 2)
286                 fprintf(stderr, "Verifying %s against %s ...\n", mtd, file);
287
288         if (stat(file, &s) || md5sum(file, f_md5)) {
289                 fprintf(stderr, "Failed to hash %s\n", file);
290                 return -1;
291         }
292
293         fd = mtd_check_open(mtd);
294         if(fd < 0) {
295                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
296                 return -1;
297         }
298
299         md5_begin(&ctx);
300         do {
301                 char buf[256];
302                 int len = (s.st_size > sizeof(buf)) ? (sizeof(buf)) : (s.st_size);
303                 int rlen = read(fd, buf, len);
304
305                 if (rlen < 0) {
306                         if (errno == EINTR)
307                                 continue;
308                         ret = -1;
309                         goto out;
310                 }
311                 if (!rlen)
312                         break;
313                 md5_hash(buf, rlen, &ctx);
314                 s.st_size -= rlen;
315         } while (s.st_size > 0);
316
317         md5_end(m_md5, &ctx);
318
319         fprintf(stderr, "%08x%08x%08x%08x - %s\n", m_md5[0], m_md5[1], m_md5[2], m_md5[3], mtd);
320         fprintf(stderr, "%08x%08x%08x%08x - %s\n", f_md5[0], f_md5[1], f_md5[2], f_md5[3], file);
321
322         ret = memcmp(f_md5, m_md5, sizeof(m_md5));
323         if (!ret)
324                 fprintf(stderr, "Success\n");
325         else
326                 fprintf(stderr, "Failed\n");
327
328 out:
329         close(fd);
330         return ret;
331 }
332
333 static void
334 indicate_writing(const char *mtd)
335 {
336         if (quiet < 2)
337                 fprintf(stderr, "\nWriting from %s to %s ... ", imagefile, mtd);
338
339         if (!quiet)
340                 fprintf(stderr, " [ ]");
341 }
342
343 static int
344 mtd_write(int imagefd, const char *mtd, char *fis_layout, size_t part_offset)
345 {
346         char *next = NULL;
347         char *str = NULL;
348         int fd, result;
349         ssize_t r, w, e;
350         ssize_t skip = 0;
351         uint32_t offset = 0;
352         int jffs2_replaced = 0;
353         int skip_bad_blocks = 0;
354
355 #ifdef FIS_SUPPORT
356         static struct fis_part new_parts[MAX_ARGS];
357         static struct fis_part old_parts[MAX_ARGS];
358         int n_new = 0, n_old = 0;
359
360         if (fis_layout) {
361                 const char *tmp = mtd;
362                 char *word, *brkt;
363                 int ret;
364
365                 memset(&old_parts, 0, sizeof(old_parts));
366                 memset(&new_parts, 0, sizeof(new_parts));
367
368                 do {
369                         next = strchr(tmp, ':');
370                         if (!next)
371                                 next = (char *) tmp + strlen(tmp);
372
373                         memcpy(old_parts[n_old].name, tmp, next - tmp);
374
375                         n_old++;
376                         tmp = next + 1;
377                 } while(*next);
378
379                 for (word = strtok_r(fis_layout, ",", &brkt);
380                      word;
381                          word = strtok_r(NULL, ",", &brkt)) {
382
383                         tmp = strtok(word, ":");
384                         strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
385
386                         tmp = strtok(NULL, ":");
387                         if (!tmp)
388                                 goto next;
389
390                         new_parts[n_new].size = strtoul(tmp, NULL, 0);
391
392                         tmp = strtok(NULL, ":");
393                         if (!tmp)
394                                 goto next;
395
396                         new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
397 next:
398                         n_new++;
399                 }
400                 ret = fis_validate(old_parts, n_old, new_parts, n_new);
401                 if (ret < 0) {
402                         fprintf(stderr, "Failed to validate the new FIS partition table\n");
403                         exit(1);
404                 }
405                 if (ret == 0)
406                         fis_layout = NULL;
407         }
408 #endif
409
410         if (strchr(mtd, ':')) {
411                 str = strdup(mtd);
412                 mtd = str;
413         }
414
415         r = 0;
416
417 resume:
418         next = strchr(mtd, ':');
419         if (next) {
420                 *next = 0;
421                 next++;
422         }
423
424         fd = mtd_check_open(mtd);
425         if(fd < 0) {
426                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
427                 exit(1);
428         }
429         if (part_offset > 0) {
430                 fprintf(stderr, "Seeking on mtd device '%s' to: %zu\n", mtd, part_offset);
431                 lseek(fd, part_offset, SEEK_SET);
432         }
433
434         indicate_writing(mtd);
435
436         w = e = 0;
437         for (;;) {
438                 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
439                 while (buflen < erasesize) {
440                         r = read(imagefd, buf + buflen, erasesize - buflen);
441                         if (r < 0) {
442                                 if ((errno == EINTR) || (errno == EAGAIN))
443                                         continue;
444                                 else {
445                                         perror("read");
446                                         break;
447                                 }
448                         }
449
450                         if (r == 0)
451                                 break;
452
453                         buflen += r;
454                 }
455
456                 if (buflen == 0)
457                         break;
458
459                 if (buflen < erasesize) {
460                         /* Pad block to eraseblock size */
461                         memset(&buf[buflen], 0xff, erasesize - buflen);
462                         buflen = erasesize;
463                 }
464
465                 if (skip > 0) {
466                         skip -= buflen;
467                         buflen = 0;
468                         if (skip <= 0)
469                                 indicate_writing(mtd);
470
471                         continue;
472                 }
473
474                 if (jffs2file && w >= jffs2_skip_bytes) {
475                         if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
476                                 if (!quiet)
477                                         fprintf(stderr, "\b\b\b   ");
478                                 if (quiet < 2)
479                                         fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtd);
480                                 /* got an EOF marker - this is the place to add some jffs2 data */
481                                 skip = mtd_replace_jffs2(mtd, fd, e, jffs2file);
482                                 jffs2_replaced = 1;
483
484                                 /* don't add it again */
485                                 jffs2file = NULL;
486
487                                 w += skip;
488                                 e += skip;
489                                 skip -= buflen;
490                                 buflen = 0;
491                                 offset = 0;
492                                 continue;
493                         }
494                         /* no EOF marker, make sure we figure out the last inode number
495                          * before appending some data */
496                         mtd_parse_jffs2data(buf, jffs2dir);
497                 }
498
499                 /* need to erase the next block before writing data to it */
500                 if(!no_erase)
501                 {
502                         while (w + buflen > e - skip_bad_blocks) {
503                                 if (!quiet)
504                                         fprintf(stderr, "\b\b\b[e]");
505
506                                 if (mtd_block_is_bad(fd, e)) {
507                                         if (!quiet)
508                                                 fprintf(stderr, "\nSkipping bad block at 0x%08zx   ", e);
509
510                                         skip_bad_blocks += erasesize;
511                                         e += erasesize;
512
513                                         // Move the file pointer along over the bad block.
514                                         lseek(fd, erasesize, SEEK_CUR);
515                                         continue;
516                                 }
517
518                                 if (mtd_erase_block(fd, e) < 0) {
519                                         if (next) {
520                                                 if (w < e) {
521                                                         write(fd, buf + offset, e - w);
522                                                         offset = e - w;
523                                                 }
524                                                 w = 0;
525                                                 e = 0;
526                                                 close(fd);
527                                                 mtd = next;
528                                                 fprintf(stderr, "\b\b\b   \n");
529                                                 goto resume;
530                                         } else {
531                                                 fprintf(stderr, "Failed to erase block\n");
532                                                 exit(1);
533                                         }
534                                 }
535
536                                 /* erase the chunk */
537                                 e += erasesize;
538                         }
539                 }
540
541                 if (!quiet)
542                         fprintf(stderr, "\b\b\b[w]");
543
544                 if ((result = write(fd, buf + offset, buflen)) < buflen) {
545                         if (result < 0) {
546                                 fprintf(stderr, "Error writing image.\n");
547                                 exit(1);
548                         } else {
549                                 fprintf(stderr, "Insufficient space.\n");
550                                 exit(1);
551                         }
552                 }
553                 w += buflen;
554
555                 buflen = 0;
556                 offset = 0;
557         }
558
559         if (jffs2_replaced && trx_fixup) {
560                 trx_fixup(fd, mtd);
561         }
562
563         if (!quiet)
564                 fprintf(stderr, "\b\b\b\b    ");
565
566         if (quiet < 2)
567                 fprintf(stderr, "\n");
568
569 #ifdef FIS_SUPPORT
570         if (fis_layout) {
571                 if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
572                         fprintf(stderr, "Failed to update the FIS partition table\n");
573         }
574 #endif
575
576         close(fd);
577         return 0;
578 }
579
580 static void usage(void)
581 {
582         fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
583         "The device is in the format of mtdX (eg: mtd4) or its label.\n"
584         "mtd recognizes these commands:\n"
585         "        unlock                  unlock the device\n"
586         "        refresh                 refresh mtd partition\n"
587         "        erase                   erase all data on device\n"
588         "        verify <imagefile>|-    verify <imagefile> (use - for stdin) to device\n"
589         "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
590         "        jffs2write <file>       append <file> to the jffs2 partition on the device\n");
591         if (mtd_fixtrx) {
592             fprintf(stderr,
593         "        fixtrx                  fix the checksum in a trx header on first boot\n");
594         }
595         if (mtd_fixseama) {
596             fprintf(stderr,
597         "        fixseama                fix the checksum in a seama header on first boot\n");
598         }
599     fprintf(stderr,
600         "Following options are available:\n"
601         "        -q                      quiet mode (once: no [w] on writing,\n"
602         "                                           twice: no status messages)\n"
603         "        -n                      write without first erasing the blocks\n"
604         "        -r                      reboot after successful command\n"
605         "        -f                      force write without trx checks\n"
606         "        -e <device>             erase <device> before executing the command\n"
607         "        -d <name>               directory for jffs2write, defaults to \"tmp\"\n"
608         "        -j <name>               integrate <file> into jffs2 data when writing an image\n"
609         "        -s <number>             skip the first n bytes when appending data to the jffs2 partiton, defaults to \"0\"\n"
610         "        -p                      write beginning at partition offset\n");
611         if (mtd_fixtrx) {
612             fprintf(stderr,
613         "        -o offset               offset of the image header in the partition(for fixtrx)\n");
614     }
615         fprintf(stderr,
616 #ifdef FIS_SUPPORT
617         "        -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
618         "                                alter the fis partition table to create new partitions replacing\n"
619         "                                the partitions provided as argument to the write command\n"
620         "                                (only valid together with the write command)\n"
621 #endif
622         "\n"
623         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
624         "         mtd -r write linux.trx linux\n\n");
625         exit(1);
626 }
627
628 static void do_reboot(void)
629 {
630         fprintf(stderr, "Rebooting ...\n");
631         fflush(stderr);
632
633         /* try regular reboot method first */
634         system("/sbin/reboot");
635         sleep(2);
636
637         /* if we're still alive at this point, force the kernel to reboot */
638         syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
639 }
640
641 int main (int argc, char **argv)
642 {
643         int ch, i, boot, imagefd = 0, force, unlocked;
644         char *erase[MAX_ARGS], *device = NULL;
645         char *fis_layout = NULL;
646         size_t offset = 0, part_offset = 0;
647         enum {
648                 CMD_ERASE,
649                 CMD_WRITE,
650                 CMD_UNLOCK,
651                 CMD_JFFS2WRITE,
652                 CMD_FIXTRX,
653                 CMD_FIXSEAMA,
654                 CMD_VERIFY,
655         } cmd = -1;
656
657         erase[0] = NULL;
658         boot = 0;
659         force = 0;
660         buflen = 0;
661         quiet = 0;
662         no_erase = 0;
663
664         while ((ch = getopt(argc, argv,
665 #ifdef FIS_SUPPORT
666                         "F:"
667 #endif
668                         "frnqe:d:s:j:p:o:")) != -1)
669                 switch (ch) {
670                         case 'f':
671                                 force = 1;
672                                 break;
673                         case 'r':
674                                 boot = 1;
675                                 break;
676                         case 'n':
677                                 no_erase = 1;
678                                 break;
679                         case 'j':
680                                 jffs2file = optarg;
681                                 break;
682                         case 's':
683                                 errno = 0;
684                                 jffs2_skip_bytes = strtoul(optarg, 0, 0);
685                                 if (errno) {
686                                                 fprintf(stderr, "-s: illegal numeric string\n");
687                                                 usage();
688                                 }
689                                 break;
690                         case 'q':
691                                 quiet++;
692                                 break;
693                         case 'e':
694                                 i = 0;
695                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
696                                         i++;
697
698                                 erase[i++] = optarg;
699                                 erase[i] = NULL;
700                                 break;
701                         case 'd':
702                                 jffs2dir = optarg;
703                                 break;
704                         case 'p':
705                                 errno = 0;
706                                 part_offset = strtoul(optarg, 0, 0);
707                                 if (errno) {
708                                         fprintf(stderr, "-p: illegal numeric string\n");
709                                         usage();
710                                 }
711                                 break;
712                         case 'o':
713                                 if (!mtd_fixtrx) {
714                                         fprintf(stderr, "-o: is not available on this platform\n");
715                                         usage();
716                                 }
717                                 errno = 0;
718                                 offset = strtoul(optarg, 0, 0);
719                                 if (errno) {
720                                         fprintf(stderr, "-o: illegal numeric string\n");
721                                         usage();
722                                 }
723                                 break;
724 #ifdef FIS_SUPPORT
725                         case 'F':
726                                 fis_layout = optarg;
727                                 break;
728 #endif
729                         case '?':
730                         default:
731                                 usage();
732                 }
733         argc -= optind;
734         argv += optind;
735
736         if (argc < 2)
737                 usage();
738
739         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
740                 cmd = CMD_UNLOCK;
741                 device = argv[1];
742         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
743                 cmd = CMD_ERASE;
744                 device = argv[1];
745         } else if (((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) && mtd_fixtrx) {
746                 cmd = CMD_FIXTRX;
747                 device = argv[1];
748         } else if (((strcmp(argv[0], "fixseama") == 0) && (argc == 2)) && mtd_fixseama) {
749                 cmd = CMD_FIXSEAMA;
750                 device = argv[1];
751         } else if ((strcmp(argv[0], "verify") == 0) && (argc == 3)) {
752                 cmd = CMD_VERIFY;
753                 imagefile = argv[1];
754                 device = argv[2];
755         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
756                 cmd = CMD_WRITE;
757                 device = argv[2];
758
759                 if (strcmp(argv[1], "-") == 0) {
760                         imagefile = "<stdin>";
761                         imagefd = 0;
762                 } else {
763                         imagefile = argv[1];
764                         if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
765                                 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
766                                 exit(1);
767                         }
768                 }
769
770                 if (!mtd_check(device)) {
771                         fprintf(stderr, "Can't open device for writing!\n");
772                         exit(1);
773                 }
774                 /* check trx file before erasing or writing anything */
775                 if (!image_check(imagefd, device) && !force) {
776                         fprintf(stderr, "Image check failed.\n");
777                         exit(1);
778                 }
779         } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
780                 cmd = CMD_JFFS2WRITE;
781                 device = argv[2];
782
783                 imagefile = argv[1];
784                 if (!mtd_check(device)) {
785                         fprintf(stderr, "Can't open device for writing!\n");
786                         exit(1);
787                 }
788         } else {
789                 usage();
790         }
791
792         sync();
793
794         i = 0;
795         unlocked = 0;
796         while (erase[i] != NULL) {
797                 mtd_unlock(erase[i]);
798                 mtd_erase(erase[i]);
799                 if (strcmp(erase[i], device) == 0)
800                         unlocked = 1;
801                 i++;
802         }
803
804         switch (cmd) {
805                 case CMD_UNLOCK:
806                         if (!unlocked)
807                                 mtd_unlock(device);
808                         break;
809                 case CMD_VERIFY:
810                         mtd_verify(device, imagefile);
811                         break;
812                 case CMD_ERASE:
813                         if (!unlocked)
814                                 mtd_unlock(device);
815                         mtd_erase(device);
816                         break;
817                 case CMD_WRITE:
818                         if (!unlocked)
819                                 mtd_unlock(device);
820                         mtd_write(imagefd, device, fis_layout, part_offset);
821                         break;
822                 case CMD_JFFS2WRITE:
823                         if (!unlocked)
824                                 mtd_unlock(device);
825                         mtd_write_jffs2(device, imagefile, jffs2dir);
826                         break;
827                 case CMD_FIXTRX:
828                     if (mtd_fixtrx) {
829                             mtd_fixtrx(device, offset);
830             }
831                 case CMD_FIXSEAMA:
832                         if (mtd_fixseama)
833                             mtd_fixseama(device, 0);
834                         break;
835         }
836
837         sync();
838
839         if (boot)
840                 do_reboot();
841
842         return 0;
843 }