add dirclean targets, add jffs2root compile
[openwrt.git] / package / openwrt / mtd.c
1 /*
2  * mtd.c
3  *
4  * Copyright (C) 2005 Waldemar Brodkorb
5  *
6  * $Id$
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * mtd utility for the openwrt project
23  * it is mainly code from the linux-mtd project, which accepts the same
24  * command line arguments as the broadcom utility
25  * 
26  */
27
28 #include <limits.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <error.h>
36 #include <time.h>
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/mount.h>
41 #include <sys/stat.h>
42 #include <string.h>
43
44 #include <linux/mtd/mtd.h>
45
46 /* trx header */
47 #define TRX_MAGIC       0x30524448      /* "HDR0" */
48 #define TRX_VERSION     1
49 #define TRX_MAX_LEN     0x3A0000
50 #define TRX_NO_HEADER   1               /* Do not write TRX header */
51
52 struct trx_header {
53         uint32_t magic;                 /* "HDR0" */
54         uint32_t len;                   /* Length of file including header */
55         uint32_t crc32;                 /* 32-bit CRC from flag_version to end of file */
56         uint32_t flag_version;  /* 0:15 flags, 16:31 version */
57         uint32_t offsets[3];    /* Offsets of partitions from start of header */
58 };
59
60 #define BUFSIZE (10 * 1024)
61
62 extern int mtd_open(const char *mtd, int flags);
63 extern int mtd_erase(const char *mtd);
64 extern int mtd_write(const char *trxfile, const char *mtd);
65
66 int
67 mtd_unlock(const char *mtd)
68 {
69         int fd;
70         struct mtd_info_user mtdInfo;
71         struct erase_info_user mtdLockInfo;
72
73         fd = mtd_open(mtd, O_RDWR);
74         if(fd < 0) {
75                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
76                 exit(1);
77         }
78
79         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
80                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
81                 close(fd);
82                 exit(1);
83         }
84
85         mtdLockInfo.start = 0;
86         mtdLockInfo.length = mtdInfo.size;
87         if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
88                 fprintf(stderr, "Could not unlock MTD device: %s\n", mtd);
89                 close(fd);
90                 exit(1);
91         }
92                 
93         close(fd);
94         return 0;
95 }
96
97 int
98 mtd_open(const char *mtd, int flags)
99 {
100         FILE *fp;
101         char dev[PATH_MAX];
102         int i;
103
104         if ((fp = fopen("/proc/mtd", "r"))) {
105                 while (fgets(dev, sizeof(dev), fp)) {
106                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
107                                 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
108                                 fclose(fp);
109                                 return open(dev, flags);
110                         }
111                 }
112                 fclose(fp);
113         }
114
115         return open(mtd, flags);
116 }
117
118 int
119 mtd_erase(const char *mtd)
120 {
121         int fd;
122         struct mtd_info_user mtdInfo;
123         struct erase_info_user mtdEraseInfo;
124
125         fd = mtd_open(mtd, O_RDWR);
126         if(fd < 0) {
127                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
128                 exit(1);
129         }
130
131         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
132                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
133                 close(fd);
134                 exit(1);
135         }
136
137         mtdEraseInfo.length = mtdInfo.erasesize;
138
139         for (mtdEraseInfo.start = 0;
140                  mtdEraseInfo.start < mtdInfo.size;
141                  mtdEraseInfo.start += mtdInfo.erasesize) {
142                 
143                 if(ioctl(fd, MEMUNLOCK, &mtdEraseInfo)) {
144                         fprintf(stderr, "Could not unlock MTD device: %s\n", mtd);
145                         close(fd);
146                         exit(1);
147                 }
148                 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
149                         fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
150                         close(fd);
151                         exit(1);
152                 }
153         }               
154
155         close(fd);
156         return 0;
157
158 }
159
160 int
161 mtd_write(const char *trxfile, const char *mtd)
162 {
163         int fd;
164         int trxfd;
165         int i;
166         size_t result,size,written;
167         struct mtd_info_user mtdInfo;
168         struct erase_info_user mtdEraseInfo;
169         struct stat trxstat;
170         unsigned char src[BUFSIZE],dest[BUFSIZE];
171
172         fd = mtd_open(mtd, O_RDWR);
173         if(fd < 0) {
174                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
175                 exit(1);
176         }
177
178         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
179                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
180                 close(fd);
181                 exit(1);
182         }
183
184         trxfd = open(trxfile,O_RDONLY); 
185         if(trxfd < 0) {
186                 fprintf(stderr, "Could not open trx image: %s\n", trxfile);
187                 exit(1);
188         }
189
190         if (fstat (trxfd,&trxstat) < 0) {
191                 fprintf(stderr, "Could not get trx image file status: %s\n", trxfile);
192                 close(trxfd);
193                 exit(1);
194         }
195                 
196         if(mtdInfo.size < trxstat.st_size) {
197                 fprintf(stderr, "image to big for mtd partition: %s\n", mtd);
198                 close(trxfd);
199                 exit(1);
200         }       
201         
202         mtdEraseInfo.start = 0;
203         mtdEraseInfo.length = trxstat.st_size & ~(mtdInfo.erasesize -1);
204         if(trxstat.st_size % mtdInfo.erasesize) mtdEraseInfo.length += mtdInfo.erasesize;
205
206         /* erase the chunk */
207         if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
208                 fprintf(stderr, "erasing mtd failed: %s\n", mtd);
209                 exit(1);
210         }
211         
212         size = trxstat.st_size;
213         i = BUFSIZE;
214         written = 0;
215
216         while (size) {
217                 if (size < BUFSIZE) i = size;
218                 read(trxfd,src,i);
219                 result = write(fd,src,i);
220                 if (i != result) {
221                         if (result < 0) {
222                                 fprintf(stderr,"error while writing image");
223                                 exit(1);
224                         }
225                         fprintf(stderr,"error writing image");
226                         exit(1);
227                 }
228                 written += i;
229                 size -= i;
230         }
231         
232         return 0;
233 }
234
235 int main(int argc, char **argv) {
236         if(argc == 3 && strcasecmp(argv[1],"unlock")==0) {
237                 printf("Unlocking %s\n",argv[2]);
238                 return mtd_unlock(argv[2]);
239         }
240         if(argc == 3 && strcasecmp(argv[1],"erase")==0) {
241                 printf("Erasing %s\n",argv[2]);
242                 return mtd_erase(argv[2]);
243         }
244         if(argc == 4 && strcasecmp(argv[1],"write")==0) {
245                 printf("Writing %s to %s\n",argv[2],argv[3]);
246                 return mtd_write(argv[2],argv[3]);
247         }
248
249         printf("no valid command given\n");
250         return -1;
251 }
252