2 * mtd - simple memory technology device manipulation tool
4 * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * mtd utility for the openwrt project
21 * it is mainly code from the linux-mtd project, which accepts the same
22 * command line arguments as the broadcom utility
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/mount.h>
44 #include <linux/mtd/mtd.h>
47 #define TRX_MAGIC 0x30524448 /* "HDR0" */
49 #define TRX_MAX_LEN 0x3A0000
50 #define TRX_NO_HEADER 1 /* Do not write TRX header */
53 uint32_t magic; /* "HDR0" */
54 uint32_t len; /* Length of file including header */
55 uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
56 uint32_t flag_version; /* 0:15 flags, 16:31 version */
57 uint32_t offsets[3]; /* Offsets of partitions from start of header */
60 #define BUFSIZE (10 * 1024)
62 extern int mtd_open(const char *mtd, int flags);
63 extern int mtd_erase(const char *mtd);
64 extern int mtd_write(const char *trxfile, const char *mtd);
67 mtd_unlock(const char *mtd)
70 struct mtd_info_user mtdInfo;
71 struct erase_info_user mtdLockInfo;
73 fd = mtd_open(mtd, O_RDWR);
75 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
79 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
80 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
85 mtdLockInfo.start = 0;
86 mtdLockInfo.length = mtdInfo.size;
87 if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
88 fprintf(stderr, "Could not unlock MTD device: %s\n", mtd);
98 mtd_open(const char *mtd, int flags)
104 if ((fp = fopen("/proc/mtd", "r"))) {
105 while (fgets(dev, sizeof(dev), fp)) {
106 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
107 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
109 return open(dev, flags);
115 return open(mtd, flags);
119 mtd_erase(const char *mtd)
122 struct mtd_info_user mtdInfo;
123 struct erase_info_user mtdEraseInfo;
125 fd = mtd_open(mtd, O_RDWR);
127 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
131 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
132 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
137 mtdEraseInfo.length = mtdInfo.erasesize;
139 for (mtdEraseInfo.start = 0;
140 mtdEraseInfo.start < mtdInfo.size;
141 mtdEraseInfo.start += mtdInfo.erasesize) {
143 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
144 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
145 fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
157 mtd_write(const char *trxfile, const char *mtd)
162 size_t result,size,written;
163 struct mtd_info_user mtdInfo;
164 struct erase_info_user mtdEraseInfo;
166 unsigned char src[BUFSIZE],dest[BUFSIZE];
168 fd = mtd_open(mtd, O_RDWR);
170 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
174 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
175 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
180 trxfd = open(trxfile,O_RDONLY);
182 fprintf(stderr, "Could not open trx image: %s\n", trxfile);
186 if (fstat (trxfd,&trxstat) < 0) {
187 fprintf(stderr, "Could not get trx image file status: %s\n", trxfile);
192 if(mtdInfo.size < trxstat.st_size) {
193 fprintf(stderr, "Image too big for partition: %s\n", 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");
231 int main(int argc, char **argv) {
232 if(argc == 3 && strcasecmp(argv[1],"unlock")==0) {
233 printf("Unlocking %s ...\n",argv[2]);
234 return mtd_unlock(argv[2]);
236 if(argc == 3 && strcasecmp(argv[1],"erase")==0) {
237 printf("Erasing %s ...\n",argv[2]);
238 return mtd_erase(argv[2]);
240 if(argc == 4 && strcasecmp(argv[1],"write")==0) {
241 printf("Writing %s to %s ...\n",argv[2],argv[3]);
242 return mtd_write(argv[2],argv[3]);
245 printf("no valid command given\n");
246 printf("\nmtd: modify data within a Memory Technology Device.\n");
247 printf("Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>\n");
248 printf("Documented by Mike Strates [dumpedcore] <mike@dilaudid.net>\n");
249 printf("mtd has ABSOLUTELY NO WARRANTY and is licensed under the GNU GPL.\n");
250 printf("\nUsage: mtd [unlock|erase] device\n");
251 printf(" mtd write imagefile device\n");
252 printf("\n .. where device is in the format of mtdX (eg: mtd4) or its label.\n");
253 printf("\nunlock enable modification to device\n");
254 printf("erase erase all data on device\n");
255 printf("write write imagefile to device\n");
256 printf("\nExample: To write linux.trx to mtd4 labeled as linux\n");
257 printf("\n mtd unlock linux && mtd write linux.trx linux\n\n");