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