wlcompat probe bugfix
[openwrt.git] / package / openwrt / 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
24 #include <limits.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <error.h>
32 #include <time.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <sys/reboot.h>
39 #include <string.h>
40
41 #include <linux/mtd/mtd.h>
42
43 /* trx header */
44 #define TRX_MAGIC       0x30524448      /* "HDR0" */
45 #define TRX_VERSION     1
46 #define TRX_MAX_LEN     0x3A0000
47 #define TRX_NO_HEADER   1               /* Do not write TRX header */
48
49 #define MAX_ARGS 8
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 #define BUFSIZE (10 * 1024)
60
61
62 int
63 mtd_unlock(const char *mtd)
64 {
65         int fd;
66         struct mtd_info_user mtdInfo;
67         struct erase_info_user mtdLockInfo;
68
69         fd = mtd_open(mtd, O_RDWR);
70         if(fd < 0) {
71                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
72                 exit(1);
73         }
74
75         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
76                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
77                 close(fd);
78                 exit(1);
79         }
80
81         printf("Unlocking %s ...\n", mtd);
82         mtdLockInfo.start = 0;
83         mtdLockInfo.length = mtdInfo.size;
84         if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
85                 fprintf(stderr, "Could not unlock MTD device: %s\n", mtd);
86                 close(fd);
87                 return 0;
88         }
89                 
90         close(fd);
91         return 0;
92 }
93
94 int
95 mtd_open(const char *mtd, int flags)
96 {
97         FILE *fp;
98         char dev[PATH_MAX];
99         int i;
100
101         if ((fp = fopen("/proc/mtd", "r"))) {
102                 while (fgets(dev, sizeof(dev), fp)) {
103                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
104                                 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
105                                 fclose(fp);
106                                 return open(dev, flags);
107                         }
108                 }
109                 fclose(fp);
110         }
111
112         return open(mtd, flags);
113 }
114
115 int
116 mtd_erase(const char *mtd)
117 {
118         int fd;
119         struct mtd_info_user mtdInfo;
120         struct erase_info_user mtdEraseInfo;
121
122         fd = mtd_open(mtd, O_RDWR);
123         if(fd < 0) {
124                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
125                 exit(1);
126         }
127
128         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
129                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
130                 close(fd);
131                 exit(1);
132         }
133
134         printf("Erasing %s ...\n", mtd);
135         mtdEraseInfo.length = mtdInfo.erasesize;
136
137         for (mtdEraseInfo.start = 0;
138                  mtdEraseInfo.start < mtdInfo.size;
139                  mtdEraseInfo.start += mtdInfo.erasesize) {
140                 
141                 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
142                 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
143                         fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
144                         close(fd);
145                         exit(1);
146                 }
147         }               
148
149         close(fd);
150         return 0;
151
152 }
153
154 int
155 mtd_write(const char *trxfile, const char *mtd)
156 {
157         int fd;
158         int trxfd;
159         int i;
160         size_t result,size,written;
161         struct mtd_info_user mtdInfo;
162         struct erase_info_user mtdEraseInfo;
163         struct stat trxstat;
164         unsigned char src[BUFSIZE],dest[BUFSIZE];
165
166         fd = mtd_open(mtd, O_RDWR);
167         if(fd < 0) {
168                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
169                 exit(1);
170         }
171
172         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
173                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
174                 close(fd);
175                 exit(1);
176         }
177
178         trxfd = open(trxfile,O_RDONLY); 
179         if(trxfd < 0) {
180                 fprintf(stderr, "Could not open trx image: %s\n", trxfile);
181                 exit(1);
182         }
183
184         if (fstat (trxfd,&trxstat) < 0) {
185                 fprintf(stderr, "Could not get trx image file status: %s\n", trxfile);
186                 close(trxfd);
187                 exit(1);
188         }
189                 
190         if(mtdInfo.size < trxstat.st_size) {
191                 fprintf(stderr, "Image too big for partition: %s\n", mtd);
192                 close(trxfd);
193                 exit(1);
194         }       
195         
196         printf("Writing %s to %s ...\n", trxfile, mtd);
197
198         mtdEraseInfo.start = 0;
199         mtdEraseInfo.length = trxstat.st_size & ~(mtdInfo.erasesize -1);
200         if(trxstat.st_size % mtdInfo.erasesize) mtdEraseInfo.length += mtdInfo.erasesize;
201
202         /* erase the chunk */
203         if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
204                 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
205                 exit(1);
206         }
207         
208         size = trxstat.st_size;
209         i = BUFSIZE;
210         written = 0;
211
212         while (size) {
213                 if (size < BUFSIZE) i = size;
214                 read(trxfd,src,i);
215                 result = write(fd,src,i);
216                 if (i != result) {
217                         if (result < 0) {
218                                 fprintf(stderr,"Error while writing image");
219                                 exit(1);
220                         }
221                         fprintf(stderr,"Error writing image");
222                         exit(1);
223                 }
224                 written += i;
225                 size -= i;
226         }
227         
228         return 0;
229 }
230
231 void usage(void)
232 {
233         printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
234         "The device is in the format of mtdX (eg: mtd4) or its label.\n"
235         "mtd recognizes these commands:\n"
236         "       unlock                  unlock the device\n"
237         "       erase                   erase all data on device\n"
238         "       write <imagefile>       write imagefile to device\n"
239         "Following options are available:\n"
240         "       -r                      reboot after successful command\n"
241         "       -e <device>             erase <device> before executing the command\n\n"
242         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
243         "         mtd -r write linux.trx linux\n\n");
244         exit(1);
245 }
246
247 int main (int argc, char **argv)
248 {
249         int ch, i, boot, unlock;
250         char *erase[MAX_ARGS], *device;
251         enum {
252                 CMD_ERASE,
253                 CMD_WRITE,
254                 CMD_UNLOCK
255         } cmd;
256         
257         erase[0] = NULL;
258         boot = 0;
259
260         while ((ch = getopt(argc, argv, "re:")) != -1)
261                 switch (ch) {
262                         case 'r':
263                                 boot = 1;
264                                 break;
265                         case 'e':
266                                 i = 0;
267                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
268                                         i++;
269                                         
270                                 erase[i++] = optarg;
271                                 erase[i] = NULL;
272                                 break;
273                         
274                         case '?':
275                         default:
276                                 usage();
277                 }
278         argc -= optind;
279         argv += optind;
280         
281         if (argc < 2)
282                 usage();
283
284         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
285                 cmd = CMD_UNLOCK;
286                 device = argv[1];
287         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
288                 cmd = CMD_ERASE;
289                 device = argv[1];
290         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
291                 cmd = CMD_WRITE;
292                 device = argv[2];
293         } else {
294                 usage();
295         }
296
297         sync();
298
299         i = 0;
300         while (erase[i] != NULL) {
301                 mtd_unlock(erase[i]);
302                 mtd_erase(erase[i]);
303                 i++;
304         }
305         
306         mtd_unlock(device);
307
308         switch (cmd) {
309                 case CMD_UNLOCK:
310                         break;
311                 case CMD_ERASE:
312                         mtd_erase(device);
313                         break;
314                 case CMD_WRITE:
315                         mtd_write(argv[1], device);
316                         break;
317         }
318
319         if (boot)
320                 kill(1, 15); // send SIGTERM to init for reboot
321
322         return 0;
323 }