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.
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/mount.h>
38 #include <sys/reboot.h>
41 #include <linux/mtd/mtd.h>
44 #define TRX_MAGIC 0x30524448 /* "HDR0" */
46 #define TRX_MAX_LEN 0x3A0000
47 #define TRX_NO_HEADER 1 /* Do not write TRX header */
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 #define BUFSIZE (10 * 1024)
63 mtd_unlock(const char *mtd
)
66 struct mtd_info_user mtdInfo
;
67 struct erase_info_user mtdLockInfo
;
69 fd
= mtd_open(mtd
, O_RDWR
);
71 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
75 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
76 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
81 printf("Unlocking %s ...\n", mtd
);
82 mtdLockInfo
.start
= 0;
83 mtdLockInfo
.length
= mtdInfo
.size
;
84 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
85 fprintf(stderr
, "Could not unlock MTD device: %s\n", mtd
);
95 mtd_open(const char *mtd
, int flags
)
101 if ((fp
= fopen("/proc/mtd", "r"))) {
102 while (fgets(dev
, sizeof(dev
), fp
)) {
103 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
104 snprintf(dev
, sizeof(dev
), "/dev/mtd/%d", i
);
106 return open(dev
, flags
);
112 return open(mtd
, flags
);
116 mtd_erase(const char *mtd
)
119 struct mtd_info_user mtdInfo
;
120 struct erase_info_user mtdEraseInfo
;
122 fd
= mtd_open(mtd
, O_RDWR
);
124 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
128 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
129 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
134 printf("Erasing %s ...\n", mtd
);
135 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
137 for (mtdEraseInfo
.start
= 0;
138 mtdEraseInfo
.start
< mtdInfo
.size
;
139 mtdEraseInfo
.start
+= mtdInfo
.erasesize
) {
141 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
142 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
)) {
143 fprintf(stderr
, "Could not erase MTD device: %s\n", mtd
);
155 mtd_write(const char *trxfile
, const char *mtd
)
160 size_t result
,size
,written
;
161 struct mtd_info_user mtdInfo
;
162 struct erase_info_user mtdEraseInfo
;
164 unsigned char src
[BUFSIZE
],dest
[BUFSIZE
];
166 fd
= mtd_open(mtd
, O_RDWR
);
168 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
172 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
173 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
178 trxfd
= open(trxfile
,O_RDONLY
);
180 fprintf(stderr
, "Could not open trx image: %s\n", trxfile
);
184 if (fstat (trxfd
,&trxstat
) < 0) {
185 fprintf(stderr
, "Could not get trx image file status: %s\n", trxfile
);
190 if(mtdInfo
.size
< trxstat
.st_size
) {
191 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
196 printf("Writing %s to %s ...\n", trxfile
, mtd
);
198 mtdEraseInfo
.start
= 0;
199 mtdEraseInfo
.length
= trxstat
.st_size
& ~(mtdInfo
.erasesize
-1);
200 if(trxstat
.st_size
% mtdInfo
.erasesize
) mtdEraseInfo
.length
+= mtdInfo
.erasesize
;
202 /* erase the chunk */
203 if (ioctl (fd
,MEMERASE
,&mtdEraseInfo
) < 0) {
204 fprintf(stderr
, "Erasing mtd failed: %s\n", mtd
);
208 size
= trxstat
.st_size
;
213 if (size
< BUFSIZE
) i
= size
;
215 result
= write(fd
,src
,i
);
218 fprintf(stderr
,"Error while writing image");
221 fprintf(stderr
,"Error writing image");
233 printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
234 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
235 "mtd recognizes these commands:\n"
236 " unlock unlock the device\n"
237 " erase erase all data on device\n"
238 " write <imagefile> write imagefile to device\n"
239 "Following options are available:\n"
240 " -u unlock device before accessing it\n"
241 " -r reboot after successful command\n"
242 " -e <device> erase <device> before executing the command\n\n"
243 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
244 " mtd -r write linux.trx linux\n\n");
248 int main (int argc
, char **argv
)
250 int ch
, i
, boot
, unlock
;
251 char *erase
[MAX_ARGS
], *device
;
262 while ((ch
= getopt(argc
, argv
, "ure:")) != -1)
272 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
289 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
292 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
295 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
305 while (erase
[i
] != NULL
) {
306 mtd_unlock(erase
[i
]);
322 mtd_write(argv
[1], device
);
327 kill(1, 15); // send SIGTERM to init for reboot