add gmp, needed for openswan
[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 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * mtd utility for the openwrt project
21 * it is mainly code from the linux-mtd project, which accepts the same
22 * command line arguments as the broadcom utility
23 *
24 * $Id$
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 extern int mtd_update(const char *trxfile, const char *mtd);
66
67 int
68 mtd_unlock(const char *mtd)
69 {
70 int fd;
71 struct mtd_info_user mtdInfo;
72 struct erase_info_user mtdLockInfo;
73
74 fd = mtd_open(mtd, O_RDWR);
75 if(fd < 0) {
76 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
77 exit(1);
78 }
79
80 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
81 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
82 close(fd);
83 exit(1);
84 }
85
86 mtdLockInfo.start = 0;
87 mtdLockInfo.length = mtdInfo.size;
88 if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
89 fprintf(stderr, "Could not unlock MTD device: %s\n", mtd);
90 close(fd);
91 exit(1);
92 }
93
94 close(fd);
95 return 0;
96 }
97
98 int
99 mtd_open(const char *mtd, int flags)
100 {
101 FILE *fp;
102 char dev[PATH_MAX];
103 int i;
104
105 if ((fp = fopen("/proc/mtd", "r"))) {
106 while (fgets(dev, sizeof(dev), fp)) {
107 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
108 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
109 fclose(fp);
110 return open(dev, flags);
111 }
112 }
113 fclose(fp);
114 }
115
116 return open(mtd, flags);
117 }
118
119 int
120 mtd_erase(const char *mtd)
121 {
122 int fd;
123 struct mtd_info_user mtdInfo;
124 struct erase_info_user mtdEraseInfo;
125
126 fd = mtd_open(mtd, O_RDWR);
127 if(fd < 0) {
128 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
129 exit(1);
130 }
131
132 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
133 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
134 close(fd);
135 exit(1);
136 }
137
138 mtdEraseInfo.length = mtdInfo.erasesize;
139
140 for (mtdEraseInfo.start = 0;
141 mtdEraseInfo.start < mtdInfo.size;
142 mtdEraseInfo.start += mtdInfo.erasesize) {
143
144 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
145 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
146 fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
147 close(fd);
148 exit(1);
149 }
150 }
151
152 close(fd);
153 return 0;
154
155 }
156
157 int
158 mtd_write(const char *trxfile, const char *mtd)
159 {
160 int fd;
161 int trxfd;
162 int i;
163 size_t result,size,written;
164 struct mtd_info_user mtdInfo;
165 struct erase_info_user mtdEraseInfo;
166 struct stat trxstat;
167 unsigned char src[BUFSIZE],dest[BUFSIZE];
168
169 fd = mtd_open(mtd, O_RDWR);
170 if(fd < 0) {
171 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
172 exit(1);
173 }
174
175 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
176 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
177 close(fd);
178 exit(1);
179 }
180
181 trxfd = open(trxfile,O_RDONLY);
182 if(trxfd < 0) {
183 fprintf(stderr, "Could not open trx image: %s\n", trxfile);
184 exit(1);
185 }
186
187 if (fstat (trxfd,&trxstat) < 0) {
188 fprintf(stderr, "Could not get trx image file status: %s\n", trxfile);
189 close(trxfd);
190 exit(1);
191 }
192
193 if(mtdInfo.size < trxstat.st_size) {
194 fprintf(stderr, "Image too big for partition: %s\n", mtd);
195 close(trxfd);
196 exit(1);
197 }
198
199 mtdEraseInfo.start = 0;
200 mtdEraseInfo.length = trxstat.st_size & ~(mtdInfo.erasesize -1);
201 if(trxstat.st_size % mtdInfo.erasesize) mtdEraseInfo.length += mtdInfo.erasesize;
202
203 /* erase the chunk */
204 if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
205 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
206 exit(1);
207 }
208
209 size = trxstat.st_size;
210 i = BUFSIZE;
211 written = 0;
212
213 while (size) {
214 if (size < BUFSIZE) i = size;
215 read(trxfd,src,i);
216 result = write(fd,src,i);
217 if (i != result) {
218 if (result < 0) {
219 fprintf(stderr,"Error while writing image");
220 exit(1);
221 }
222 fprintf(stderr,"Error writing image");
223 exit(1);
224 }
225 written += i;
226 size -= i;
227 }
228
229 return 0;
230 }
231
232 int
233 mtd_update(const char *trxfile, const char *mtd)
234 {
235 if (mtd_erase("rootfs") != 0) {
236 fprintf(stderr, "Could not erase rootfs\n");
237 exit(1);
238 }
239 if (mtd_write(trxfile, mtd) != 0) {
240 fprintf(stderr, "Could not update %s with %s\n", mtd, trxfile);
241 exit(1);
242 }
243 return 0;
244 }
245
246 int main(int argc, char **argv) {
247 if(argc == 3 && strcasecmp(argv[1],"unlock")==0) {
248 printf("Unlocking %s ...\n",argv[2]);
249 return mtd_unlock(argv[2]);
250 }
251 if(argc == 3 && strcasecmp(argv[1],"erase")==0) {
252 printf("Erasing %s ...\n",argv[2]);
253 return mtd_erase(argv[2]);
254 }
255 if(argc == 4 && strcasecmp(argv[1],"write")==0) {
256 printf("Writing %s to %s ...\n",argv[2],argv[3]);
257 return mtd_write(argv[2],argv[3]);
258 }
259 if(argc == 4 && strcasecmp(argv[1],"update")==0) {
260 printf("Updating %s on %s ...\n",argv[2],argv[3]);
261 return mtd_update(argv[2],argv[3]);
262 }
263
264 printf("no valid command given\n");
265 printf("\nmtd: modify data within a Memory Technology Device.\n");
266 printf("Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>\n");
267 printf("Documented by Mike Strates [dumpedcore] <mike@dilaudid.net>\n");
268 printf("mtd has ABSOLUTELY NO WARRANTY and is licensed under the GNU GPL.\n");
269 printf("\nUsage: mtd [unlock|erase] device\n");
270 printf(" mtd [write|update] imagefile device\n");
271 printf("\n.. where device is in the format of mtdX (eg: mtd4) or its label.\n\n");
272 printf("unlock enable modification to device\n");
273 printf("erase erase all data on device\n");
274 printf("write write imagefile to device\n");
275 printf("update remove rootfs and update imagefile on device\n");
276 printf("\nExample: To write linux.trx to mtd4 labeled as linux\n");
277 printf("\n mtd unlock linux && mtd write linux.trx linux\n\n");
278 return -1;
279 }
280
This page took 0.051269 seconds and 5 git commands to generate.