enhance the new mtd -q option
[openwrt.git] / package / mtd / mtd.c
1 /*
2  * mtd - simple memory technology device manipulation tool
3  *
4  * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>,
5  *                        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
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * $Id$
22  *
23  * The code is based on the linux-mtd examples.
24  */
25
26 #include <limits.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <error.h>
34 #include <time.h>
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40 #include <sys/reboot.h>
41 #include <string.h>
42
43 #include <linux/mtd/mtd.h>
44
45 #define TRX_MAGIC       0x30524448      /* "HDR0" */
46 #define BUFSIZE (16 * 1024)
47 #define MAX_ARGS 8
48
49 #define DEBUG
50
51 #define SYSTYPE_UNKNOWN     0
52 #define SYSTYPE_BROADCOM    1
53 /* to be continued */
54
55 struct trx_header {
56         uint32_t magic;         /* "HDR0" */
57         uint32_t len;           /* Length of file including header */
58         uint32_t crc32;         /* 32-bit CRC from flag_version to end of file */
59         uint32_t flag_version;  /* 0:15 flags, 16:31 version */
60         uint32_t offsets[3];    /* Offsets of partitions from start of header */
61 };
62
63 char buf[BUFSIZE];
64 int buflen;
65
66 int
67 image_check_bcom(int imagefd, const char *mtd)
68 {
69         struct trx_header *trx = (struct trx_header *) buf;
70         struct mtd_info_user mtdInfo;
71         int fd;
72
73         buflen = read(imagefd, buf, 32);
74         if (buflen < 32) {
75                 fprintf(stdout, "Could not get image header, file too small (%ld bytes)\n", buflen);
76                 return 0;
77         }
78
79         switch(trx->magic) {
80                 case 0x47343557: /* W54G */
81                 case 0x53343557: /* W54S */
82                 case 0x73343557: /* W54s */
83                 case 0x46343557: /* W54F */
84                 case 0x55343557: /* W54U */
85                         /* ignore the first 32 bytes */
86                         buflen = read(imagefd, buf, sizeof(struct trx_header));
87                         break;
88         }
89         
90         if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
91                 fprintf(stderr, "Bad trx header\n");
92                 fprintf(stderr, "If this is a firmware in bin format, like some of the\n"
93                                 "original firmware files are, use following command to convert to trx:\n"
94                                 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
95                 return 0;
96         }
97
98         /* check if image fits to mtd device */
99         fd = mtd_open(mtd, O_RDWR);
100         if(fd < 0) {
101                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
102                 exit(1);
103         }
104
105         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
106                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
107                 exit(1);
108         }
109                 
110         if(mtdInfo.size < trx->len) {
111                 fprintf(stderr, "Image too big for partition: %s\n", mtd);
112                 close(fd);
113                 return 0;
114         }       
115         
116         return 1;
117 }
118
119 int
120 image_check(int imagefd, const char *mtd)
121 {
122         int fd, systype;
123         size_t count;
124         char *c;
125         FILE *f;
126
127         systype = SYSTYPE_UNKNOWN;
128         f = fopen("/proc/cpuinfo", "r");
129         while (!feof(f) && (fgets(buf, BUFSIZE - 1, f) != NULL)) {
130                 if ((strncmp(buf, "system type", 11) == 0) && (c = strchr(buf, ':'))) {
131                         c += 2;
132                         if (strncmp(c, "Broadcom BCM947XX", 17) == 0)
133                                 systype = SYSTYPE_BROADCOM;
134                 }
135         }
136         fclose(f);
137         
138         switch(systype) {
139                 case SYSTYPE_BROADCOM:
140                         return image_check_bcom(imagefd, mtd);
141                 default:
142                         return 1;
143         }
144 }
145
146 int mtd_check(char *mtd)
147 {
148         struct mtd_info_user mtdInfo;
149         int fd;
150
151         fd = mtd_open(mtd, O_RDWR);
152         if(fd < 0) {
153                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
154                 return 0;
155         }
156
157         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
158                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
159                 close(fd);
160                 return 0;
161         }
162
163         close(fd);
164         return 1;
165 }
166
167 int
168 mtd_unlock(const char *mtd)
169 {
170         int fd;
171         struct mtd_info_user mtdInfo;
172         struct erase_info_user mtdLockInfo;
173
174         fd = mtd_open(mtd, O_RDWR);
175         if(fd < 0) {
176                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
177                 exit(1);
178         }
179
180         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
181                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
182                 close(fd);
183                 exit(1);
184         }
185
186         mtdLockInfo.start = 0;
187         mtdLockInfo.length = mtdInfo.size;
188         if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
189                 close(fd);
190                 return 0;
191         }
192                 
193         close(fd);
194         return 0;
195 }
196
197 int
198 mtd_open(const char *mtd, int flags)
199 {
200         FILE *fp;
201         char dev[PATH_MAX];
202         int i;
203
204         if ((fp = fopen("/proc/mtd", "r"))) {
205                 while (fgets(dev, sizeof(dev), fp)) {
206                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
207                                 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
208                                 fclose(fp);
209                                 return open(dev, flags);
210                         }
211                 }
212                 fclose(fp);
213         }
214
215         return open(mtd, flags);
216 }
217
218 int
219 mtd_erase(const char *mtd)
220 {
221         int fd;
222         struct mtd_info_user mtdInfo;
223         struct erase_info_user mtdEraseInfo;
224
225         fd = mtd_open(mtd, O_RDWR);
226         if(fd < 0) {
227                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
228                 exit(1);
229         }
230
231         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
232                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
233                 close(fd);
234                 exit(1);
235         }
236
237         mtdEraseInfo.length = mtdInfo.erasesize;
238
239         for (mtdEraseInfo.start = 0;
240                  mtdEraseInfo.start < mtdInfo.size;
241                  mtdEraseInfo.start += mtdInfo.erasesize) {
242                 
243                 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
244                 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
245                         fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
246                         close(fd);
247                         exit(1);
248                 }
249         }               
250
251         close(fd);
252         return 0;
253
254 }
255
256 int
257 mtd_write(int imagefd, const char *mtd, int quiet)
258 {
259         int fd, i, result;
260         size_t r, w, e;
261         struct mtd_info_user mtdInfo;
262         struct erase_info_user mtdEraseInfo;
263
264         fd = mtd_open(mtd, O_RDWR);
265         if(fd < 0) {
266                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
267                 exit(1);
268         }
269
270         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
271                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
272                 close(fd);
273                 exit(1);
274         }
275                 
276         r = w = e = 0;
277         if (!quiet)
278                 fprintf(stderr, " [ ]");
279
280         for (;;) {
281                 /* buffer may contain data already (from trx check) */
282                 r = buflen;
283                 r += read(imagefd, buf + buflen, BUFSIZE - buflen);
284                 w += r;
285
286                 /* EOF */
287                 if (r <= 0) break;
288
289                 /* need to erase the next block before writing data to it */
290                 while (w > e) {
291                         mtdEraseInfo.start = e;
292                         mtdEraseInfo.length = mtdInfo.erasesize;
293
294                         if (!quiet)
295                                 fprintf(stderr, "\b\b\b[e]");
296                         /* erase the chunk */
297                         if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
298                                 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
299                                 exit(1);
300                         }
301                         e += mtdInfo.erasesize;
302                 }
303                 
304                 if (!quiet)
305                         fprintf(stderr, "\b\b\b[w]");
306                 
307                 if ((result = write(fd, buf, r)) < r) {
308                         if (result < 0) {
309                                 fprintf(stderr, "Error writing image.\n");
310                                 exit(1);
311                         } else {
312                                 fprintf(stderr, "Insufficient space.\n");
313                                 exit(1);
314                         }
315                 }
316                 
317                 buflen = 0;
318         }
319         if (!quiet)
320                 fprintf(stderr, "\b\b\b\b");
321         
322         return 0;
323 }
324
325 void usage(void)
326 {
327         fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
328         "The device is in the format of mtdX (eg: mtd4) or its label.\n"
329         "mtd recognizes these commands:\n"
330         "        unlock                  unlock the device\n"
331         "        erase                   erase all data on device\n"
332         "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
333         "Following options are available:\n"
334         "        -q                      quiet mode (once: no [w] on writing,\n"
335         "                                           twice: no status messages)\n"
336         "        -r                      reboot after successful command\n"
337         "        -f                      force write without trx checks\n"
338         "        -e <device>             erase <device> before executing the command\n\n"
339         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
340         "         mtd -r write linux.trx linux\n\n");
341         exit(1);
342 }
343
344 int main (int argc, char **argv)
345 {
346         int ch, i, boot, unlock, imagefd, force, quiet, unlocked;
347         char *erase[MAX_ARGS], *device, *imagefile;
348         enum {
349                 CMD_ERASE,
350                 CMD_WRITE,
351                 CMD_UNLOCK
352         } cmd;
353         
354         erase[0] = NULL;
355         boot = 0;
356         force = 0;
357         buflen = 0;
358         quiet = 0;
359
360         while ((ch = getopt(argc, argv, "frqe:")) != -1)
361                 switch (ch) {
362                         case 'f':
363                                 force = 1;
364                                 break;
365                         case 'r':
366                                 boot = 1;
367                                 break;
368                         case 'q':
369                                 quiet++;
370                                 break;
371                         case 'e':
372                                 i = 0;
373                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
374                                         i++;
375                                         
376                                 erase[i++] = optarg;
377                                 erase[i] = NULL;
378                                 break;
379                         
380                         case '?':
381                         default:
382                                 usage();
383                 }
384         argc -= optind;
385         argv += optind;
386         
387         if (argc < 2)
388                 usage();
389
390         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
391                 cmd = CMD_UNLOCK;
392                 device = argv[1];
393         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
394                 cmd = CMD_ERASE;
395                 device = argv[1];
396         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
397                 cmd = CMD_WRITE;
398                 device = argv[2];
399         
400                 if (strcmp(argv[1], "-") == 0) {
401                         imagefile = "<stdin>";
402                         imagefd = 0;
403                 } else {
404                         imagefile = argv[1];
405                         if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
406                                 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
407                                 exit(1);
408                         }
409                 }
410         
411                 /* check trx file before erasing or writing anything */
412                 if (!image_check(imagefd, device)) {
413                         if ((quiet < 2) || !force)
414                                 fprintf(stderr, "TRX check failed!\n");
415                         if (!force)
416                                 exit(1);
417                 } else {
418                         if (!mtd_check(device)) {
419                                 fprintf(stderr, "Can't open device for writing!\n");
420                                 exit(1);
421                         }
422                 }
423         } else {
424                 usage();
425         }
426
427         sync();
428         
429         i = 0;
430         unlocked = 0;
431         while (erase[i] != NULL) {
432                 if (quiet < 2)
433                         fprintf(stderr, "Unlocking %s ...\n", erase[i]);
434                 mtd_unlock(erase[i]);
435                 if (quiet < 2)
436                         fprintf(stderr, "Erasing %s ...\n", erase[i]);
437                 mtd_erase(erase[i]);
438                 if (strcmp(erase[i], device) == 0)
439                         unlocked = 1;
440                 i++;
441         }
442         
443         if (!unlocked) {
444                 if (quiet < 2) 
445                         fprintf(stderr, "Unlocking %s ...\n", device);
446                 mtd_unlock(device);
447         }
448                 
449         switch (cmd) {
450                 case CMD_UNLOCK:
451                         break;
452                 case CMD_ERASE:
453                         if (quiet < 2)
454                                 fprintf(stderr, "Erasing %s ...\n", device);
455                         mtd_erase(device);
456                         break;
457                 case CMD_WRITE:
458                         if (quiet < 2)
459                                 fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
460                         mtd_write(imagefd, device, quiet);
461                         if (quiet < 2)
462                                 fprintf(stderr, "\n");
463                         break;
464         }
465
466         if (boot)
467                 kill(1, 15); // send SIGTERM to init for reboot
468
469         return 0;
470 }