3e84fd696e35b89c869d2f6bd27201686c73d7cf
[openwrt/svn-archive/archive.git] / package / mtd / src / 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 <signal.h>
32 #include <sys/ioctl.h>
33 #include <sys/syscall.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <error.h>
37 #include <time.h>
38 #include <string.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/mount.h>
43 #include <sys/stat.h>
44 #include <sys/reboot.h>
45 #include <linux/reboot.h>
46 #include "mtd-api.h"
47 #include "mtd.h"
48
49 #define MAX_ARGS 8
50 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
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 static char *buf = NULL;
61 static char *imagefile = NULL;
62 static char *jffs2file = NULL, *jffs2dir = JFFS2_DEFAULT_DIR;
63 static int buflen = 0;
64 int quiet;
65 int mtdsize = 0;
66 int erasesize = 0;
67
68 int mtd_open(const char *mtd, bool block)
69 {
70 FILE *fp;
71 char dev[PATH_MAX];
72 int i;
73 int ret;
74 int flags = O_RDWR | O_SYNC;
75
76 if ((fp = fopen("/proc/mtd", "r"))) {
77 while (fgets(dev, sizeof(dev), fp)) {
78 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
79 snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
80 if ((ret=open(dev, flags))<0) {
81 snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
82 ret=open(dev, flags);
83 }
84 fclose(fp);
85 return ret;
86 }
87 }
88 fclose(fp);
89 }
90
91 return open(mtd, flags);
92 }
93
94 int mtd_check_open(const char *mtd)
95 {
96 struct mtd_info_user mtdInfo;
97 int fd;
98
99 fd = mtd_open(mtd, false);
100 if(fd < 0) {
101 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
102 return 0;
103 }
104
105 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
106 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
107 close(fd);
108 return 0;
109 }
110 mtdsize = mtdInfo.size;
111 erasesize = mtdInfo.erasesize;
112
113 return fd;
114 }
115
116 int mtd_erase_block(int fd, int offset)
117 {
118 struct erase_info_user mtdEraseInfo;
119
120 mtdEraseInfo.start = offset;
121 mtdEraseInfo.length = erasesize;
122 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
123 if (ioctl (fd, MEMERASE, &mtdEraseInfo) < 0) {
124 fprintf(stderr, "Erasing mtd failed.\n");
125 exit(1);
126 }
127 return 0;
128 }
129
130 int mtd_write_buffer(int fd, const char *buf, int offset, int length)
131 {
132 lseek(fd, offset, SEEK_SET);
133 write(fd, buf, length);
134 return 0;
135 }
136
137
138 static int
139 image_check(int imagefd, const char *mtd)
140 {
141 int ret = 1;
142 #ifdef target_brcm
143 ret = trx_check(imagefd, mtd, buf, &buflen);
144 #endif
145 return ret;
146 }
147
148 static int mtd_check(const char *mtd)
149 {
150 int fd;
151
152 fd = mtd_check_open(mtd);
153 if (!fd)
154 return 0;
155
156 if (!buf)
157 buf = malloc(erasesize);
158
159 close(fd);
160 return 1;
161 }
162
163 static int
164 mtd_unlock(const char *mtd)
165 {
166 int fd;
167 struct erase_info_user mtdLockInfo;
168
169 fd = mtd_check_open(mtd);
170 if(fd <= 0) {
171 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
172 exit(1);
173 }
174
175 if (quiet < 2)
176 fprintf(stderr, "Unlocking %s ...\n", mtd);
177
178 mtdLockInfo.start = 0;
179 mtdLockInfo.length = mtdsize;
180 if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
181 close(fd);
182 return 0;
183 }
184
185 close(fd);
186 return 0;
187 }
188
189 static int
190 mtd_erase(const char *mtd)
191 {
192 int fd;
193 struct erase_info_user mtdEraseInfo;
194
195 if (quiet < 2)
196 fprintf(stderr, "Erasing %s ...\n", mtd);
197
198 fd = mtd_check_open(mtd);
199 if(fd <= 0) {
200 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
201 exit(1);
202 }
203
204 mtdEraseInfo.length = erasesize;
205
206 for (mtdEraseInfo.start = 0;
207 mtdEraseInfo.start < mtdsize;
208 mtdEraseInfo.start += erasesize) {
209
210 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
211 if(ioctl(fd, MEMERASE, &mtdEraseInfo))
212 fprintf(stderr, "Failed to erase block on %s at 0x%x\n", mtd, mtdEraseInfo.start);
213 }
214
215 close(fd);
216 return 0;
217
218 }
219
220 static int
221 mtd_refresh(const char *mtd)
222 {
223 int fd;
224
225 if (quiet < 2)
226 fprintf(stderr, "Refreshing mtd partition %s ... ", mtd);
227
228 fd = mtd_check_open(mtd);
229 if(fd <= 0) {
230 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
231 exit(1);
232 }
233
234 if (ioctl(fd, MTDREFRESH, NULL)) {
235 fprintf(stderr, "Failed to refresh the MTD device\n");
236 close(fd);
237 exit(1);
238 }
239 close(fd);
240
241 if (quiet < 2)
242 fprintf(stderr, "\n");
243
244 return 0;
245 }
246
247 static int
248 mtd_write(int imagefd, const char *mtd)
249 {
250 int fd, result;
251 ssize_t r, w, e;
252
253 fd = mtd_check_open(mtd);
254 if(fd < 0) {
255 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
256 exit(1);
257 }
258
259 if (quiet < 2)
260 fprintf(stderr, "Writing from %s to %s ... ", imagefile, mtd);
261
262 r = w = e = 0;
263 if (!quiet)
264 fprintf(stderr, " [ ]");
265
266 for (;;) {
267 /* buffer may contain data already (from trx check) */
268 do {
269 r = read(imagefd, buf + buflen, erasesize - buflen);
270 if (r < 0) {
271 if ((errno == EINTR) || (errno == EAGAIN))
272 continue;
273 else {
274 perror("read");
275 break;
276 }
277 }
278
279 if (r == 0) {
280 fprintf(stderr, "No more data left\n");
281 break;
282 }
283
284 buflen += r;
285 } while (buflen < erasesize);
286
287 if (buflen == 0)
288 break;
289
290 if (jffs2file) {
291 if (memcmp(buf, JFFS2_EOF, sizeof(JFFS2_EOF)) == 0) {
292 if (!quiet)
293 fprintf(stderr, "\b\b\b ");
294 if (quiet < 2)
295 fprintf(stderr, "\nAppending jffs2 data to from %s to %s...", jffs2file, mtd);
296 /* got an EOF marker - this is the place to add some jffs2 data */
297 mtd_replace_jffs2(mtd, fd, e, jffs2file);
298 goto done;
299 }
300 /* no EOF marker, make sure we figure out the last inode number
301 * before appending some data */
302 mtd_parse_jffs2data(buf, jffs2dir);
303 }
304
305 /* need to erase the next block before writing data to it */
306 while (w + buflen > e) {
307 if (!quiet)
308 fprintf(stderr, "\b\b\b[e]");
309
310 mtd_erase_block(fd, e);
311
312 /* erase the chunk */
313 e += erasesize;
314 }
315
316 if (!quiet)
317 fprintf(stderr, "\b\b\b[w]");
318
319 if ((result = write(fd, buf, buflen)) < buflen) {
320 if (result < 0) {
321 fprintf(stderr, "Error writing image.\n");
322 exit(1);
323 } else {
324 fprintf(stderr, "Insufficient space.\n");
325 exit(1);
326 }
327 }
328 w += buflen;
329
330 buflen = 0;
331 }
332 if (!quiet)
333 fprintf(stderr, "\b\b\b\b");
334
335 done:
336 if (quiet < 2)
337 fprintf(stderr, "\n");
338
339 close(fd);
340 return 0;
341 }
342
343 static void usage(void)
344 {
345 fprintf(stderr, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
346 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
347 "mtd recognizes these commands:\n"
348 " unlock unlock the device\n"
349 " refresh refresh mtd partition\n"
350 " erase erase all data on device\n"
351 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
352 " jffs2write <file> append <file> to the jffs2 partition on the device\n"
353 "Following options are available:\n"
354 " -q quiet mode (once: no [w] on writing,\n"
355 " twice: no status messages)\n"
356 " -r reboot after successful command\n"
357 " -f force write without trx checks\n"
358 " -e <device> erase <device> before executing the command\n"
359 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
360 " -j <name> integrate <file> into jffs2 data when writing an image\n"
361 "\n"
362 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
363 " mtd -r write linux.trx linux\n\n");
364 exit(1);
365 }
366
367 static void do_reboot(void)
368 {
369 fprintf(stderr, "Rebooting ...\n");
370 fflush(stderr);
371
372 /* try regular reboot method first */
373 system("/sbin/reboot");
374 sleep(2);
375
376 /* if we're still alive at this point, force the kernel to reboot */
377 syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
378 }
379
380 int main (int argc, char **argv)
381 {
382 int ch, i, boot, imagefd = 0, force, unlocked;
383 char *erase[MAX_ARGS], *device = NULL;
384 enum {
385 CMD_ERASE,
386 CMD_WRITE,
387 CMD_UNLOCK,
388 CMD_REFRESH,
389 CMD_JFFS2WRITE
390 } cmd = -1;
391
392 erase[0] = NULL;
393 boot = 0;
394 force = 0;
395 buflen = 0;
396 quiet = 0;
397
398 while ((ch = getopt(argc, argv, "frqe:d:j:")) != -1)
399 switch (ch) {
400 case 'f':
401 force = 1;
402 break;
403 case 'r':
404 boot = 1;
405 break;
406 case 'j':
407 jffs2file = optarg;
408 break;
409 case 'q':
410 quiet++;
411 break;
412 case 'e':
413 i = 0;
414 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
415 i++;
416
417 erase[i++] = optarg;
418 erase[i] = NULL;
419 break;
420 case 'd':
421 jffs2dir = optarg;
422 break;
423 case '?':
424 default:
425 usage();
426 }
427 argc -= optind;
428 argv += optind;
429
430 if (argc < 2)
431 usage();
432
433 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
434 cmd = CMD_UNLOCK;
435 device = argv[1];
436 } else if ((strcmp(argv[0], "refresh") == 0) && (argc == 2)) {
437 cmd = CMD_REFRESH;
438 device = argv[1];
439 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
440 cmd = CMD_ERASE;
441 device = argv[1];
442 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
443 cmd = CMD_WRITE;
444 device = argv[2];
445
446 if (strcmp(argv[1], "-") == 0) {
447 imagefile = "<stdin>";
448 imagefd = 0;
449 } else {
450 imagefile = argv[1];
451 if ((imagefd = open(argv[1], O_RDONLY)) < 0) {
452 fprintf(stderr, "Couldn't open image file: %s!\n", imagefile);
453 exit(1);
454 }
455 }
456
457 if (!mtd_check(device)) {
458 fprintf(stderr, "Can't open device for writing!\n");
459 exit(1);
460 }
461 /* check trx file before erasing or writing anything */
462 if (!image_check(imagefd, device) && !force) {
463 fprintf(stderr, "Image check failed.\n");
464 exit(1);
465 }
466 } else if ((strcmp(argv[0], "jffs2write") == 0) && (argc == 3)) {
467 cmd = CMD_JFFS2WRITE;
468 device = argv[2];
469
470 imagefile = argv[1];
471 if (!mtd_check(device)) {
472 fprintf(stderr, "Can't open device for writing!\n");
473 exit(1);
474 }
475 } else {
476 usage();
477 }
478
479 sync();
480
481 i = 0;
482 unlocked = 0;
483 while (erase[i] != NULL) {
484 mtd_unlock(erase[i]);
485 mtd_erase(erase[i]);
486 if (strcmp(erase[i], device) == 0)
487 unlocked = 1;
488 i++;
489 }
490
491
492 switch (cmd) {
493 case CMD_UNLOCK:
494 if (!unlocked)
495 mtd_unlock(device);
496 break;
497 case CMD_ERASE:
498 if (!unlocked)
499 mtd_unlock(device);
500 mtd_erase(device);
501 break;
502 case CMD_WRITE:
503 if (!unlocked)
504 mtd_unlock(device);
505 mtd_write(imagefd, device);
506 break;
507 case CMD_JFFS2WRITE:
508 if (!unlocked)
509 mtd_unlock(device);
510 mtd_write_jffs2(device, imagefile, jffs2dir);
511 break;
512 case CMD_REFRESH:
513 mtd_refresh(device);
514 break;
515 }
516
517 sync();
518
519 if (boot)
520 do_reboot();
521
522 return 0;
523 }