Add ether-wake package
[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@vd-s.ath.cx>
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  * code is based on linux-mtd example code
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 (10 * 1024)
47 #define MAX_ARGS 8
48
49 struct trx_header {
50         uint32_t magic;         /* "HDR0" */
51         uint32_t len;           /* Length of file including header */
52         uint32_t crc32;         /* 32-bit CRC from flag_version to end of file */
53         uint32_t flag_version;  /* 0:15 flags, 16:31 version */
54         uint32_t offsets[3];    /* Offsets of partitions from start of header */
55 };
56
57 int
58 trx_check(const char *trxfile, const char *mtd)
59 {
60         struct mtd_info_user mtdInfo;
61         int trxfd, fd;
62         size_t count;
63         struct trx_header trx;
64         struct stat trxstat;
65
66         trxfd = open(trxfile,O_RDONLY); 
67         if(trxfd < 0) {
68                 fprintf(stderr, "Could not open trx image: %s\n", trxfile);
69                 exit(1);
70         }
71
72         if (fstat(trxfd,&trxstat) < 0) {
73                 fprintf(stderr, "Could not get trx image file status: %s\n", trxfile);
74                 close(trxfd);
75                 exit(1);
76         }
77
78         count = read(trxfd, &trx, sizeof(struct trx_header));
79         if (count < sizeof(struct trx_header)) {
80                 fprintf(stderr, "Could not trx header, file too small (%ld bytes)\n", count);
81                 close(trxfd);
82                 exit(1);
83         }
84
85         if (trx.magic != TRX_MAGIC || trx.len < sizeof(struct trx_header)) {
86                 fprintf(stderr, "Bad trx header\n");
87                 fprintf(stderr, "If this is a firmware in bin format, like some of the\n"
88                                 "original firmware files are, use following command to convert to trx:\n"
89                                 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
90                 close(trxfd);
91                 exit(1);
92         }
93         
94         lseek(trxfd, 0, SEEK_SET);
95
96         /* check if image fits to mtd device */
97
98         fd = mtd_open(mtd, O_RDWR);
99         if(fd < 0) {
100                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
101                 exit(1);
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                 exit(1);
108         }
109                 
110         if(mtdInfo.size < trxstat.st_size) {
111                 fprintf(stderr, "Image too big for partition: %s\n", mtd);
112                 close(trxfd);
113                 close(fd);
114                 exit(1);
115         }       
116         
117         printf("Writing %s to %s ...\n", trxfile, mtd);
118
119         close(fd);
120
121         return(trxfd);
122 }
123
124 int
125 mtd_unlock(const char *mtd)
126 {
127         int fd;
128         struct mtd_info_user mtdInfo;
129         struct erase_info_user mtdLockInfo;
130
131         fd = mtd_open(mtd, O_RDWR);
132         if(fd < 0) {
133                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
134                 exit(1);
135         }
136
137         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
138                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
139                 close(fd);
140                 exit(1);
141         }
142
143         printf("Unlocking %s ...\n", mtd);
144         mtdLockInfo.start = 0;
145         mtdLockInfo.length = mtdInfo.size;
146         if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
147                 close(fd);
148                 return 0;
149         }
150                 
151         close(fd);
152         return 0;
153 }
154
155 int
156 mtd_open(const char *mtd, int flags)
157 {
158         FILE *fp;
159         char dev[PATH_MAX];
160         int i;
161
162         if ((fp = fopen("/proc/mtd", "r"))) {
163                 while (fgets(dev, sizeof(dev), fp)) {
164                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
165                                 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
166                                 fclose(fp);
167                                 return open(dev, flags);
168                         }
169                 }
170                 fclose(fp);
171         }
172
173         return open(mtd, flags);
174 }
175
176 int
177 mtd_erase(const char *mtd)
178 {
179         int fd;
180         struct mtd_info_user mtdInfo;
181         struct erase_info_user mtdEraseInfo;
182
183         fd = mtd_open(mtd, O_RDWR);
184         if(fd < 0) {
185                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
186                 exit(1);
187         }
188
189         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
190                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
191                 close(fd);
192                 exit(1);
193         }
194
195         printf("Erasing %s ...\n", mtd);
196         mtdEraseInfo.length = mtdInfo.erasesize;
197
198         for (mtdEraseInfo.start = 0;
199                  mtdEraseInfo.start < mtdInfo.size;
200                  mtdEraseInfo.start += mtdInfo.erasesize) {
201                 
202                 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
203                 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
204                         fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
205                         close(fd);
206                         exit(1);
207                 }
208         }               
209
210         close(fd);
211         return 0;
212
213 }
214
215 int
216 mtd_write(int trxfd, const char *mtd)
217 {
218         int fd,i;
219         size_t result,size,written;
220         struct mtd_info_user mtdInfo;
221         struct erase_info_user mtdEraseInfo;
222         unsigned char src[BUFSIZE],dest[BUFSIZE];
223         struct stat trxstat;
224
225         if (fstat(trxfd,&trxstat) < 0) {
226                 fprintf(stderr, "Could not get trx image file status\n");
227                 close(trxfd);
228                 exit(1);
229         }
230
231         fd = mtd_open(mtd, O_RDWR);
232         if(fd < 0) {
233                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
234                 exit(1);
235         }
236
237         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
238                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
239                 close(fd);
240                 exit(1);
241         }
242                 
243         mtdEraseInfo.start = 0;
244         mtdEraseInfo.length = trxstat.st_size & ~(mtdInfo.erasesize -1);
245         if(trxstat.st_size % mtdInfo.erasesize) mtdEraseInfo.length += mtdInfo.erasesize;
246
247         /* erase the chunk */
248         if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
249                 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
250                 exit(1);
251         }
252         
253         size = trxstat.st_size;
254         i = BUFSIZE;
255         written = 0;
256
257         while (size) {
258                 if (size < BUFSIZE) i = size;
259                 read(trxfd,src,i);
260                 result = write(fd,src,i);
261                 if (i != result) {
262                         if (result < 0) {
263                                 fprintf(stderr,"Error while writing image");
264                                 exit(1);
265                         }
266                         fprintf(stderr,"Error writing image");
267                         exit(1);
268                 }
269                 written += i;
270                 size -= i;
271         }
272         
273         return 0;
274 }
275
276 void usage(void)
277 {
278         printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
279         "The device is in the format of mtdX (eg: mtd4) or its label.\n"
280         "mtd recognizes these commands:\n"
281         "       unlock                  unlock the device\n"
282         "       erase                   erase all data on device\n"
283         "       write <imagefile>       write imagefile to device\n"
284         "Following options are available:\n"
285         "       -r                      reboot after successful command\n"
286         "       -e <device>             erase <device> before executing the command\n\n"
287         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
288         "         mtd -r write linux.trx linux\n\n");
289         exit(1);
290 }
291
292 int main (int argc, char **argv)
293 {
294         int ch, i, boot, unlock, trxfd;
295         char *erase[MAX_ARGS], *device;
296         enum {
297                 CMD_ERASE,
298                 CMD_WRITE,
299                 CMD_UNLOCK
300         } cmd;
301         
302         erase[0] = NULL;
303         boot = 0;
304
305         while ((ch = getopt(argc, argv, "re:")) != -1)
306                 switch (ch) {
307                         case 'r':
308                                 boot = 1;
309                                 break;
310                         case 'e':
311                                 i = 0;
312                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
313                                         i++;
314                                         
315                                 erase[i++] = optarg;
316                                 erase[i] = NULL;
317                                 break;
318                         
319                         case '?':
320                         default:
321                                 usage();
322                 }
323         argc -= optind;
324         argv += optind;
325         
326         if (argc < 2)
327                 usage();
328
329         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
330                 cmd = CMD_UNLOCK;
331                 device = argv[1];
332         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
333                 cmd = CMD_ERASE;
334                 device = argv[1];
335         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
336                 cmd = CMD_WRITE;
337                 device = argv[2];
338                 /* check trx file before erasing or writing anything */
339                 trxfd = trx_check(argv[1], device);
340         } else {
341                 usage();
342         }
343
344         sync();
345
346         i = 0;
347         while (erase[i] != NULL) {
348                 mtd_unlock(erase[i]);
349                 mtd_erase(erase[i]);
350                 i++;
351         }
352         
353         mtd_unlock(device);
354
355         switch (cmd) {
356                 case CMD_UNLOCK:
357                         break;
358                 case CMD_ERASE:
359                         mtd_erase(device);
360                         break;
361                 case CMD_WRITE:
362                         mtd_write(trxfd, device);
363                         break;
364         }
365
366         if (boot)
367                 kill(1, 15); // send SIGTERM to init for reboot
368
369         return 0;
370 }