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 trx_check(const char *trxfile
, const char *mtd
)
60 struct mtd_info_user mtdInfo
;
63 struct trx_header trx
;
66 trxfd
= open(trxfile
,O_RDONLY
);
68 fprintf(stderr
, "Could not open trx image: %s\n", trxfile
);
72 if (fstat(trxfd
,&trxstat
) < 0) {
73 fprintf(stderr
, "Could not get trx image file status: %s\n", trxfile
);
78 count
= read(trxfd
, &trx
, sizeof(struct trx_header
));
79 if (count
< sizeof(struct trx_header
)) {
80 fprintf(stderr
, "Could not trx header, file too small (%ld bytes)\n", count
);
85 if (trx
.magic
!= TRX_MAGIC
|| trx
.len
< sizeof(struct trx_header
)) {
86 fprintf(stderr
, "Bad trx header\n");
87 fprintf(stderr
, "If this is a firmware in bin format, like some of the\n"
88 "original firmware files are, use following command to convert to trx:\n"
89 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
94 lseek(trxfd
, 0, SEEK_SET
);
96 /* check if image fits to mtd device */
98 fd
= mtd_open(mtd
, O_RDWR
);
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
);
110 if(mtdInfo
.size
< trxstat
.st_size
) {
111 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
117 printf("Writing %s to %s ...\n", trxfile
, mtd
);
125 mtd_unlock(const char *mtd
)
128 struct mtd_info_user mtdInfo
;
129 struct erase_info_user mtdLockInfo
;
131 fd
= mtd_open(mtd
, O_RDWR
);
133 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
137 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
138 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
143 printf("Unlocking %s ...\n", mtd
);
144 mtdLockInfo
.start
= 0;
145 mtdLockInfo
.length
= mtdInfo
.size
;
146 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
156 mtd_open(const char *mtd
, int flags
)
162 if ((fp
= fopen("/proc/mtd", "r"))) {
163 while (fgets(dev
, sizeof(dev
), fp
)) {
164 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
165 snprintf(dev
, sizeof(dev
), "/dev/mtd/%d", i
);
167 return open(dev
, flags
);
173 return open(mtd
, flags
);
177 mtd_erase(const char *mtd
)
180 struct mtd_info_user mtdInfo
;
181 struct erase_info_user mtdEraseInfo
;
183 fd
= mtd_open(mtd
, O_RDWR
);
185 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
189 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
190 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
195 printf("Erasing %s ...\n", mtd
);
196 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
198 for (mtdEraseInfo
.start
= 0;
199 mtdEraseInfo
.start
< mtdInfo
.size
;
200 mtdEraseInfo
.start
+= mtdInfo
.erasesize
) {
202 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
203 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
)) {
204 fprintf(stderr
, "Could not erase MTD device: %s\n", mtd
);
216 mtd_write(int trxfd
, const char *mtd
)
219 size_t result
,size
,written
;
220 struct mtd_info_user mtdInfo
;
221 struct erase_info_user mtdEraseInfo
;
222 unsigned char src
[BUFSIZE
],dest
[BUFSIZE
];
225 if (fstat(trxfd
,&trxstat
) < 0) {
226 fprintf(stderr
, "Could not get trx image file status\n");
231 fd
= mtd_open(mtd
, O_RDWR
);
233 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
237 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
238 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
243 mtdEraseInfo
.start
= 0;
244 mtdEraseInfo
.length
= trxstat
.st_size
& ~(mtdInfo
.erasesize
-1);
245 if(trxstat
.st_size
% mtdInfo
.erasesize
) mtdEraseInfo
.length
+= mtdInfo
.erasesize
;
247 /* erase the chunk */
248 if (ioctl (fd
,MEMERASE
,&mtdEraseInfo
) < 0) {
249 fprintf(stderr
, "Erasing mtd failed: %s\n", mtd
);
253 size
= trxstat
.st_size
;
258 if (size
< BUFSIZE
) i
= size
;
260 result
= write(fd
,src
,i
);
263 fprintf(stderr
,"Error while writing image");
266 fprintf(stderr
,"Error writing image");
278 printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
279 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
280 "mtd recognizes these commands:\n"
281 " unlock unlock the device\n"
282 " erase erase all data on device\n"
283 " write <imagefile> write imagefile to device\n"
284 "Following options are available:\n"
285 " -r reboot after successful command\n"
286 " -e <device> erase <device> before executing the command\n\n"
287 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
288 " mtd -r write linux.trx linux\n\n");
292 int main (int argc
, char **argv
)
294 int ch
, i
, boot
, unlock
, trxfd
;
295 char *erase
[MAX_ARGS
], *device
;
305 while ((ch
= getopt(argc
, argv
, "re:")) != -1)
312 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
329 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
332 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
335 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
338 /* check trx file before erasing or writing anything */
339 trxfd
= trx_check(argv
[1], device
);
347 while (erase
[i
] != NULL
) {
348 mtd_unlock(erase
[i
]);
362 mtd_write(trxfd
, device
);
367 kill(1, 15); // send SIGTERM to init for reboot