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.
23 * The code is based on the linux-mtd examples.
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 (16 * 1024)
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 */
63 trx_check(int imagefd
, const char *mtd
)
65 struct mtd_info_user mtdInfo
;
68 struct trx_header
*trx
= (struct trx_header
*) buf
;
71 buflen
= read(imagefd
, buf
, sizeof(struct trx_header
));
72 if (buflen
< sizeof(struct trx_header
)) {
73 fprintf(stderr
, "Could not get trx header, file too small (%ld bytes)\n", buflen
);
77 if (trx
->magic
!= TRX_MAGIC
|| trx
->len
< sizeof(struct trx_header
)) {
78 fprintf(stderr
, "Bad trx header\n");
79 fprintf(stderr
, "If this is a firmware in bin format, like some of the\n"
80 "original firmware files are, use following command to convert to trx:\n"
81 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
85 /* check if image fits to mtd device */
86 fd
= mtd_open(mtd
, O_RDWR
);
88 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
92 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
93 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
97 if(mtdInfo
.size
< trx
->len
) {
98 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
106 int mtd_check(char *mtd
)
108 struct mtd_info_user mtdInfo
;
111 fd
= mtd_open(mtd
, O_RDWR
);
113 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
117 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
118 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
128 mtd_unlock(const char *mtd
)
131 struct mtd_info_user mtdInfo
;
132 struct erase_info_user mtdLockInfo
;
134 fd
= mtd_open(mtd
, O_RDWR
);
136 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
140 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
141 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
146 fprintf(stderr
, "Unlocking %s ...\n", mtd
);
147 mtdLockInfo
.start
= 0;
148 mtdLockInfo
.length
= mtdInfo
.size
;
149 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
159 mtd_open(const char *mtd
, int flags
)
165 if ((fp
= fopen("/proc/mtd", "r"))) {
166 while (fgets(dev
, sizeof(dev
), fp
)) {
167 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
168 snprintf(dev
, sizeof(dev
), "/dev/mtd/%d", i
);
170 return open(dev
, flags
);
176 return open(mtd
, flags
);
180 mtd_erase(const char *mtd
)
183 struct mtd_info_user mtdInfo
;
184 struct erase_info_user mtdEraseInfo
;
186 fd
= mtd_open(mtd
, O_RDWR
);
188 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
192 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
193 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
198 fprintf(stderr
, "Erasing %s ...\n", mtd
);
199 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
201 for (mtdEraseInfo
.start
= 0;
202 mtdEraseInfo
.start
< mtdInfo
.size
;
203 mtdEraseInfo
.start
+= mtdInfo
.erasesize
) {
205 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
206 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
)) {
207 fprintf(stderr
, "Could not erase MTD device: %s\n", mtd
);
219 mtd_write(int imagefd
, const char *mtd
)
223 struct mtd_info_user mtdInfo
;
224 struct erase_info_user mtdEraseInfo
;
226 fd
= mtd_open(mtd
, O_RDWR
);
228 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
232 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
233 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
239 fprintf(stderr
, " [ ]");
242 /* buffer may contain data already (from trx check) */
244 r
+= read(imagefd
, buf
+ buflen
, BUFSIZE
- buflen
);
250 /* need to erase the next block before writing data to it */
252 mtdEraseInfo
.start
= e
;
253 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
255 fprintf(stderr
, "\b\b\b[e]");
256 /* erase the chunk */
257 if (ioctl (fd
,MEMERASE
,&mtdEraseInfo
) < 0) {
258 fprintf(stderr
, "Erasing mtd failed: %s\n", mtd
);
261 e
+= mtdInfo
.erasesize
;
264 fprintf(stderr
, "\b\b\b[w]");
266 if ((result
= write(fd
, buf
, r
)) < r
) {
268 fprintf(stderr
, "Error writing image.\n");
271 fprintf(stderr
, "Insufficient space.\n");
278 fprintf(stderr
, "\b\b\b\b");
285 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
286 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
287 "mtd recognizes these commands:\n"
288 " unlock unlock the device\n"
289 " erase erase all data on device\n"
290 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
291 "Following options are available:\n"
292 " -r reboot after successful command\n"
293 " -f force write without trx checks\n"
294 " -e <device> erase <device> before executing the command\n\n"
295 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
296 " mtd -r write linux.trx linux\n\n");
300 int main (int argc
, char **argv
)
302 int ch
, i
, boot
, unlock
, imagefd
, force
;
303 char *erase
[MAX_ARGS
], *device
, *imagefile
;
315 while ((ch
= getopt(argc
, argv
, "fre:")) != -1)
325 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
342 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
345 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
348 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
352 if (strcmp(argv
[1], "-") == 0) {
353 imagefile
= "<stdin>";
357 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
358 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
363 if (system("grep Broadcom /proc/cpuinfo >&- >&-") == 0) {
364 /* check trx file before erasing or writing anything */
365 if (!trx_check(imagefd
, device
)) {
366 fprintf(stderr
, "TRX check failed!\n");
371 if (!mtd_check(device
)) {
372 fprintf(stderr
, "Can't open device for writing!\n");
383 while (erase
[i
] != NULL
) {
384 mtd_unlock(erase
[i
]);
398 fprintf(stderr
, "Writing from %s to %s ... ", imagefile
, device
);
399 mtd_write(imagefd
, device
);
400 fprintf(stderr
, "\n");
405 kill(1, 15); // send SIGTERM to init for reboot