6771b3c2175305a24ad62d438cd8bfac2237d07b
[openwrt.git] / openwrt / 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         "       -u                      unlock device before accessing it\n"
241         "       -r                      reboot after successful command\n"
242         "       -e <device>             erase <device> before executing the command\n\n"
243         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
244         "         mtd -r write linux.trx linux\n\n");
245         exit(1);
246 }
247
248 int main (int argc, char **argv)
249 {
250         int ch, i, boot, unlock;
251         char *erase[MAX_ARGS], *device;
252         enum {
253                 CMD_ERASE,
254                 CMD_WRITE,
255                 CMD_UNLOCK
256         } cmd;
257         
258         erase[0] = NULL;
259         boot = 0;
260         unlock = 0;
261
262         while ((ch = getopt(argc, argv, "ure:")) != -1)
263                 switch (ch) {
264                         case 'u':
265                                 unlock = 1;
266                                 break;
267                         case 'r':
268                                 boot = 1;
269                                 break;
270                         case 'e':
271                                 i = 0;
272                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
273                                         i++;
274                                         
275                                 erase[i++] = optarg;
276                                 erase[i] = NULL;
277                                 break;
278                         
279                         case '?':
280                         default:
281                                 usage();
282                 }
283         argc -= optind;
284         argv += optind;
285         
286         if (argc < 2)
287                 usage();
288
289         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
290                 cmd = CMD_UNLOCK;
291                 device = argv[1];
292         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
293                 cmd = CMD_ERASE;
294                 device = argv[1];
295         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
296                 cmd = CMD_WRITE;
297                 device = argv[2];
298         } else {
299                 usage();
300         }
301
302         sync();
303
304         i = 0;
305         while (erase[i] != NULL) {
306                 mtd_unlock(erase[i]);
307                 mtd_erase(erase[i]);
308                 i++;
309         }
310         
311         if (unlock) 
312                 mtd_unlock(device);
313
314         switch (cmd) {
315                 case CMD_UNLOCK:
316                         mtd_unlock(device);
317                         break;
318                 case CMD_ERASE:
319                         mtd_erase(device);
320                         break;
321                 case CMD_WRITE:
322                         mtd_write(argv[1], device);
323                         break;
324         }
325
326         if (boot)
327                 kill(1, 15); // send SIGTERM to init for reboot
328
329         return 0;
330 }