finally move buildroot-ng to trunk
[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  *                        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 | O_SYNC);
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         close(fd);
117         return 1;
118 }
119
120 int
121 image_check(int imagefd, const char *mtd)
122 {
123         int fd, systype;
124         size_t count;
125         char *c;
126         FILE *f;
127
128         systype = SYSTYPE_UNKNOWN;
129         f = fopen("/proc/cpuinfo", "r");
130         while (!feof(f) && (fgets(buf, BUFSIZE - 1, f) != NULL)) {
131                 if ((strncmp(buf, "system type", 11) == 0) && (c = strchr(buf, ':'))) {
132                         c += 2;
133                         if (strncmp(c, "Broadcom BCM947XX", 17) == 0)
134                                 systype = SYSTYPE_BROADCOM;
135                 }
136         }
137         fclose(f);
138         
139         switch(systype) {
140                 case SYSTYPE_BROADCOM:
141                         return image_check_bcom(imagefd, mtd);
142                 default:
143                         return 1;
144         }
145 }
146
147 int mtd_check(char *mtd)
148 {
149         struct mtd_info_user mtdInfo;
150         int fd;
151
152         fd = mtd_open(mtd, O_RDWR | O_SYNC);
153         if(fd < 0) {
154                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
155                 return 0;
156         }
157
158         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
159                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
160                 close(fd);
161                 return 0;
162         }
163
164         close(fd);
165         return 1;
166 }
167
168 int
169 mtd_unlock(const char *mtd)
170 {
171         int fd;
172         struct mtd_info_user mtdInfo;
173         struct erase_info_user mtdLockInfo;
174
175         fd = mtd_open(mtd, O_RDWR | O_SYNC);
176         if(fd < 0) {
177                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
178                 exit(1);
179         }
180
181         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
182                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
183                 close(fd);
184                 exit(1);
185         }
186
187         mtdLockInfo.start = 0;
188         mtdLockInfo.length = mtdInfo.size;
189         if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
190                 close(fd);
191                 return 0;
192         }
193                 
194         close(fd);
195         return 0;
196 }
197
198 int
199 mtd_open(const char *mtd, int flags)
200 {
201         FILE *fp;
202         char dev[PATH_MAX];
203         int i;
204
205         if ((fp = fopen("/proc/mtd", "r"))) {
206                 while (fgets(dev, sizeof(dev), fp)) {
207                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
208                                 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
209                                 fclose(fp);
210                                 return open(dev, flags);
211                         }
212                 }
213                 fclose(fp);
214         }
215
216         return open(mtd, flags);
217 }
218
219 int
220 mtd_erase(const char *mtd)
221 {
222         int fd;
223         struct mtd_info_user mtdInfo;
224         struct erase_info_user mtdEraseInfo;
225
226         fd = mtd_open(mtd, O_RDWR | O_SYNC);
227         if(fd < 0) {
228                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
229                 exit(1);
230         }
231
232         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
233                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
234                 close(fd);
235                 exit(1);
236         }
237
238         mtdEraseInfo.length = mtdInfo.erasesize;
239
240         for (mtdEraseInfo.start = 0;
241                  mtdEraseInfo.start < mtdInfo.size;
242                  mtdEraseInfo.start += mtdInfo.erasesize) {
243                 
244                 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
245                 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
246                         fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
247                         close(fd);
248                         exit(1);
249                 }
250         }               
251
252         close(fd);
253         return 0;
254
255 }
256
257 int
258 mtd_write(int imagefd, const char *mtd, int quiet)
259 {
260         int fd, i, result;
261         size_t r, w, e;
262         struct mtd_info_user mtdInfo;
263         struct erase_info_user mtdEraseInfo;
264         int ret = 0;
265
266         fd = mtd_open(mtd, O_RDWR | O_SYNC);
267         if(fd < 0) {
268                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
269                 exit(1);
270         }
271
272         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
273                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
274                 close(fd);
275                 exit(1);
276         }
277                 
278         r = w = e = 0;
279         if (!quiet)
280                 fprintf(stderr, " [ ]");
281
282         for (;;) {
283                 /* buffer may contain data already (from trx check) */
284                 r = buflen;
285                 r += read(imagefd, buf + buflen, BUFSIZE - buflen);
286                 w += r;
287
288                 /* EOF */
289                 if (r <= 0) break;
290
291                 /* need to erase the next block before writing data to it */
292                 while (w > e) {
293                         mtdEraseInfo.start = e;
294                         mtdEraseInfo.length = mtdInfo.erasesize;
295
296                         if (!quiet)
297                                 fprintf(stderr, "\b\b\b[e]");
298                         /* erase the chunk */
299                         if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
300                                 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
301                                 exit(1);
302                         }
303                         e += mtdInfo.erasesize;
304                 }
305                 
306                 if (!quiet)
307                         fprintf(stderr, "\b\b\b[w]");
308                 
309                 if ((result = write(fd, buf, r)) < r) {
310                         if (result < 0) {
311                                 fprintf(stderr, "Error writing image.\n");
312                                 exit(1);
313                         } else {
314                                 fprintf(stderr, "Insufficient space.\n");
315                                 exit(1);
316                         }
317                 }
318                 
319                 buflen = 0;
320         }
321         if (!quiet)
322                 fprintf(stderr, "\b\b\b\b");
323
324         close(fd);
325         return 0;
326 }
327
328 void usage(void)
329 {
330         fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
331         "The device is in the format of mtdX (eg: mtd4) or its label.\n"
332         "mtd recognizes these commands:\n"
333         "        unlock                  unlock the device\n"
334         "        erase                   erase all data on device\n"
335         "        write <imagefile>|-     write <imagefile> (use - for stdin) to device\n"
336         "Following options are available:\n"
337         "        -q                      quiet mode (once: no [w] on writing,\n"
338         "                                           twice: no status messages)\n"
339         "        -r                      reboot after successful command\n"
340         "        -f                      force write without trx checks\n"
341         "        -e <device>             erase <device> before executing the command\n\n"
342         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
343         "         mtd -r write linux.trx linux\n\n");
344         exit(1);
345 }
346
347 int main (int argc, char **argv)
348 {
349         int ch, i, boot, unlock, imagefd, force, quiet, unlocked;
350         char *erase[MAX_ARGS], *device, *imagefile;
351         enum {
352                 CMD_ERASE,
353                 CMD_WRITE,
354                 CMD_UNLOCK
355         } cmd;
356         
357         erase[0] = NULL;
358         boot = 0;
359         force = 0;
360         buflen = 0;
361         quiet = 0;
362
363         while ((ch = getopt(argc, argv, "frqe:")) != -1)
364                 switch (ch) {
365                         case 'f':
366                                 force = 1;
367                                 break;
368                         case 'r':
369                                 boot = 1;
370                                 break;
371                         case 'q':
372                                 quiet++;
373                                 break;
374                         case 'e':
375                                 i = 0;
376                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
377                                         i++;
378                                         
379                                 erase[i++] = optarg;
380                                 erase[i] = NULL;
381                                 break;
382                         
383                         case '?':
384                         default:
385                                 usage();
386                 }
387         argc -= optind;
388         argv += optind;
389         
390         if (argc < 2)
391                 usage();
392
393         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
394                 cmd = CMD_UNLOCK;
395                 device = argv[1];
396         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
397                 cmd = CMD_ERASE;
398                 device = argv[1];
399         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
400                 cmd = CMD_WRITE;
401                 device = argv[2];
402         
403                 if (strcmp(argv[1], "-") == 0) {
404                         imagefile = "<stdin>";
405                         imagefd = 0;
406                 } else {
407                         imagefile = argv[1];
408                         if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
409                                 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
410                                 exit(1);
411                         }
412                 }
413         
414                 /* check trx file before erasing or writing anything */
415                 if (!image_check(imagefd, device)) {
416                         if ((quiet < 2) || !force)
417                                 fprintf(stderr, "TRX check failed!\n");
418                         if (!force)
419                                 exit(1);
420                 } else {
421                         if (!mtd_check(device)) {
422                                 fprintf(stderr, "Can't open device for writing!\n");
423                                 exit(1);
424                         }
425                 }
426         } else {
427                 usage();
428         }
429
430         sync();
431         
432         i = 0;
433         unlocked = 0;
434         while (erase[i] != NULL) {
435                 if (quiet < 2)
436                         fprintf(stderr, "Unlocking %s ...\n", erase[i]);
437                 mtd_unlock(erase[i]);
438                 if (quiet < 2)
439                         fprintf(stderr, "Erasing %s ...\n", erase[i]);
440                 mtd_erase(erase[i]);
441                 if (strcmp(erase[i], device) == 0)
442                         unlocked = 1;
443                 i++;
444         }
445         
446         if (!unlocked) {
447                 if (quiet < 2) 
448                         fprintf(stderr, "Unlocking %s ...\n", device);
449                 mtd_unlock(device);
450         }
451                 
452         switch (cmd) {
453                 case CMD_UNLOCK:
454                         break;
455                 case CMD_ERASE:
456                         if (quiet < 2)
457                                 fprintf(stderr, "Erasing %s ...\n", device);
458                         mtd_erase(device);
459                         break;
460                 case CMD_WRITE:
461                         if (quiet < 2)
462                                 fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
463                         mtd_write(imagefd, device, quiet);
464                         if (quiet < 2)
465                                 fprintf(stderr, "\n");
466                         break;
467         }
468
469         sync();
470         
471         if (boot)
472                 kill(1, 15); // send SIGTERM to init for reboot
473
474         return 0;
475 }