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.
31 #include <sys/ioctl.h>
32 #include <sys/syscall.h>
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/mount.h>
43 #include <sys/reboot.h>
44 #include <linux/reboot.h>
48 #define TRX_MAGIC 0x30524448 /* "HDR0" */
49 #define BUFSIZE (16 * 1024)
54 #define SYSTYPE_UNKNOWN 0
55 #define SYSTYPE_BROADCOM 1
59 uint32_t magic
; /* "HDR0" */
60 uint32_t len
; /* Length of file including header */
61 uint32_t crc32
; /* 32-bit CRC from flag_version to end of file */
62 uint32_t flag_version
; /* 0:15 flags, 16:31 version */
63 uint32_t offsets
[3]; /* Offsets of partitions from start of header */
72 image_check_brcm(int imagefd
, const char *mtd
)
74 struct trx_header
*trx
= (struct trx_header
*) buf
;
75 struct mtd_info_user mtdInfo
;
78 if (strcmp(mtd
, "linux") != 0)
81 buflen
= read(imagefd
, buf
, 32);
83 fprintf(stdout
, "Could not get image header, file too small (%ld bytes)\n", buflen
);
87 if (trx
->magic
!= TRX_MAGIC
|| trx
->len
< sizeof(struct trx_header
)) {
89 fprintf(stderr
, "Bad trx header\n");
90 fprintf(stderr
, "This is not the correct file format; refusing to flash.\n"
91 "Please specify the correct file or use -f to force.\n");
96 /* check if image fits to mtd device */
97 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
99 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
103 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
104 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
108 if(mtdInfo
.size
< trx
->len
) {
109 fprintf(stderr
, "Image too big for partition: %s\n", mtd
);
117 #endif /* target_brcm */
120 image_check(int imagefd
, const char *mtd
)
128 return image_check_brcm(imagefd
, mtd
);
132 int mtd_check(char *mtd
)
134 struct mtd_info_user mtdInfo
;
137 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
139 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
143 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
144 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
154 mtd_unlock(const char *mtd
)
157 struct mtd_info_user mtdInfo
;
158 struct erase_info_user mtdLockInfo
;
160 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
162 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
166 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
167 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
172 mtdLockInfo
.start
= 0;
173 mtdLockInfo
.length
= mtdInfo
.size
;
174 if(ioctl(fd
, MEMUNLOCK
, &mtdLockInfo
)) {
184 mtd_open(const char *mtd
, int flags
)
191 if ((fp
= fopen("/proc/mtd", "r"))) {
192 while (fgets(dev
, sizeof(dev
), fp
)) {
193 if (sscanf(dev
, "mtd%d:", &i
) && strstr(dev
, mtd
)) {
194 snprintf(dev
, sizeof(dev
), "/dev/mtd/%d", i
);
195 if ((ret
=open(dev
, flags
))<0) {
196 snprintf(dev
, sizeof(dev
), "/dev/mtd%d", i
);
197 ret
=open(dev
, flags
);
206 return open(mtd
, flags
);
210 mtd_erase(const char *mtd
)
213 struct mtd_info_user mtdInfo
;
214 struct erase_info_user mtdEraseInfo
;
216 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
218 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
222 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
223 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
228 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
230 for (mtdEraseInfo
.start
= 0;
231 mtdEraseInfo
.start
< mtdInfo
.size
;
232 mtdEraseInfo
.start
+= mtdInfo
.erasesize
) {
234 ioctl(fd
, MEMUNLOCK
, &mtdEraseInfo
);
235 if(ioctl(fd
, MEMERASE
, &mtdEraseInfo
))
236 fprintf(stderr
, "Failed to erase block on %s at 0x%x\n", mtd
, mtdEraseInfo
.start
);
245 mtd_write(int imagefd
, const char *mtd
)
249 struct mtd_info_user mtdInfo
;
250 struct erase_info_user mtdEraseInfo
;
253 fd
= mtd_open(mtd
, O_RDWR
| O_SYNC
);
255 fprintf(stderr
, "Could not open mtd device: %s\n", mtd
);
259 if(ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
260 fprintf(stderr
, "Could not get MTD device info from %s\n", mtd
);
267 fprintf(stderr
, " [ ]");
270 /* buffer may contain data already (from trx check) */
272 r
+= read(imagefd
, buf
+ buflen
, BUFSIZE
- buflen
);
278 /* need to erase the next block before writing data to it */
280 mtdEraseInfo
.start
= e
;
281 mtdEraseInfo
.length
= mtdInfo
.erasesize
;
284 fprintf(stderr
, "\b\b\b[e]");
285 /* erase the chunk */
286 if (ioctl (fd
,MEMERASE
,&mtdEraseInfo
) < 0) {
287 fprintf(stderr
, "Erasing mtd failed: %s\n", mtd
);
290 e
+= mtdInfo
.erasesize
;
294 fprintf(stderr
, "\b\b\b[w]");
296 if ((result
= write(fd
, buf
, r
)) < r
) {
298 fprintf(stderr
, "Error writing image.\n");
301 fprintf(stderr
, "Insufficient space.\n");
309 fprintf(stderr
, "\b\b\b\b");
317 fprintf(stderr
, "Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
318 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
319 "mtd recognizes these commands:\n"
320 " unlock unlock the device\n"
321 " erase erase all data on device\n"
322 " write <imagefile>|- write <imagefile> (use - for stdin) to device\n"
323 "Following options are available:\n"
324 " -q quiet mode (once: no [w] on writing,\n"
325 " twice: no status messages)\n"
326 " -r reboot after successful command\n"
327 " -f force write without trx checks\n"
328 " -e <device> erase <device> before executing the command\n\n"
329 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
330 " mtd -r write linux.trx linux\n\n");
334 int main (int argc
, char **argv
)
336 int ch
, i
, boot
, unlock
, imagefd
, force
, unlocked
;
337 char *erase
[MAX_ARGS
], *device
, *imagefile
;
350 while ((ch
= getopt(argc
, argv
, "frqe:")) != -1)
363 while ((erase
[i
] != NULL
) && ((i
+ 1) < MAX_ARGS
))
380 if ((strcmp(argv
[0], "unlock") == 0) && (argc
== 2)) {
383 } else if ((strcmp(argv
[0], "erase") == 0) && (argc
== 2)) {
386 } else if ((strcmp(argv
[0], "write") == 0) && (argc
== 3)) {
390 if (strcmp(argv
[1], "-") == 0) {
391 imagefile
= "<stdin>";
395 if ((imagefd
= open(argv
[1], O_RDONLY
)) < 0) {
396 fprintf(stderr
, "Couldn't open image file: %s!\n", imagefile
);
401 /* check trx file before erasing or writing anything */
402 if (!image_check(imagefd
, device
)) {
404 fprintf(stderr
, "Image check failed.\n");
408 if (!mtd_check(device
)) {
409 fprintf(stderr
, "Can't open device for writing!\n");
421 while (erase
[i
] != NULL
) {
423 fprintf(stderr
, "Unlocking %s ...\n", erase
[i
]);
424 mtd_unlock(erase
[i
]);
426 fprintf(stderr
, "Erasing %s ...\n", erase
[i
]);
428 if (strcmp(erase
[i
], device
) == 0)
435 fprintf(stderr
, "Unlocking %s ...\n", device
);
444 fprintf(stderr
, "Erasing %s ...\n", device
);
449 fprintf(stderr
, "Writing from %s to %s ... ", imagefile
, device
);
450 mtd_write(imagefd
, device
);
452 fprintf(stderr
, "\n");
460 syscall(SYS_reboot
,LINUX_REBOOT_MAGIC1
,LINUX_REBOOT_MAGIC2
,LINUX_REBOOT_CMD_RESTART
,NULL
);