mtd: fix compile error with musl
[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 #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%08x ", 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 done:
567 if (quiet < 2)
568 fprintf(stderr, "\n");
569
570 #ifdef FIS_SUPPORT
571 if (fis_layout) {
572 if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
573 fprintf(stderr, "Failed to update the FIS partition table\n");
574 }
575 #endif
576
577 close(fd);
578 return 0;
579 }
580
581 static void usage(void)
582 {
583 fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
584 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
585 "mtd recognizes these commands:\n"
586 " unlock unlock the device\n"
587 " refresh refresh mtd partition\n"
588 " erase erase all data on device\n"
589 " verify <imagefile>|- verify <imagefile> (use - for stdin) to device\n"
590 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
591 " jffs2write <file> append <file> to the jffs2 partition on the device\n");
592 if (mtd_fixtrx) {
593 fprintf(stderr,
594 " fixtrx fix the checksum in a trx header on first boot\n");
595 }
596 if (mtd_fixseama) {
597 fprintf(stderr,
598 " fixseama fix the checksum in a seama header on first boot\n");
599 }
600 fprintf(stderr,
601 "Following options are available:\n"
602 " -q quiet mode (once: no [w] on writing,\n"
603 " twice: no status messages)\n"
604 " -n write without first erasing the blocks\n"
605 " -r reboot after successful command\n"
606 " -f force write without trx checks\n"
607 " -e <device> erase <device> before executing the command\n"
608 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
609 " -j <name> integrate <file> into jffs2 data when writing an image\n"
610 " -s <number> skip the first n bytes when appending data to the jffs2 partiton, defaults to \"0\"\n"
611 " -p write beginning at partition offset\n");
612 if (mtd_fixtrx) {
613 fprintf(stderr,
614 " -o offset offset of the image header in the partition(for fixtrx)\n");
615 }
616 fprintf(stderr,
617 #ifdef FIS_SUPPORT
618 " -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
619 " alter the fis partition table to create new partitions replacing\n"
620 " the partitions provided as argument to the write command\n"
621 " (only valid together with the write command)\n"
622 #endif
623 "\n"
624 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
625 " mtd -r write linux.trx linux\n\n");
626 exit(1);
627 }
628
629 static void do_reboot(void)
630 {
631 fprintf(stderr, "Rebooting ...\n");
632 fflush(stderr);
633
634 /* try regular reboot method first */
635 system("/sbin/reboot");
636 sleep(2);
637
638 /* if we're still alive at this point, force the kernel to reboot */
639 syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
640 }
641
642 int main (int argc, char **argv)
643 {
644 int ch, i, boot, imagefd = 0, force, unlocked;
645 char *erase[MAX_ARGS], *device = NULL;
646 char *fis_layout = NULL;
647 size_t offset = 0, part_offset = 0;
648 enum {
649 CMD_ERASE,
650 CMD_WRITE,
651 CMD_UNLOCK,
652 CMD_JFFS2WRITE,
653 CMD_FIXTRX,
654 CMD_FIXSEAMA,
655 CMD_VERIFY,
656 } cmd = -1;
657
658 erase[0] = NULL;
659 boot = 0;
660 force = 0;
661 buflen = 0;
662 quiet = 0;
663 no_erase = 0;
664
665 while ((ch = getopt(argc, argv,
666 #ifdef FIS_SUPPORT
667 "F:"
668 #endif
669 "frnqe:d:s:j:p:o:")) != -1)
670 switch (ch) {
671 case 'f':
672 force = 1;
673 break;
674 case 'r':
675 boot = 1;
676 break;
677 case 'n':
678 no_erase = 1;
679 break;
680 case 'j':
681 jffs2file = optarg;
682 break;
683 case 's':
684 errno = 0;
685 jffs2_skip_bytes = strtoul(optarg, 0, 0);
686 if (errno) {
687 fprintf(stderr, "-s: illegal numeric string\n");
688 usage();
689 }
690 break;
691 case 'q':
692 quiet++;
693 break;
694 case 'e':
695 i = 0;
696 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
697 i++;
698
699 erase[i++] = optarg;
700 erase[i] = NULL;
701 break;
702 case 'd':
703 jffs2dir = optarg;
704 break;
705 case 'p':
706 errno = 0;
707 part_offset = strtoul(optarg, 0, 0);
708 if (errno) {
709 fprintf(stderr, "-p: illegal numeric string\n");
710 usage();
711 }
712 break;
713 case 'o':
714 if (!mtd_fixtrx) {
715 fprintf(stderr, "-o: is not available on this platform\n");
716 usage();
717 }
718 errno = 0;
719 offset = strtoul(optarg, 0, 0);
720 if (errno) {
721 fprintf(stderr, "-o: illegal numeric string\n");
722 usage();
723 }
724 break;
725 #ifdef FIS_SUPPORT
726 case 'F':
727 fis_layout = optarg;
728 break;
729 #endif
730 case '?':
731 default:
732 usage();
733 }
734 argc -= optind;
735 argv += optind;
736
737 if (argc < 2)
738 usage();
739
740 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
741 cmd = CMD_UNLOCK;
742 device = argv[1];
743 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
744 cmd = CMD_ERASE;
745 device = argv[1];
746 } else if (((strcmp(argv[0], "fixtrx") == 0) && (argc == 2)) && mtd_fixtrx) {
747 cmd = CMD_FIXTRX;
748 device = argv[1];
749 } else if (((strcmp(argv[0], "fixseama") == 0) && (argc == 2)) && mtd_fixseama) {
750 cmd = CMD_FIXSEAMA;
751 device = argv[1];
752 } else if ((strcmp(argv[0], "verify") == 0) && (argc == 3)) {
753 cmd = CMD_VERIFY;
754 imagefile = argv[1];
755 device = argv[2];
756 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
757 cmd = CMD_WRITE;
758 device = argv[2];
759
760 if (strcmp(argv[1], "-") == 0) {
761 imagefile = "<stdin>";
762 imagefd = 0;
763 } else {
764 imagefile = argv[1];
765 if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
766 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
767 exit(1);
768 }
769 }
770
771 if (!mtd_check(device)) {
772 fprintf(stderr, "Can't open device for writing!\n");
773 exit(1);
774 }
775 /* check trx file before erasing or writing anything */
776 if (!image_check(imagefd, device) && !force) {
777 fprintf(stderr, "Image check failed.\n");
778 exit(1);
779 }
780 } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
781 cmd = CMD_JFFS2WRITE;
782 device = argv[2];
783
784 imagefile = argv[1];
785 if (!mtd_check(device)) {
786 fprintf(stderr, "Can't open device for writing!\n");
787 exit(1);
788 }
789 } else {
790 usage();
791 }
792
793 sync();
794
795 i = 0;
796 unlocked = 0;
797 while (erase[i] != NULL) {
798 mtd_unlock(erase[i]);
799 mtd_erase(erase[i]);
800 if (strcmp(erase[i], device) == 0)
801 unlocked = 1;
802 i++;
803 }
804
805 switch (cmd) {
806 case CMD_UNLOCK:
807 if (!unlocked)
808 mtd_unlock(device);
809 break;
810 case CMD_VERIFY:
811 mtd_verify(device, imagefile);
812 break;
813 case CMD_ERASE:
814 if (!unlocked)
815 mtd_unlock(device);
816 mtd_erase(device);
817 break;
818 case CMD_WRITE:
819 if (!unlocked)
820 mtd_unlock(device);
821 mtd_write(imagefd, device, fis_layout, part_offset);
822 break;
823 case CMD_JFFS2WRITE:
824 if (!unlocked)
825 mtd_unlock(device);
826 mtd_write_jffs2(device, imagefile, jffs2dir);
827 break;
828 case CMD_FIXTRX:
829 if (mtd_fixtrx) {
830 mtd_fixtrx(device, offset);
831 }
832 case CMD_FIXSEAMA:
833 if (mtd_fixseama)
834 mtd_fixseama(device, 0);
835 break;
836 }
837
838 sync();
839
840 if (boot)
841 do_reboot();
842
843 return 0;
844 }