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