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.
22 * The code is based on the linux-mtd examples.
31 #include <sys/ioctl.h>
32 #include <sys/syscall.h>
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/mount.h>
43 #include <sys/reboot.h>
44 #include <linux/reboot.h>
49 #define JFFS2_DEFAULT_DIR "" /* directory name without /, empty means root dir */
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 */
59 static char *buf
= NULL
;
60 static char *imagefile
= NULL
;
61 static char *jffs2file
= NULL
, *jffs2dir
= JFFS2_DEFAULT_DIR
;
62 static int buflen
= 0;
67 int mtd_open(const char *mtd
, bool block
)
73 int flags
= O_RDWR
| O_SYNC
;
75 if ((fp
= fopen("/proc/mtd", "r"))) {
76 while (fgets(dev
, sizeof(dev
), fp
)) {
77 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
78 snprintf(dev
, sizeof(dev
), "/dev/mtd%s/%d", (block
? "block" : ""), i
);
79 if ((ret
=open(dev
, flags
))<0) {
80 snprintf(dev
, sizeof(dev
), "/dev/mtd%s%d", (block
? "block" : ""), i
);
90 return open(mtd
, flags
);
93 int mtd_check_open(const char *mtd
)
95 struct mtd_info_user mtdInfo
;
98 fd
= mtd_open(mtd
, false);
100 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
104 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
105 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
109 mtdsize
= mtdInfo
.size
;
110 erasesize
= mtdInfo
.erasesize
;
115 int mtd_erase_block(int fd
, int offset
)
117 struct erase_info_user mtdEraseInfo
;
119 mtdEraseInfo
.start
= offset
;
120 mtdEraseInfo
.length
= erasesize
;
121 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
122 if (ioctl (fd
, MEMERASE
, &mtdEraseInfo
) < 0) {
123 fprintf(stderr
, "Erasing mtd failed.\n");
129 int mtd_write_buffer(int fd
, const char *buf
, int offset
, int length
)
131 lseek(fd
, offset
, SEEK_SET
);
132 write(fd
, buf
, length
);
138 image_check(int imagefd
, const char *mtd
)
142 ret
= trx_check(imagefd
, mtd
, buf
, &buflen
);
147 static int mtd_check(const char *mtd
)
151 fd
= mtd_check_open(mtd
);
156 buf
= malloc(erasesize
);
163 mtd_unlock(const char *mtd
)
166 struct erase_info_user mtdLockInfo
;
168 fd
= mtd_check_open(mtd
);
170 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
175 fprintf(stderr
, "Unlocking %s ...\n", mtd
);
177 mtdLockInfo
.start
= 0;
178 mtdLockInfo
.length
= mtdsize
;
179 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
189 mtd_erase(const char *mtd
)
192 struct erase_info_user mtdEraseInfo
;
195 fprintf(stderr
, "Erasing %s ...\n", mtd
);
197 fd
= mtd_check_open(mtd
);
199 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
203 mtdEraseInfo
.length
= erasesize
;
205 for (mtdEraseInfo
.start
= 0;
206 mtdEraseInfo
.start
< mtdsize
;
207 mtdEraseInfo
.start
+= erasesize
) {
209 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
210 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
))
211 fprintf(stderr
, "Failed to erase block on %s at 0x%x\n", mtd
, mtdEraseInfo
.start
);
220 mtd_refresh(const char *mtd
)
225 fprintf(stderr
, "Refreshing mtd partition %s ... ", mtd
);
227 fd
= mtd_check_open(mtd
);
229 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
233 if (ioctl(fd
, MTDREFRESH
, NULL
)) {
234 fprintf(stderr
, "Failed to refresh the MTD device\n");
241 fprintf(stderr
, "\n");
247 mtd_write(int imagefd
, const char *mtd
)
252 fd
= mtd_check_open(mtd
);
254 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
259 fprintf(stderr
, "Writing from %s to %s ... ", imagefile
, mtd
);
263 fprintf(stderr
, " [ ]");
266 /* buffer may contain data already (from trx check) */
268 r
= read(imagefd
, buf
+ buflen
, erasesize
- buflen
);
270 if ((errno
== EINTR
) || (errno
== EAGAIN
))
282 } while (buflen
< erasesize
);
288 if (memcmp(buf
, JFFS2_EOF
, sizeof(JFFS2_EOF
) - 1) == 0) {
290 fprintf(stderr
, "\b\b\b ");
292 fprintf(stderr
, "\nAppending jffs2 data to from %s to %s...", jffs2file
, mtd
);
293 /* got an EOF marker - this is the place to add some jffs2 data */
294 mtd_replace_jffs2(mtd
, fd
, e
, jffs2file
);
297 /* no EOF marker, make sure we figure out the last inode number
298 * before appending some data */
299 mtd_parse_jffs2data(buf
, jffs2dir
);
302 /* need to erase the next block before writing data to it */
303 while (w
+ buflen
> e
) {
305 fprintf(stderr
, "\b\b\b[e]");
307 mtd_erase_block(fd
, e
);
309 /* erase the chunk */
314 fprintf(stderr
, "\b\b\b[w]");
316 if ((result
= write(fd
, buf
, buflen
)) < buflen
) {
318 fprintf(stderr
, "Error writing image.\n");
321 fprintf(stderr
, "Insufficient space.\n");
330 fprintf(stderr
, "\b\b\b\b");
334 fprintf(stderr
, "\n");
340 static void usage(void)
342 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
343 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
344 "mtd recognizes these commands:\n"
345 " unlock unlock the device\n"
346 " refresh refresh mtd partition\n"
347 " erase erase all data on device\n"
348 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
349 " jffs2write <file> append <file> to the jffs2 partition on the device\n"
350 "Following options are available:\n"
351 " -q quiet mode (once: no [w] on writing,\n"
352 " twice: no status messages)\n"
353 " -r reboot after successful command\n"
354 " -f force write without trx checks\n"
355 " -e <device> erase <device> before executing the command\n"
356 " -d <name> directory for jffs2write, defaults to \"tmp\"\n"
357 " -j <name> integrate <file> into jffs2 data when writing an image\n"
359 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
360 " mtd -r write linux.trx linux\n\n");
364 static void do_reboot(void)
366 fprintf(stderr
, "Rebooting ...\n");
369 /* try regular reboot method first */
370 system("/sbin/reboot");
373 /* if we're still alive at this point, force the kernel to reboot */
374 syscall(SYS_reboot
,LINUX_REBOOT_MAGIC1
,LINUX_REBOOT_MAGIC2
,LINUX_REBOOT_CMD_RESTART
,NULL
);
377 int main (int argc
, char **argv
)
379 int ch
, i
, boot
, imagefd
= 0, force
, unlocked
;
380 char *erase
[MAX_ARGS
], *device
= NULL
;
395 while ((ch
= getopt(argc
, argv
, "frqe:d:j:")) != -1)
411 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
430 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
433 } else if ((strcmp(argv
[0], "refresh") == 0) && (argc
== 2)) {
436 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
439 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
443 if (strcmp(argv
[1], "-") == 0) {
444 imagefile
= "<stdin>";
448 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
449 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
454 if (!mtd_check(device
)) {
455 fprintf(stderr
, "Can't open device for writing!\n");
458 /* check trx file before erasing or writing anything */
459 if (!image_check(imagefd
, device
) && !force
) {
460 fprintf(stderr
, "Image check failed.\n");
463 } else if ((strcmp(argv
[0], "jffs2write") == 0) && (argc
== 3)) {
464 cmd
= CMD_JFFS2WRITE
;
468 if (!mtd_check(device
)) {
469 fprintf(stderr
, "Can't open device for writing!\n");
480 while (erase
[i
] != NULL
) {
481 mtd_unlock(erase
[i
]);
483 if (strcmp(erase
[i
], device
) == 0)
502 mtd_write(imagefd
, device
);
507 mtd_write_jffs2(device
, imagefile
, jffs2dir
);