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
, int force
)
60 struct mtd_info_user mtdInfo
;
63 struct trx_header trx
;
66 trxfd
= open(trxfile
,O_RDONLY
);
68 fprintf(stderr
, "Could not open image: %s\n", trxfile
);
72 if (fstat(trxfd
,&trxstat
) < 0) {
73 fprintf(stderr
, "Could not get image file status: %s\n", trxfile
);
79 count
= read(trxfd
, &trx
, sizeof(struct trx_header
));
80 if (count
< sizeof(struct trx_header
)) {
81 fprintf(stderr
, "Could not get trx header, file too small (%ld bytes)\n", count
);
86 if (trx
.magic
!= TRX_MAGIC
|| trx
.len
< sizeof(struct trx_header
)) {
87 fprintf(stderr
, "Bad trx header\n");
88 fprintf(stderr
, "If this is a firmware in bin format, like some of the\n"
89 "original firmware files are, use following command to convert to trx:\n"
90 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
95 lseek(trxfd
, 0, SEEK_SET
);
98 /* check if image fits to mtd device */
100 fd
= mtd_open(mtd
, O_RDWR
);
102 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
106 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
107 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
112 if(mtdInfo
.size
< trxstat
.st_size
) {
113 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
119 printf("Writing %s to %s ...\n", trxfile
, mtd
);
127 mtd_unlock(const char *mtd
)
130 struct mtd_info_user mtdInfo
;
131 struct erase_info_user mtdLockInfo
;
133 fd
= mtd_open(mtd
, O_RDWR
);
135 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
139 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
140 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
145 printf("Unlocking %s ...\n", mtd
);
146 mtdLockInfo
.start
= 0;
147 mtdLockInfo
.length
= mtdInfo
.size
;
148 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
158 mtd_open(const char *mtd
, int flags
)
164 if ((fp
= fopen("/proc/mtd", "r"))) {
165 while (fgets(dev
, sizeof(dev
), fp
)) {
166 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
167 snprintf(dev
, sizeof(dev
), "/dev/mtd/%d", i
);
169 return open(dev
, flags
);
175 return open(mtd
, flags
);
179 mtd_erase(const char *mtd
)
182 struct mtd_info_user mtdInfo
;
183 struct erase_info_user mtdEraseInfo
;
185 fd
= mtd_open(mtd
, O_RDWR
);
187 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
191 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
192 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
197 printf("Erasing %s ...\n", mtd
);
198 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
200 for (mtdEraseInfo
.start
= 0;
201 mtdEraseInfo
.start
< mtdInfo
.size
;
202 mtdEraseInfo
.start
+= mtdInfo
.erasesize
) {
204 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
205 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
)) {
206 fprintf(stderr
, "Could not erase MTD device: %s\n", mtd
);
218 mtd_write(int trxfd
, const char *mtd
)
221 size_t result
,size
,written
;
222 struct mtd_info_user mtdInfo
;
223 struct erase_info_user mtdEraseInfo
;
224 unsigned char src
[BUFSIZE
],dest
[BUFSIZE
];
227 if (fstat(trxfd
,&trxstat
) < 0) {
228 fprintf(stderr
, "Could not get trx image file status\n");
233 fd
= mtd_open(mtd
, O_RDWR
);
235 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
239 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
240 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
245 mtdEraseInfo
.start
= 0;
246 mtdEraseInfo
.length
= trxstat
.st_size
& ~(mtdInfo
.erasesize
-1);
247 if(trxstat
.st_size
% mtdInfo
.erasesize
) mtdEraseInfo
.length
+= mtdInfo
.erasesize
;
249 /* erase the chunk */
250 if (ioctl (fd
,MEMERASE
,&mtdEraseInfo
) < 0) {
251 fprintf(stderr
, "Erasing mtd failed: %s\n", mtd
);
255 size
= trxstat
.st_size
;
260 if (size
< BUFSIZE
) i
= size
;
262 result
= write(fd
,src
,i
);
265 fprintf(stderr
,"Error while writing image");
268 fprintf(stderr
,"Error writing image");
280 printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
281 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
282 "mtd recognizes these commands:\n"
283 " unlock unlock the device\n"
284 " erase erase all data on device\n"
285 " write <imagefile> write imagefile to device\n"
286 "Following options are available:\n"
287 " -r reboot after successful command\n"
288 " -f force write without trx checks\n"
289 " -e <device> erase <device> before executing the command\n\n"
290 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
291 " mtd -r write linux.trx linux\n\n");
295 int main (int argc
, char **argv
)
297 int ch
, i
, boot
, unlock
, trxfd
, force
;
298 char *erase
[MAX_ARGS
], *device
;
309 while ((ch
= getopt(argc
, argv
, "fre:")) != -1)
319 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
336 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
339 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
342 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
345 /* check trx file before erasing or writing anything */
346 trxfd
= trx_check(argv
[1], device
, force
);
354 while (erase
[i
] != NULL
) {
355 mtd_unlock(erase
[i
]);
369 mtd_write(trxfd
, device
);
374 kill(1, 15); // send SIGTERM to init for reboot