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