mtd: improve mtd detection
[openwrt/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 char name[PATH_MAX];
71
72 snprintf(name, sizeof(name), "\"%s\"", mtd);
73 if ((fp = fopen("/proc/mtd", "r"))) {
74 while (fgets(dev, sizeof(dev), fp)) {
75 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, name)) {
76 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
77 if ((ret=open(dev, flags))<0) {
78 snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
79 ret=open(dev, flags);
80 }
81 fclose(fp);
82 return ret;
83 }
84 }
85 fclose(fp);
86 }
87
88 return open(mtd, flags);
89 }
90
91 int mtd_check_open(const char *mtd)
92 {
93 struct mtd_info_user mtdInfo;
94 int fd;
95
96 fd = mtd_open(mtd, false);
97 if(fd < 0) {
98 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
99 return -1;
100 }
101
102 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
103 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
104 close(fd);
105 return -1;
106 }
107 mtdsize = mtdInfo.size;
108 erasesize = mtdInfo.erasesize;
109 mtdtype = mtdInfo.type;
110
111 return fd;
112 }
113
114 int mtd_block_is_bad(int fd, int offset)
115 {
116 int r = 0;
117 loff_t o = offset;
118
119 if (mtdtype == MTD_NANDFLASH)
120 {
121 r = ioctl(fd, MEMGETBADBLOCK, &o);
122 if (r < 0)
123 {
124 fprintf(stderr, "Failed to get erase block status\n");
125 exit(1);
126 }
127 }
128 return r;
129 }
130
131 int mtd_erase_block(int fd, int offset)
132 {
133 struct erase_info_user mtdEraseInfo;
134
135 mtdEraseInfo.start = offset;
136 mtdEraseInfo.length = erasesize;
137 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
138 if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
139 return -1;
140
141 return 0;
142 }
143
144 int mtd_write_buffer(int fd, const char *buf, int offset, int length)
145 {
146 lseek(fd, offset, SEEK_SET);
147 write(fd, buf, length);
148 return 0;
149 }
150
151
152 static int
153 image_check(int imagefd, const char *mtd)
154 {
155 int ret = 1;
156 if (trx_check) {
157 ret = trx_check(imagefd, mtd, buf, &buflen);
158 }
159
160 return ret;
161 }
162
163 static int mtd_check(const char *mtd)
164 {
165 char *next = NULL;
166 char *str = NULL;
167 int fd;
168
169 if (strchr(mtd, ':')) {
170 str = strdup(mtd);
171 mtd = str;
172 }
173
174 do {
175 next = strchr(mtd, ':');
176 if (next) {
177 *next = 0;
178 next++;
179 }
180
181 fd = mtd_check_open(mtd);
182 if (fd < 0)
183 return 0;
184
185 if (!buf)
186 buf = malloc(erasesize);
187
188 close(fd);
189 mtd = next;
190 } while (next);
191
192 if (str)
193 free(str);
194
195 return 1;
196 }
197
198 static int
199 mtd_unlock(const char *mtd)
200 {
201 struct erase_info_user mtdLockInfo;
202 char *next = NULL;
203 char *str = NULL;
204 int fd;
205
206 if (strchr(mtd, ':')) {
207 str = strdup(mtd);
208 mtd = str;
209 }
210
211 do {
212 next = strchr(mtd, ':');
213 if (next) {
214 *next = 0;
215 next++;
216 }
217
218 fd = mtd_check_open(mtd);
219 if(fd < 0) {
220 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
221 exit(1);
222 }
223
224 if (quiet < 2)
225 fprintf(stderr, "Unlocking %s ...\n", mtd);
226
227 mtdLockInfo.start = 0;
228 mtdLockInfo.length = mtdsize;
229 ioctl(fd, MEMUNLOCK, &mtdLockInfo);
230 close(fd);
231 mtd = next;
232 } while (next);
233
234 if (str)
235 free(str);
236
237 return 0;
238 }
239
240 static int
241 mtd_erase(const char *mtd)
242 {
243 int fd;
244 struct erase_info_user mtdEraseInfo;
245
246 if (quiet < 2)
247 fprintf(stderr, "Erasing %s ...\n", mtd);
248
249 fd = mtd_check_open(mtd);
250 if(fd < 0) {
251 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
252 exit(1);
253 }
254
255 mtdEraseInfo.length = erasesize;
256
257 for (mtdEraseInfo.start = 0;
258 mtdEraseInfo.start < mtdsize;
259 mtdEraseInfo.start += erasesize) {
260 if (mtd_block_is_bad(fd, mtdEraseInfo.start)) {
261 if (!quiet)
262 fprintf(stderr, "\nSkipping bad block at 0x%x ", mtdEraseInfo.start);
263 } else {
264 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
265 if(ioctl(fd, MEMERASE, &mtdEraseInfo))
266 fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
267 }
268 }
269
270 close(fd);
271 return 0;
272
273 }
274
275 static int
276 mtd_verify(const char *mtd, char *file)
277 {
278 uint32_t f_md5[4], m_md5[4];
279 struct stat s;
280 md5_ctx_t ctx;
281 int ret = 0;
282 int fd;
283
284 if (quiet < 2)
285 fprintf(stderr, "Verifying %s against %s ...\n", mtd, file);
286
287 if (stat(file, &s) || md5sum(file, f_md5)) {
288 fprintf(stderr, "Failed to hash %s\n", file);
289 return -1;
290 }
291
292 fd = mtd_check_open(mtd);
293 if(fd < 0) {
294 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
295 return -1;
296 }
297
298 md5_begin(&ctx);
299 do {
300 char buf[256];
301 int len = (s.st_size > sizeof(buf)) ? (sizeof(buf)) : (s.st_size);
302 int rlen = read(fd, buf, len);
303
304 if (rlen < 0) {
305 if (errno == EINTR)
306 continue;
307 ret = -1;
308 goto out;
309 }
310 if (!rlen)
311 break;
312 md5_hash(buf, rlen, &ctx);
313 s.st_size -= rlen;
314 } while (s.st_size > 0);
315
316 md5_end(m_md5, &ctx);
317
318 fprintf(stderr, "%08x%08x%08x%08x - %s\n", m_md5[0], m_md5[1], m_md5[2], m_md5[3], mtd);
319 fprintf(stderr, "%08x%08x%08x%08x - %s\n", f_md5[0], f_md5[1], f_md5[2], f_md5[3], file);
320
321 ret = memcmp(f_md5, m_md5, sizeof(m_md5));
322 if (!ret)
323 fprintf(stderr, "Success\n");
324 else
325 fprintf(stderr, "Failed\n");
326
327 out:
328 close(fd);
329 return ret;
330 }
331
332 static void
333 indicate_writing(const char *mtd)
334 {
335 if (quiet < 2)
336 fprintf(stderr, "\nWriting from %s to %s ... ", imagefile, mtd);
337
338 if (!quiet)
339 fprintf(stderr, " [ ]");
340 }
341
342 static int
343 mtd_write(int imagefd, const char *mtd, char *fis_layout, size_t part_offset)
344 {
345 char *next = NULL;
346 char *str = NULL;
347 int fd, result;
348 ssize_t r, w, e;
349 ssize_t skip = 0;
350 uint32_t offset = 0;
351 int jffs2_replaced = 0;
352 int skip_bad_blocks = 0;
353
354 #ifdef FIS_SUPPORT
355 static struct fis_part new_parts[MAX_ARGS];
356 static struct fis_part old_parts[MAX_ARGS];
357 int n_new = 0, n_old = 0;
358
359 if (fis_layout) {
360 const char *tmp = mtd;
361 char *word, *brkt;
362 int ret;
363
364 memset(&old_parts, 0, sizeof(old_parts));
365 memset(&new_parts, 0, sizeof(new_parts));
366
367 do {
368 next = strchr(tmp, ':');
369 if (!next)
370 next = (char *) tmp + strlen(tmp);
371
372 memcpy(old_parts[n_old].name, tmp, next - tmp);
373
374 n_old++;
375 tmp = next + 1;
376 } while(*next);
377
378 for (word = strtok_r(fis_layout, ",", &brkt);
379 word;
380 word = strtok_r(NULL, ",", &brkt)) {
381
382 tmp = strtok(word, ":");
383 strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
384
385 tmp = strtok(NULL, ":");
386 if (!tmp)
387 goto next;
388
389 new_parts[n_new].size = strtoul(tmp, NULL, 0);
390
391 tmp = strtok(NULL, ":");
392 if (!tmp)
393 goto next;
394
395 new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
396 next:
397 n_new++;
398 }
399 ret = fis_validate(old_parts, n_old, new_parts, n_new);
400 if (ret < 0) {
401 fprintf(stderr, "Failed to validate the new FIS partition table\n");
402 exit(1);
403 }
404 if (ret == 0)
405 fis_layout = NULL;
406 }
407 #endif
408
409 if (strchr(mtd, ':')) {
410 str = strdup(mtd);
411 mtd = str;
412 }
413
414 r = 0;
415
416 resume:
417 next = strchr(mtd, ':');
418 if (next) {
419 *next = 0;
420 next++;
421 }
422
423 fd = mtd_check_open(mtd);
424 if(fd < 0) {
425 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
426 exit(1);
427 }
428 if (part_offset > 0) {
429 fprintf(stderr, "Seeking on mtd device '%s' to: %zu\n", mtd, part_offset);
430 lseek(fd, part_offset, SEEK_SET);
431 }
432
433 indicate_writing(mtd);
434
435 w = e = 0;
436 for (;;) {
437 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
438 while (buflen < erasesize) {
439 r = read(imagefd, buf + buflen, erasesize - buflen);
440 if (r < 0) {
441 if ((errno == EINTR) || (errno == EAGAIN))
442 continue;
443 else {
444 perror("read");
445 break;
446 }
447 }
448
449 if (r == 0)
450 break;
451
452 buflen += r;
453 }
454
455 if (buflen == 0)
456 break;
457
458 if (buflen < erasesize) {
459 /* Pad block to eraseblock size */
460 memset(&buf[buflen], 0xff, erasesize - buflen);
461 buflen = erasesize;
462 }
463
464 if (skip > 0) {
465 skip -= buflen;
466 buflen = 0;
467 if (skip <= 0)
468 indicate_writing(mtd);
469
470 continue;
471 }
472
473 if (jffs2file && w >= jffs2_skip_bytes) {
474 if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
475 if (!quiet)
476 fprintf(stderr, "\b\b\b ");
477 if (quiet < 2)
478 fprintf(stderr, "\nAppending jffs2 data from %s to %s...", jffs2file, mtd);
479 /* got an EOF marker - this is the place to add some jffs2 data */
480 skip = mtd_replace_jffs2(mtd, fd, e, jffs2file);
481 jffs2_replaced = 1;
482
483 /* don't add it again */
484 jffs2file = NULL;
485
486 w += skip;
487 e += skip;
488 skip -= buflen;
489 buflen = 0;
490 offset = 0;
491 continue;
492 }
493 /* no EOF marker, make sure we figure out the last inode number
494 * before appending some data */
495 mtd_parse_jffs2data(buf, jffs2dir);
496 }
497
498 /* need to erase the next block before writing data to it */
499 if(!no_erase)
500 {
501 while (w + buflen > e - skip_bad_blocks) {
502 if (!quiet)
503 fprintf(stderr, "\b\b\b[e]");
504
505 if (mtd_block_is_bad(fd, e)) {
506 if (!quiet)
507 fprintf(stderr, "\nSkipping bad block at 0x%08x ", e);
508
509 skip_bad_blocks += erasesize;
510 e += erasesize;
511
512 // Move the file pointer along over the bad block.
513 lseek(fd, erasesize, SEEK_CUR);
514 continue;
515 }
516
517 if (mtd_erase_block(fd, e) < 0) {
518 if (next) {
519 if (w < e) {
520 write(fd, buf + offset, e - w);
521 offset = e - w;
522 }
523 w = 0;
524 e = 0;
525 close(fd);
526 mtd = next;
527 fprintf(stderr, "\b\b\b \n");
528 goto resume;
529 } else {
530 fprintf(stderr, "Failed to erase block\n");
531 exit(1);
532 }
533 }
534
535 /* erase the chunk */
536 e += erasesize;
537 }
538 }
539
540 if (!quiet)
541 fprintf(stderr, "\b\b\b[w]");
542
543 if ((result = write(fd, buf + offset, buflen)) < buflen) {
544 if (result < 0) {
545 fprintf(stderr, "Error writing image.\n");
546 exit(1);
547 } else {
548 fprintf(stderr, "Insufficient space.\n");
549 exit(1);
550 }
551 }
552 w += buflen;
553
554 buflen = 0;
555 offset = 0;
556 }
557
558 if (jffs2_replaced && trx_fixup) {
559 trx_fixup(fd, mtd);
560 }
561
562 if (!quiet)
563 fprintf(stderr, "\b\b\b\b ");
564
565 done:
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 }