2 * mtd - simple memory technology device manipulation tool
4 * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>,
5 * Felix Fietkau <nbd@vd-s.ath.cx>
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 * code is based on linux-mtd example code
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/mount.h>
40 #include <sys/reboot.h>
43 #include <linux/mtd/mtd.h>
45 #define TRX_MAGIC 0x30524448 /* "HDR0" */
46 #define BUFSIZE (10 * 1024)
50 uint32_t magic
; /* "HDR0" */
51 uint32_t len
; /* Length of file including header */
52 uint32_t crc32
; /* 32-bit CRC from flag_version to end of file */
53 uint32_t flag_version
; /* 0:15 flags, 16:31 version */
54 uint32_t offsets
[3]; /* Offsets of partitions from start of header */
58 mtd_unlock(const char *mtd
)
61 struct mtd_info_user mtdInfo
;
62 struct erase_info_user mtdLockInfo
;
64 fd
= mtd_open(mtd
, O_RDWR
);
66 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
70 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
71 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
76 printf("Unlocking %s ...\n", mtd
);
77 mtdLockInfo
.start
= 0;
78 mtdLockInfo
.length
= mtdInfo
.size
;
79 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
89 mtd_open(const char *mtd
, int flags
)
95 if ((fp
= fopen("/proc/mtd", "r"))) {
96 while (fgets(dev
, sizeof(dev
), fp
)) {
97 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
98 snprintf(dev
, sizeof(dev
), "/dev/mtd/%d", i
);
100 return open(dev
, flags
);
106 return open(mtd
, flags
);
110 mtd_erase(const char *mtd
)
113 struct mtd_info_user mtdInfo
;
114 struct erase_info_user mtdEraseInfo
;
116 fd
= mtd_open(mtd
, O_RDWR
);
118 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
122 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
123 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
128 printf("Erasing %s ...\n", mtd
);
129 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
131 for (mtdEraseInfo
.start
= 0;
132 mtdEraseInfo
.start
< mtdInfo
.size
;
133 mtdEraseInfo
.start
+= mtdInfo
.erasesize
) {
135 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
136 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
)) {
137 fprintf(stderr
, "Could not erase MTD device: %s\n", mtd
);
149 mtd_write(const char *trxfile
, const char *mtd
)
152 struct trx_header trx
;
153 size_t count
,result
,size
,written
;
154 struct mtd_info_user mtdInfo
;
155 struct erase_info_user mtdEraseInfo
;
157 unsigned char src
[BUFSIZE
],dest
[BUFSIZE
];
159 trxfd
= open(trxfile
,O_RDONLY
);
161 fprintf(stderr
, "Could not open trx image: %s\n", trxfile
);
165 if (fstat(trxfd
,&trxstat
) < 0) {
166 fprintf(stderr
, "Could not get trx image file status: %s\n", trxfile
);
171 count
= read(trxfd
, &trx
, sizeof(struct trx_header
));
172 if (count
< sizeof(struct trx_header
)) {
173 fprintf(stderr
, "Could not trx header, file too small (%ld bytes)\n", count
);
178 if (trx
.magic
!= TRX_MAGIC
|| trx
.len
< sizeof(struct trx_header
)) {
179 fprintf(stderr
, "Bad trx header\n");
180 fprintf(stderr
, "If this is a firmware in bin format, like some of the\n"
181 "original firmware files are, use following command to convert to trx:\n"
182 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
187 lseek(trxfd
, 0, SEEK_SET
);
189 fd
= mtd_open(mtd
, O_RDWR
);
191 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
195 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
196 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
201 if(mtdInfo
.size
< trxstat
.st_size
) {
202 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
207 printf("Writing %s to %s ...\n", trxfile
, mtd
);
209 mtdEraseInfo
.start
= 0;
210 mtdEraseInfo
.length
= trxstat
.st_size
& ~(mtdInfo
.erasesize
-1);
211 if(trxstat
.st_size
% mtdInfo
.erasesize
) mtdEraseInfo
.length
+= mtdInfo
.erasesize
;
213 /* erase the chunk */
214 if (ioctl (fd
,MEMERASE
,&mtdEraseInfo
) < 0) {
215 fprintf(stderr
, "Erasing mtd failed: %s\n", mtd
);
219 size
= trxstat
.st_size
;
224 if (size
< BUFSIZE
) i
= size
;
226 result
= write(fd
,src
,i
);
229 fprintf(stderr
,"Error while writing image");
232 fprintf(stderr
,"Error writing image");
244 printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
245 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
246 "mtd recognizes these commands:\n"
247 " unlock unlock the device\n"
248 " erase erase all data on device\n"
249 " write <imagefile> write imagefile to device\n"
250 "Following options are available:\n"
251 " -r reboot after successful command\n"
252 " -e <device> erase <device> before executing the command\n\n"
253 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
254 " mtd -r write linux.trx linux\n\n");
258 int main (int argc
, char **argv
)
260 int ch
, i
, boot
, unlock
;
261 char *erase
[MAX_ARGS
], *device
;
271 while ((ch
= getopt(argc
, argv
, "re:")) != -1)
278 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
295 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
298 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
301 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
311 while (erase
[i
] != NULL
) {
312 mtd_unlock(erase
[i
]);
326 mtd_write(argv
[1], device
);
331 kill(1, 15); // send SIGTERM to init for reboot