mtd: update copyright headers
[openwrt.git] / package / 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 <error.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-api.h"
45 #include "fis.h"
46 #include "mtd.h"
47
48 #define MAX_ARGS 8
49 #define JFFS2_DEFAULT_DIR       "" /* directory name without /, empty means root dir */
50
51 struct trx_header {
52         uint32_t magic;         /* "HDR0" */
53         uint32_t len;           /* Length of file including header */
54         uint32_t crc32;         /* 32-bit CRC from flag_version to end of file */
55         uint32_t flag_version;  /* 0:15 flags, 16:31 version */
56         uint32_t offsets[3];    /* Offsets of partitions from start of header */
57 };
58
59 static char *buf = NULL;
60 static char *imagefile = NULL;
61 static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
62 static int buflen = 0;
63 int quiet;
64 int mtdsize = 0;
65 int erasesize = 0;
66
67 int mtd_open(const char *mtd, bool block)
68 {
69         FILE *fp;
70         char dev[PATH_MAX];
71         int i;
72         int ret;
73         int flags = O_RDWR | O_SYNC;
74
75         if ((fp = fopen("/proc/mtd", "r"))) {
76                 while (fgets(dev, sizeof(dev), fp)) {
77                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
78                                 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
79                                 if ((ret=open(dev, flags))<0) {
80                                         snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
81                                         ret=open(dev, flags);
82                                 }
83                                 fclose(fp);
84                                 return ret;
85                         }
86                 }
87                 fclose(fp);
88         }
89
90         return open(mtd, flags);
91 }
92
93 int mtd_check_open(const char *mtd)
94 {
95         struct mtd_info_user mtdInfo;
96         int fd;
97
98         fd = mtd_open(mtd, false);
99         if(fd < 0) {
100                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
101                 return 0;
102         }
103
104         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
105                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
106                 close(fd);
107                 return 0;
108         }
109         mtdsize = mtdInfo.size;
110         erasesize = mtdInfo.erasesize;
111
112         return fd;
113 }
114
115 int mtd_erase_block(int fd, int offset)
116 {
117         struct erase_info_user mtdEraseInfo;
118
119         mtdEraseInfo.start = offset;
120         mtdEraseInfo.length = erasesize;
121         ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
122         if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0)
123                 return -1;
124
125         return 0;
126 }
127
128 int mtd_write_buffer(int fd, const char *buf, int offset, int length)
129 {
130         lseek(fd, offset, SEEK_SET);
131         write(fd, buf, length);
132         return 0;
133 }
134
135
136 static int
137 image_check(int imagefd, const char *mtd)
138 {
139         int ret = 1;
140 #ifdef target_brcm
141         ret = trx_check(imagefd, mtd, buf, &buflen);
142 #endif
143         return ret;
144 }
145
146 static int mtd_check(const char *mtd)
147 {
148         char *next = NULL;
149         char *str = NULL;
150         int fd;
151
152         if (strchr(mtd, ':')) {
153                 str = strdup(mtd);
154                 mtd = str;
155         }
156
157         do {
158                 next = strchr(mtd, ':');
159                 if (next) {
160                         *next = 0;
161                         next++;
162                 }
163
164                 fd = mtd_check_open(mtd);
165                 if (!fd)
166                         return 0;
167
168                 if (!buf)
169                         buf = malloc(erasesize);
170
171                 close(fd);
172                 mtd = next;
173         } while (next);
174
175         if (str)
176                 free(str);
177
178         return 1;
179 }
180
181 static int
182 mtd_unlock(const char *mtd)
183 {
184         struct erase_info_user mtdLockInfo;
185         char *next = NULL;
186         char *str = NULL;
187         int fd;
188
189         if (strchr(mtd, ':')) {
190                 str = strdup(mtd);
191                 mtd = str;
192         }
193
194         do {
195                 next = strchr(mtd, ':');
196                 if (next) {
197                         *next = 0;
198                         next++;
199                 }
200
201                 fd = mtd_check_open(mtd);
202                 if(fd <= 0) {
203                         fprintf(stderr, "Could not open mtd device: %s\n", mtd);
204                         exit(1);
205                 }
206
207                 if (quiet < 2)
208                         fprintf(stderr, "Unlocking %s ...\n", mtd);
209
210                 mtdLockInfo.start = 0;
211                 mtdLockInfo.length = mtdsize;
212                 ioctl(fd, MEMUNLOCK, &mtdLockInfo);
213                 close(fd);
214                 mtd = next;
215         } while (next);
216
217         if (str)
218                 free(str);
219
220         return 0;
221 }
222
223 static int
224 mtd_erase(const char *mtd)
225 {
226         int fd;
227         struct erase_info_user mtdEraseInfo;
228
229         if (quiet < 2)
230                 fprintf(stderr, "Erasing %s ...\n", mtd);
231
232         fd = mtd_check_open(mtd);
233         if(fd <= 0) {
234                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
235                 exit(1);
236         }
237
238         mtdEraseInfo.length = erasesize;
239
240         for (mtdEraseInfo.start = 0;
241                  mtdEraseInfo.start < mtdsize;
242                  mtdEraseInfo.start += erasesize) {
243
244                 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
245                 if(ioctl(fd, MEMERASE, &mtdEraseInfo))
246                         fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
247         }
248
249         close(fd);
250         return 0;
251
252 }
253
254 static int
255 mtd_refresh(const char *mtd)
256 {
257         int fd;
258
259         if (quiet < 2)
260                 fprintf(stderr, "Refreshing mtd partition %s ... ", mtd);
261
262         fd = mtd_check_open(mtd);
263         if(fd <= 0) {
264                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
265                 exit(1);
266         }
267
268         if (ioctl(fd, MTDREFRESH, NULL)) {
269                 fprintf(stderr, "Failed to refresh the MTD device\n");
270                 close(fd);
271                 exit(1);
272         }
273         close(fd);
274
275         if (quiet < 2)
276                 fprintf(stderr, "\n");
277
278         return 0;
279 }
280
281 static int
282 mtd_write(int imagefd, const char *mtd, char *fis_layout)
283 {
284         char *next = NULL;
285         char *str = NULL;
286         int fd, result;
287         ssize_t r, w, e;
288         uint32_t offset = 0;
289
290 #ifdef FIS_SUPPORT
291         static struct fis_part new_parts[MAX_ARGS];
292         static struct fis_part old_parts[MAX_ARGS];
293         int n_new = 0, n_old = 0;
294
295         if (fis_layout) {
296                 const char *tmp = mtd;
297                 char *word, *brkt;
298                 int ret;
299
300                 memset(&old_parts, 0, sizeof(old_parts));
301                 memset(&new_parts, 0, sizeof(new_parts));
302
303                 do {
304                         next = strchr(tmp, ':');
305                         if (!next)
306                                 next = (char *) tmp + strlen(tmp);
307
308                         memcpy(old_parts[n_old].name, tmp, next - tmp);
309
310                         n_old++;
311                         tmp = next + 1;
312                 } while(*next);
313
314                 for (word = strtok_r(fis_layout, ",", &brkt);
315                      word;
316                          word = strtok_r(NULL, ",", &brkt)) {
317
318                         tmp = strtok(word, ":");
319                         strncpy((char *) new_parts[n_new].name, tmp, sizeof(new_parts[n_new].name) - 1);
320
321                         tmp = strtok(NULL, ":");
322                         if (!tmp)
323                                 goto next;
324
325                         new_parts[n_new].size = strtoul(tmp, NULL, 0);
326
327                         tmp = strtok(NULL, ":");
328                         if (!tmp)
329                                 goto next;
330
331                         new_parts[n_new].loadaddr = strtoul(tmp, NULL, 16);
332 next:
333                         n_new++;
334                 }
335                 ret = fis_validate(old_parts, n_old, new_parts, n_new);
336                 if (ret < 0) {
337                         fprintf(stderr, "Failed to validate the new FIS partition table\n");
338                         exit(1);
339                 }
340                 if (ret == 0)
341                         fis_layout = NULL;
342         }
343 #endif
344
345         if (strchr(mtd, ':')) {
346                 str = strdup(mtd);
347                 mtd = str;
348         }
349
350         r = 0;
351
352 resume:
353         next = strchr(mtd, ':');
354         if (next) {
355                 *next = 0;
356                 next++;
357         }
358
359         fd = mtd_check_open(mtd);
360         if(fd < 0) {
361                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
362                 exit(1);
363         }
364
365         if (quiet < 2)
366                 fprintf(stderr, "Writing from %s to %s ... ", imagefile, mtd);
367
368         w = e = 0;
369         if (!quiet)
370                 fprintf(stderr, " [ ]");
371
372         for (;;) {
373                 /* buffer may contain data already (from trx check or last mtd partition write attempt) */
374                 while (buflen < erasesize) {
375                         r = read(imagefd, buf + buflen, erasesize - buflen);
376                         if (r < 0) {
377                                 if ((errno == EINTR) || (errno == EAGAIN))
378                                         continue;
379                                 else {
380                                         perror("read");
381                                         break;
382                                 }
383                         }
384
385                         if (r == 0)
386                                 break;
387
388                         buflen += r;
389                 }
390
391                 if (buflen == 0)
392                         break;
393
394                 if (jffs2file) {
395                         if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF) - 1) == 0) {
396                                 if (!quiet)
397                                         fprintf(stderr, "\b\b\b   ");
398                                 if (quiet < 2)
399                                         fprintf(stderr, "\nAppending jffs2 data to from %s to %s...", jffs2file, mtd);
400                                 /* got an EOF marker - this is the place to add some jffs2 data */
401                                 mtd_replace_jffs2(mtd, fd, e, jffs2file);
402                                 goto done;
403                         }
404                         /* no EOF marker, make sure we figure out the last inode number
405                          * before appending some data */
406                         mtd_parse_jffs2data(buf, jffs2dir);
407                 }
408
409                 /* need to erase the next block before writing data to it */
410                 while (w + buflen > e) {
411                         if (!quiet)
412                                 fprintf(stderr, "\b\b\b[e]");
413
414
415                         if (mtd_erase_block(fd, e) < 0) {
416                                 if (next) {
417                                         if (w < e) {
418                                                 write(fd, buf + offset, e - w);
419                                                 offset = e - w;
420                                         }
421                                         w = 0;
422                                         e = 0;
423                                         close(fd);
424                                         mtd = next;
425                                         fprintf(stderr, "\b\b\b   \n");
426                                         goto resume;
427                                 } else {
428                                         fprintf(stderr, "Failed to erase block\n");
429                                         exit(1);
430                                 }
431                         }
432
433                         /* erase the chunk */
434                         e += erasesize;
435                 }
436
437                 if (!quiet)
438                         fprintf(stderr, "\b\b\b[w]");
439
440                 if ((result = write(fd, buf + offset, buflen)) < buflen) {
441                         if (result < 0) {
442                                 fprintf(stderr, "Error writing image.\n");
443                                 exit(1);
444                         } else {
445                                 fprintf(stderr, "Insufficient space.\n");
446                                 exit(1);
447                         }
448                 }
449                 w += buflen;
450
451                 buflen = 0;
452                 offset = 0;
453         }
454
455         if (!quiet)
456                 fprintf(stderr, "\b\b\b\b    ");
457
458 done:
459         if (quiet < 2)
460                 fprintf(stderr, "\n");
461
462 #ifdef FIS_SUPPORT
463         if (fis_layout) {
464                 if (fis_remap(old_parts, n_old, new_parts, n_new) < 0)
465                         fprintf(stderr, "Failed to update the FIS partition table\n");
466         }
467 #endif
468
469         close(fd);
470         return 0;
471 }
472
473 static void usage(void)
474 {
475         fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>[:<device>...]\n\n"
476         "The device is in the format of mtdX (eg: mtd4) or its label.\n"
477         "mtd recognizes these commands:\n"
478         "        unlock                  unlock the device\n"
479         "        refresh                 refresh mtd partition\n"
480         "        erase                   erase all data on device\n"
481         "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
482         "        jffs2write <file>       append <file> to the jffs2 partition on the device\n"
483         "Following options are available:\n"
484         "        -q                      quiet mode (once: no [w] on writing,\n"
485         "                                           twice: no status messages)\n"
486         "        -r                      reboot after successful command\n"
487         "        -f                      force write without trx checks\n"
488         "        -e <device>             erase <device> before executing the command\n"
489         "        -d <name>               directory for jffs2write, defaults to \"tmp\"\n"
490         "        -j <name>               integrate <file> into jffs2 data when writing an image\n"
491 #ifdef FIS_SUPPORT
492         "        -F <part>[:<size>[:<entrypoint>]][,<part>...]\n"
493         "                                alter the fis partition table to create new partitions replacing\n"
494         "                                the partitions provided as argument to the write command\n"
495         "                                (only valid together with the write command)\n"
496 #endif
497         "\n"
498         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
499         "         mtd -r write linux.trx linux\n\n");
500         exit(1);
501 }
502
503 static void do_reboot(void)
504 {
505         fprintf(stderr, "Rebooting ...\n");
506         fflush(stderr);
507
508         /* try regular reboot method first */
509         system("/sbin/reboot");
510         sleep(2);
511
512         /* if we're still alive at this point, force the kernel to reboot */
513         syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
514 }
515
516 int main (int argc, char **argv)
517 {
518         int ch, i, boot, imagefd = 0, force, unlocked;
519         char *erase[MAX_ARGS], *device = NULL;
520         char *fis_layout = NULL;
521         enum {
522                 CMD_ERASE,
523                 CMD_WRITE,
524                 CMD_UNLOCK,
525                 CMD_REFRESH,
526                 CMD_JFFS2WRITE
527         } cmd = -1;
528
529         erase[0] = NULL;
530         boot = 0;
531         force = 0;
532         buflen = 0;
533         quiet = 0;
534
535         while ((ch = getopt(argc, argv,
536 #ifdef FIS_SUPPORT
537                         "F:"
538 #endif
539                         "frqe:d:j:")) != -1)
540                 switch (ch) {
541                         case 'f':
542                                 force = 1;
543                                 break;
544                         case 'r':
545                                 boot = 1;
546                                 break;
547                         case 'j':
548                                 jffs2file = optarg;
549                                 break;
550                         case 'q':
551                                 quiet++;
552                                 break;
553                         case 'e':
554                                 i = 0;
555                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
556                                         i++;
557
558                                 erase[i++] = optarg;
559                                 erase[i] = NULL;
560                                 break;
561                         case 'd':
562                                 jffs2dir = optarg;
563                                 break;
564 #ifdef FIS_SUPPORT
565                         case 'F':
566                                 fis_layout = optarg;
567                                 break;
568 #endif
569                         case '?':
570                         default:
571                                 usage();
572                 }
573         argc -= optind;
574         argv += optind;
575
576         if (argc < 2)
577                 usage();
578
579         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
580                 cmd = CMD_UNLOCK;
581                 device = argv[1];
582         } else if ((strcmp(argv[0], "refresh") == 0) && (argc == 2)) {
583                 cmd = CMD_REFRESH;
584                 device = argv[1];
585         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
586                 cmd = CMD_ERASE;
587                 device = argv[1];
588         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
589                 cmd = CMD_WRITE;
590                 device = argv[2];
591
592                 if (strcmp(argv[1], "-") == 0) {
593                         imagefile = "<stdin>";
594                         imagefd = 0;
595                 } else {
596                         imagefile = argv[1];
597                         if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
598                                 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
599                                 exit(1);
600                         }
601                 }
602
603                 if (!mtd_check(device)) {
604                         fprintf(stderr, "Can't open device for writing!\n");
605                         exit(1);
606                 }
607                 /* check trx file before erasing or writing anything */
608                 if (!image_check(imagefd, device) && !force) {
609                         fprintf(stderr, "Image check failed.\n");
610                         exit(1);
611                 }
612         } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
613                 cmd = CMD_JFFS2WRITE;
614                 device = argv[2];
615
616                 imagefile = argv[1];
617                 if (!mtd_check(device)) {
618                         fprintf(stderr, "Can't open device for writing!\n");
619                         exit(1);
620                 }
621         } else {
622                 usage();
623         }
624
625         sync();
626
627         i = 0;
628         unlocked = 0;
629         while (erase[i] != NULL) {
630                 mtd_unlock(erase[i]);
631                 mtd_erase(erase[i]);
632                 if (strcmp(erase[i], device) == 0)
633                         unlocked = 1;
634                 i++;
635         }
636
637         switch (cmd) {
638                 case CMD_UNLOCK:
639                         if (!unlocked)
640                                 mtd_unlock(device);
641                         break;
642                 case CMD_ERASE:
643                         if (!unlocked)
644                                 mtd_unlock(device);
645                         mtd_erase(device);
646                         break;
647                 case CMD_WRITE:
648                         if (!unlocked)
649                                 mtd_unlock(device);
650                         mtd_write(imagefd, device, fis_layout);
651                         break;
652                 case CMD_JFFS2WRITE:
653                         if (!unlocked)
654                                 mtd_unlock(device);
655                         mtd_write_jffs2(device, imagefile, jffs2dir);
656                         break;
657                 case CMD_REFRESH:
658                         mtd_refresh(device);
659                         break;
660         }
661
662         sync();
663
664         if (boot)
665                 do_reboot();
666
667         return 0;
668 }