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)
51 #define SYSTYPE_UNKNOWN 0
52 #define SYSTYPE_BROADCOM 1
56 uint32_t magic
; /* "HDR0" */
57 uint32_t len
; /* Length of file including header */
58 uint32_t crc32
; /* 32-bit CRC from flag_version to end of file */
59 uint32_t flag_version
; /* 0:15 flags, 16:31 version */
60 uint32_t offsets
[3]; /* Offsets of partitions from start of header */
68 image_check_bcom(int imagefd
, const char *mtd
)
70 struct trx_header
*trx
= (struct trx_header
*) buf
;
71 struct mtd_info_user mtdInfo
;
74 if (strcmp(mtd
, "linux") != 0)
77 buflen
= read(imagefd
, buf
, 32);
79 fprintf(stdout
, "Could not get image header, file too small (%ld bytes)\n", buflen
);
84 case 0x47343557: /* W54G */
85 case 0x53343557: /* W54S */
86 case 0x73343557: /* W54s */
87 case 0x46343557: /* W54F */
88 case 0x55343557: /* W54U */
89 /* ignore the first 32 bytes */
90 buflen
= read(imagefd
, buf
, sizeof(struct trx_header
));
94 if (trx
->magic
!= TRX_MAGIC
|| trx
->len
< sizeof(struct trx_header
)) {
96 fprintf(stderr
, "Bad trx header\n");
97 fprintf(stderr
, "If this is a firmware in bin format, like some of the\n"
98 "original firmware files are, you need to convert it to trx.\n");
103 /* check if image fits to mtd device */
104 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
106 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
110 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
111 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
115 if(mtdInfo
.size
< trx
->len
) {
116 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
126 image_check(int imagefd
, const char *mtd
)
133 systype
= SYSTYPE_UNKNOWN
;
134 f
= fopen("/proc/cpuinfo", "r");
135 while (!feof(f
) && (fgets(buf
, BUFSIZE
- 1, f
) != NULL
)) {
136 if ((strncmp(buf
, "system type", 11) == 0) && (c
= strchr(buf
, ':'))) {
138 if (strncmp(c
, "Broadcom BCM947XX", 17) == 0)
139 systype
= SYSTYPE_BROADCOM
;
145 case SYSTYPE_BROADCOM
:
146 return image_check_bcom(imagefd
, mtd
);
152 int mtd_check(char *mtd
)
154 struct mtd_info_user mtdInfo
;
157 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
159 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
163 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
164 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
174 mtd_unlock(const char *mtd
)
177 struct mtd_info_user mtdInfo
;
178 struct erase_info_user mtdLockInfo
;
180 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
182 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
186 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
187 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
192 mtdLockInfo
.start
= 0;
193 mtdLockInfo
.length
= mtdInfo
.size
;
194 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
204 mtd_open(const char *mtd
, int flags
)
210 if ((fp
= fopen("/proc/mtd", "r"))) {
211 while (fgets(dev
, sizeof(dev
), fp
)) {
212 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
213 snprintf(dev
, sizeof(dev
), "/dev/mtd/%d", i
);
215 return open(dev
, flags
);
221 return open(mtd
, flags
);
225 mtd_erase(const char *mtd
)
228 struct mtd_info_user mtdInfo
;
229 struct erase_info_user mtdEraseInfo
;
231 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
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
.length
= mtdInfo
.erasesize
;
245 for (mtdEraseInfo
.start
= 0;
246 mtdEraseInfo
.start
< mtdInfo
.size
;
247 mtdEraseInfo
.start
+= mtdInfo
.erasesize
) {
249 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
250 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
)) {
251 fprintf(stderr
, "Could not erase MTD device: %s\n", mtd
);
263 mtd_write(int imagefd
, const char *mtd
)
267 struct mtd_info_user mtdInfo
;
268 struct erase_info_user mtdEraseInfo
;
271 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
273 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
277 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
278 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
285 fprintf(stderr
, " [ ]");
288 /* buffer may contain data already (from trx check) */
290 r
+= read(imagefd
, buf
+ buflen
, BUFSIZE
- buflen
);
296 /* need to erase the next block before writing data to it */
298 mtdEraseInfo
.start
= e
;
299 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
302 fprintf(stderr
, "\b\b\b[e]");
303 /* erase the chunk */
304 if (ioctl (fd
,MEMERASE
,&mtdEraseInfo
) < 0) {
305 fprintf(stderr
, "Erasing mtd failed: %s\n", mtd
);
308 e
+= mtdInfo
.erasesize
;
312 fprintf(stderr
, "\b\b\b[w]");
314 if ((result
= write(fd
, buf
, r
)) < r
) {
316 fprintf(stderr
, "Error writing image.\n");
319 fprintf(stderr
, "Insufficient space.\n");
327 fprintf(stderr
, "\b\b\b\b");
335 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
336 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
337 "mtd recognizes these commands:\n"
338 " unlock unlock the device\n"
339 " erase erase all data on device\n"
340 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
341 "Following options are available:\n"
342 " -q quiet mode (once: no [w] on writing,\n"
343 " twice: no status messages)\n"
344 " -r reboot after successful command\n"
345 " -f force write without trx checks\n"
346 " -e <device> erase <device> before executing the command\n\n"
347 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
348 " mtd -r write linux.trx linux\n\n");
352 int main (int argc
, char **argv
)
354 int ch
, i
, boot
, unlock
, imagefd
, force
, unlocked
;
355 char *erase
[MAX_ARGS
], *device
, *imagefile
;
368 while ((ch
= getopt(argc
, argv
, "frqe:")) != -1)
381 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
398 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
401 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
404 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
408 if (strcmp(argv
[1], "-") == 0) {
409 imagefile
= "<stdin>";
413 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
414 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
419 /* check trx file before erasing or writing anything */
420 if (!image_check(imagefd
, device
)) {
422 fprintf(stderr
, "Image check failed.\n");
426 if (!mtd_check(device
)) {
427 fprintf(stderr
, "Can't open device for writing!\n");
439 while (erase
[i
] != NULL
) {
441 fprintf(stderr
, "Unlocking %s ...\n", erase
[i
]);
442 mtd_unlock(erase
[i
]);
444 fprintf(stderr
, "Erasing %s ...\n", erase
[i
]);
446 if (strcmp(erase
[i
], device
) == 0)
453 fprintf(stderr
, "Unlocking %s ...\n", device
);
462 fprintf(stderr
, "Erasing %s ...\n", device
);
467 fprintf(stderr
, "Writing from %s to %s ... ", imagefile
, device
);
468 mtd_write(imagefd
, device
);
470 fprintf(stderr
, "\n");
477 kill(1, 15); // send SIGTERM to init for reboot