2 * mtd - simple memory technology device manipulation tool
4 * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>,
5 * Felix Fietkau <nbd@openwrt.org>
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.
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.
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.
23 * The code is based on the linux-mtd examples.
32 #include <sys/ioctl.h>
33 #include <sys/syscall.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/mount.h>
44 #include <sys/reboot.h>
45 #include <linux/reboot.h>
49 #ifdef target_brcm47xx
53 #define TRX_MAGIC 0x30524448 /* "HDR0" */
58 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
60 #define SYSTYPE_UNKNOWN 0
61 #define SYSTYPE_BROADCOM 1
65 uint32_t magic
; /* "HDR0" */
66 uint32_t len
; /* Length of file including header */
67 uint32_t crc32
; /* 32-bit CRC from flag_version to end of file */
68 uint32_t flag_version
; /* 0:15 flags, 16:31 version */
69 uint32_t offsets
[3]; /* Offsets of partitions from start of header */
72 static char *buf
= NULL
;
73 static char *imagefile
= NULL
;
74 static char *jffs2file
= NULL
, *jffs2dir
= JFFS2_DEFAULT_DIR
;
75 static int buflen
= 0;
80 int mtd_open(const char *mtd
)
86 int flags
= O_RDWR
| O_SYNC
;
88 if ((fp
= fopen("/proc/mtd", "r"))) {
89 while (fgets(dev
, sizeof(dev
), fp
)) {
90 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
91 snprintf(dev
, sizeof(dev
), "/dev/mtd/%d", i
);
92 if ((ret
=open(dev
, flags
))<0) {
93 snprintf(dev
, sizeof(dev
), "/dev/mtd%d", i
);
103 return open(mtd
, flags
);
106 int mtd_check_open(const char *mtd
)
108 struct mtd_info_user mtdInfo
;
113 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
117 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
118 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
122 mtdsize
= mtdInfo
.size
;
123 erasesize
= mtdInfo
.erasesize
;
128 int mtd_erase_block(int fd
, int offset
)
130 struct erase_info_user mtdEraseInfo
;
132 mtdEraseInfo
.start
= offset
;
133 mtdEraseInfo
.length
= erasesize
;
134 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
135 if (ioctl (fd
, MEMERASE
, &mtdEraseInfo
) < 0) {
136 fprintf(stderr
, "Erasing mtd failed.\n");
141 int mtd_write_buffer(int fd
, const char *buf
, int offset
, int length
)
143 lseek(fd
, offset
, SEEK_SET
);
144 write(fd
, buf
, length
);
150 image_check_brcm(int imagefd
, const char *mtd
)
152 struct trx_header
*trx
= (struct trx_header
*) buf
;
155 if (strcmp(mtd
, "linux") != 0)
158 buflen
= read(imagefd
, buf
, 32);
160 fprintf(stdout
, "Could not get image header, file too small (%ld bytes)\n", buflen
);
164 if (trx
->magic
!= TRX_MAGIC
|| trx
->len
< sizeof(struct trx_header
)) {
166 fprintf(stderr
, "Bad trx header\n");
167 fprintf(stderr
, "This is not the correct file format; refusing to flash.\n"
168 "Please specify the correct file or use -f to force.\n");
173 /* check if image fits to mtd device */
174 fd
= mtd_check_open(mtd
);
176 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
180 if(mtdsize
< trx
->len
) {
181 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
189 #endif /* target_brcm */
192 image_check(int imagefd
, const char *mtd
)
200 return image_check_brcm(imagefd
, mtd
);
204 static int mtd_check(const char *mtd
)
208 fd
= mtd_check_open(mtd
);
213 buf
= malloc(erasesize
);
220 mtd_unlock(const char *mtd
)
223 struct erase_info_user mtdLockInfo
;
225 fd
= mtd_check_open(mtd
);
227 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
232 fprintf(stderr
, "Unlocking %s ...\n", mtd
);
234 mtdLockInfo
.start
= 0;
235 mtdLockInfo
.length
= mtdsize
;
236 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
246 mtd_erase(const char *mtd
)
249 struct erase_info_user mtdEraseInfo
;
252 fprintf(stderr
, "Erasing %s ...\n", mtd
);
254 fd
= mtd_check_open(mtd
);
256 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
260 mtdEraseInfo
.length
= erasesize
;
262 for (mtdEraseInfo
.start
= 0;
263 mtdEraseInfo
.start
< mtdsize
;
264 mtdEraseInfo
.start
+= erasesize
) {
266 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
267 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
))
268 fprintf(stderr
, "Failed to erase block on %s at 0x%x\n", mtd
, mtdEraseInfo
.start
);
277 mtd_refresh(const char *mtd
)
282 fprintf(stderr
, "Refreshing mtd partition %s ... ", mtd
);
284 fd
= mtd_check_open(mtd
);
286 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
290 if (ioctl(fd
, MTDREFRESH
, NULL
)) {
291 fprintf(stderr
, "Failed to refresh the MTD device\n");
298 fprintf(stderr
, "\n");
304 mtd_write(int imagefd
, const char *mtd
)
308 struct erase_info_user mtdEraseInfo
;
311 fd
= mtd_check_open(mtd
);
313 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
318 fprintf(stderr
, "Writing from %s to %s ... ", imagefile
, mtd
);
322 fprintf(stderr
, " [ ]");
325 /* buffer may contain data already (from trx check) */
326 r
= read(imagefd
, buf
+ buflen
, erasesize
- buflen
);
333 if (memcmp(buf
, JFFS2_EOF
, sizeof(JFFS2_EOF
)) == 0) {
335 fprintf(stderr
, "\b\b\b ");
337 fprintf(stderr
, "\nAppending jffs2 data to from %s to %s...", jffs2file
, mtd
);
338 /* got an EOF marker - this is the place to add some jffs2 data */
339 mtd_replace_jffs2(fd
, e
, jffs2file
);
342 /* no EOF marker, make sure we figure out the last inode number
343 * before appending some data */
344 mtd_parse_jffs2data(buf
, jffs2dir
);
347 /* need to erase the next block before writing data to it */
348 while (w
+ buflen
> e
) {
350 fprintf(stderr
, "\b\b\b[e]");
352 mtd_erase_block(fd
, e
);
354 /* erase the chunk */
359 fprintf(stderr
, "\b\b\b[w]");
361 if ((result
= write(fd
, buf
, buflen
)) < buflen
) {
363 fprintf(stderr
, "Error writing image.\n");
366 fprintf(stderr
, "Insufficient space.\n");
372 /* not enough data - eof */
373 if (buflen
< erasesize
)
379 fprintf(stderr
, "\b\b\b\b");
383 fprintf(stderr
, "\n");
389 static void usage(void)
391 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
392 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
393 "mtd recognizes these commands:\n"
394 " unlock unlock the device\n"
395 " refresh refresh mtd partition\n"
396 " erase erase all data on device\n"
397 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
398 " jffs2write <file> append <file> to the jffs2 partition on the device\n"
399 "Following options are available:\n"
400 " -q quiet mode (once: no [w] on writing,\n"
401 " twice: no status messages)\n"
402 " -r reboot after successful command\n"
403 " -f force write without trx checks\n"
404 " -e <device> erase <device> before executing the command\n"
405 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
406 " -j <name> integrate <file> into jffs2 data when writing an image\n"
408 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
409 " mtd -r write linux.trx linux\n\n");
413 static void do_reboot(void)
415 fprintf(stderr
, "Rebooting ...\n");
418 /* try regular reboot method first */
419 system("/sbin/reboot");
422 /* if we're still alive at this point, force the kernel to reboot */
423 syscall(SYS_reboot
,LINUX_REBOOT_MAGIC1
,LINUX_REBOOT_MAGIC2
,LINUX_REBOOT_CMD_RESTART
,NULL
);
426 int main (int argc
, char **argv
)
428 int ch
, i
, boot
, unlock
, imagefd
, force
, unlocked
;
429 char *erase
[MAX_ARGS
], *device
;
444 while ((ch
= getopt(argc
, argv
, "frqe:d:j:")) != -1)
460 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
479 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
482 } else if ((strcmp(argv
[0], "refresh") == 0) && (argc
== 2)) {
485 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
488 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
492 if (strcmp(argv
[1], "-") == 0) {
493 imagefile
= "<stdin>";
497 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
498 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
503 if (!mtd_check(device
)) {
504 fprintf(stderr
, "Can't open device for writing!\n");
507 /* check trx file before erasing or writing anything */
508 if (!image_check(imagefd
, device
) && !force
) {
509 fprintf(stderr
, "Image check failed.\n");
512 } else if ((strcmp(argv
[0], "jffs2write") == 0) && (argc
== 3)) {
513 cmd
= CMD_JFFS2WRITE
;
517 if (!mtd_check(device
)) {
518 fprintf(stderr
, "Can't open device for writing!\n");
529 while (erase
[i
] != NULL
) {
530 mtd_unlock(erase
[i
]);
532 if (strcmp(erase
[i
], device
) == 0)
551 mtd_write(imagefd
, device
);
556 mtd_write_jffs2(device
, imagefile
, jffs2dir
);