06d765ce8bea74aa2c9f0dbdb300e913f7c20e66
[openwrt/svn-archive/archive.git] / openwrt / package / mtd / 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@openwrt.org>
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 * The code is based on the linux-mtd examples.
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 #define TRX_MAGIC 0x30524448 /* "HDR0" */
46 #define BUFSIZE (16 * 1024)
47 #define MAX_ARGS 8
48
49 #define DEBUG
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 char buf[BUFSIZE];
60 int buflen;
61
62 int
63 trx_check(int imagefd, const char *mtd)
64 {
65 struct mtd_info_user mtdInfo;
66 int fd;
67 size_t count;
68 struct trx_header *trx = (struct trx_header *) buf;
69 struct stat trxstat;
70
71 buflen = read(imagefd, buf, sizeof(struct trx_header));
72 if (buflen < sizeof(struct trx_header)) {
73 fprintf(stderr, "Could not get trx header, file too small (%ld bytes)\n", buflen);
74 return 0;
75 }
76
77 if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
78 fprintf(stderr, "Bad trx header\n");
79 fprintf(stderr, "If this is a firmware in bin format, like some of the\n"
80 "original firmware files are, use following command to convert to trx:\n"
81 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
82 return 0;
83 }
84
85 /* check if image fits to mtd device */
86 fd = mtd_open(mtd, O_RDWR);
87 if(fd < 0) {
88 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
89 exit(1);
90 }
91
92 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
93 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
94 exit(1);
95 }
96
97 if(mtdInfo.size < trx->len) {
98 fprintf(stderr, "Image too big for partition: %s\n", mtd);
99 close(fd);
100 return 0;
101 }
102
103 return 1;
104 }
105
106 int mtd_check(char *mtd)
107 {
108 struct mtd_info_user mtdInfo;
109 int fd;
110
111 fd = mtd_open(mtd, O_RDWR);
112 if(fd < 0) {
113 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
114 return 0;
115 }
116
117 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
118 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
119 close(fd);
120 return 0;
121 }
122
123 close(fd);
124 return 1;
125 }
126
127 int
128 mtd_unlock(const char *mtd)
129 {
130 int fd;
131 struct mtd_info_user mtdInfo;
132 struct erase_info_user mtdLockInfo;
133
134 fd = mtd_open(mtd, O_RDWR);
135 if(fd < 0) {
136 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
137 exit(1);
138 }
139
140 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
141 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
142 close(fd);
143 exit(1);
144 }
145
146 fprintf(stderr, "Unlocking %s ...\n", mtd);
147 mtdLockInfo.start = 0;
148 mtdLockInfo.length = mtdInfo.size;
149 if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
150 close(fd);
151 return 0;
152 }
153
154 close(fd);
155 return 0;
156 }
157
158 int
159 mtd_open(const char *mtd, int flags)
160 {
161 FILE *fp;
162 char dev[PATH_MAX];
163 int i;
164
165 if ((fp = fopen("/proc/mtd", "r"))) {
166 while (fgets(dev, sizeof(dev), fp)) {
167 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
168 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
169 fclose(fp);
170 return open(dev, flags);
171 }
172 }
173 fclose(fp);
174 }
175
176 return open(mtd, flags);
177 }
178
179 int
180 mtd_erase(const char *mtd)
181 {
182 int fd;
183 struct mtd_info_user mtdInfo;
184 struct erase_info_user mtdEraseInfo;
185
186 fd = mtd_open(mtd, O_RDWR);
187 if(fd < 0) {
188 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
189 exit(1);
190 }
191
192 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
193 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
194 close(fd);
195 exit(1);
196 }
197
198 fprintf(stderr, "Erasing %s ...\n", mtd);
199 mtdEraseInfo.length = mtdInfo.erasesize;
200
201 for (mtdEraseInfo.start = 0;
202 mtdEraseInfo.start < mtdInfo.size;
203 mtdEraseInfo.start += mtdInfo.erasesize) {
204
205 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
206 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
207 fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
208 close(fd);
209 exit(1);
210 }
211 }
212
213 close(fd);
214 return 0;
215
216 }
217
218 int
219 mtd_write(int imagefd, const char *mtd)
220 {
221 int fd, i, result;
222 size_t r, w, e;
223 struct mtd_info_user mtdInfo;
224 struct erase_info_user mtdEraseInfo;
225
226 fd = mtd_open(mtd, O_RDWR);
227 if(fd < 0) {
228 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
229 exit(1);
230 }
231
232 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
233 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
234 close(fd);
235 exit(1);
236 }
237
238 r = w = e = 0;
239 fprintf(stderr, " [ ]");
240
241 for (;;) {
242 /* buffer may contain data already (from trx check) */
243 r = buflen;
244 r += read(imagefd, buf + buflen, BUFSIZE - buflen);
245 w += r;
246
247 /* EOF */
248 if (r <= 0) break;
249
250 /* need to erase the next block before writing data to it */
251 while (w > e) {
252 mtdEraseInfo.start = e;
253 mtdEraseInfo.length = mtdInfo.erasesize;
254
255 fprintf(stderr, "\b\b\b[e]");
256 /* erase the chunk */
257 if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
258 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
259 exit(1);
260 }
261 e += mtdInfo.erasesize;
262 }
263
264 fprintf(stderr, "\b\b\b[w]");
265
266 if ((result = write(fd, buf, r)) < r) {
267 if (result < 0) {
268 fprintf(stderr, "Error writing image.\n");
269 exit(1);
270 } else {
271 fprintf(stderr, "Insufficient space.\n");
272 exit(1);
273 }
274 }
275
276 buflen = 0;
277 }
278 fprintf(stderr, "\b\b\b\b");
279
280 return 0;
281 }
282
283 void usage(void)
284 {
285 fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
286 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
287 "mtd recognizes these commands:\n"
288 " unlock unlock the device\n"
289 " erase erase all data on device\n"
290 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
291 "Following options are available:\n"
292 " -r reboot after successful command\n"
293 " -f force write without trx checks\n"
294 " -e <device> erase <device> before executing the command\n\n"
295 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
296 " mtd -r write linux.trx linux\n\n");
297 exit(1);
298 }
299
300 int main (int argc, char **argv)
301 {
302 int ch, i, boot, unlock, imagefd, force;
303 char *erase[MAX_ARGS], *device, *imagefile;
304 enum {
305 CMD_ERASE,
306 CMD_WRITE,
307 CMD_UNLOCK
308 } cmd;
309
310 erase[0] = NULL;
311 boot = 0;
312 force = 0;
313 buflen = 0;
314
315 while ((ch = getopt(argc, argv, "fre:")) != -1)
316 switch (ch) {
317 case 'f':
318 force = 1;
319 break;
320 case 'r':
321 boot = 1;
322 break;
323 case 'e':
324 i = 0;
325 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
326 i++;
327
328 erase[i++] = optarg;
329 erase[i] = NULL;
330 break;
331
332 case '?':
333 default:
334 usage();
335 }
336 argc -= optind;
337 argv += optind;
338
339 if (argc < 2)
340 usage();
341
342 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
343 cmd = CMD_UNLOCK;
344 device = argv[1];
345 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
346 cmd = CMD_ERASE;
347 device = argv[1];
348 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
349 cmd = CMD_WRITE;
350 device = argv[2];
351
352 if (strcmp(argv[1], "-") == 0) {
353 imagefile = "<stdin>";
354 imagefd = 0;
355 } else {
356 imagefile = argv[1];
357 if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
358 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
359 exit(1);
360 }
361 }
362
363 if (system("grep Broadcom /proc/cpuinfo >&- >&-") == 0) {
364 /* check trx file before erasing or writing anything */
365 if (!trx_check(imagefd, device)) {
366 fprintf(stderr, "TRX check failed!\n");
367 if (!force)
368 exit(1);
369 }
370 } else {
371 if (!mtd_check(device)) {
372 fprintf(stderr, "Can't open device for writing!\n");
373 exit(1);
374 }
375 }
376 } else {
377 usage();
378 }
379
380 sync();
381
382 i = 0;
383 while (erase[i] != NULL) {
384 mtd_unlock(erase[i]);
385 mtd_erase(erase[i]);
386 i++;
387 }
388
389 mtd_unlock(device);
390
391 switch (cmd) {
392 case CMD_UNLOCK:
393 break;
394 case CMD_ERASE:
395 mtd_erase(device);
396 break;
397 case CMD_WRITE:
398 fprintf(stderr, "Writing from %s to %s ... ", imagefile, device);
399 mtd_write(imagefd, device);
400 fprintf(stderr, "\n");
401 break;
402 }
403
404 if (boot)
405 kill(1, 15); // send SIGTERM to init for reboot
406
407 return 0;
408 }