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.
23 * code is based on linux-mtd example code
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>
44 #include <linux/mtd/mtd.h>
46 #include <mtd/mtd-user.h>
49 #define TRX_MAGIC 0x30524448 /* "HDR0" */
50 #define BUFSIZE (10 * 1024)
54 uint32_t magic; /* "HDR0" */
55 uint32_t len; /* Length of file including header */
56 uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
57 uint32_t flag_version; /* 0:15 flags, 16:31 version */
58 uint32_t offsets[3]; /* Offsets of partitions from start of header */
62 trx_check(const char *trxfile, const char *mtd, int force)
64 struct mtd_info_user mtdInfo;
67 struct trx_header trx;
70 trxfd = open(trxfile,O_RDONLY);
72 fprintf(stderr, "Could not open image: %s\n", trxfile);
76 if (fstat(trxfd,&trxstat) < 0) {
77 fprintf(stderr, "Could not get image file status: %s\n", trxfile);
83 count = read(trxfd, &trx, sizeof(struct trx_header));
84 if (count < sizeof(struct trx_header)) {
85 fprintf(stderr, "Could not get trx header, file too small (%ld bytes)\n", count);
90 if (trx.magic != TRX_MAGIC || trx.len < sizeof(struct trx_header)) {
91 fprintf(stderr, "Bad trx header\n");
92 fprintf(stderr, "If this is a firmware in bin format, like some of the\n"
93 "original firmware files are, use following command to convert to trx:\n"
94 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
99 lseek(trxfd, 0, SEEK_SET);
102 /* check if image fits to mtd device */
104 fd = mtd_open(mtd, O_RDWR);
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);
116 if(mtdInfo.size < trxstat.st_size) {
117 fprintf(stderr, "Image too big for partition: %s\n", mtd);
123 printf("Writing %s to %s ...\n", trxfile, mtd);
131 mtd_unlock(const char *mtd)
134 struct mtd_info_user mtdInfo;
135 struct erase_info_user mtdLockInfo;
137 fd = mtd_open(mtd, O_RDWR);
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);
149 printf("Unlocking %s ...\n", mtd);
150 mtdLockInfo.start = 0;
151 mtdLockInfo.length = mtdInfo.size;
152 if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
162 mtd_open(const char *mtd, int flags)
168 if ((fp = fopen("/proc/mtd", "r"))) {
169 while (fgets(dev, sizeof(dev), fp)) {
170 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
171 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
173 return open(dev, flags);
179 return open(mtd, flags);
183 mtd_erase(const char *mtd)
186 struct mtd_info_user mtdInfo;
187 struct erase_info_user mtdEraseInfo;
189 fd = mtd_open(mtd, O_RDWR);
191 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
195 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
196 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
201 printf("Erasing %s ...\n", mtd);
202 mtdEraseInfo.length = mtdInfo.erasesize;
204 for (mtdEraseInfo.start = 0;
205 mtdEraseInfo.start < mtdInfo.size;
206 mtdEraseInfo.start += mtdInfo.erasesize) {
208 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
209 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
210 fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
222 mtd_write(int trxfd, const char *mtd)
225 size_t result,size,written;
226 struct mtd_info_user mtdInfo;
227 struct erase_info_user mtdEraseInfo;
228 unsigned char src[BUFSIZE],dest[BUFSIZE];
231 if (fstat(trxfd,&trxstat) < 0) {
232 fprintf(stderr, "Could not get trx image file status\n");
237 fd = mtd_open(mtd, O_RDWR);
239 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
243 if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
244 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
249 mtdEraseInfo.start = 0;
250 mtdEraseInfo.length = trxstat.st_size & ~(mtdInfo.erasesize -1);
251 if(trxstat.st_size % mtdInfo.erasesize) mtdEraseInfo.length += mtdInfo.erasesize;
253 /* erase the chunk */
254 if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
255 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
259 size = trxstat.st_size;
264 if (size < BUFSIZE) i = size;
266 result = write(fd,src,i);
269 fprintf(stderr,"Error while writing image");
272 fprintf(stderr,"Error writing image");
284 printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
285 "The device is in the format of mtdX (eg: mtd4) or its label.\n"
286 "mtd recognizes these commands:\n"
287 " unlock unlock the device\n"
288 " erase erase all data on device\n"
289 " write <imagefile> write imagefile to device\n"
290 "Following options are available:\n"
291 " -r reboot after successful command\n"
292 " -f force write without trx checks\n"
293 " -e <device> erase <device> before executing the command\n\n"
294 "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
295 " mtd -r write linux.trx linux\n\n");
299 int main (int argc, char **argv)
301 int ch, i, boot, unlock, trxfd, force;
302 char *erase[MAX_ARGS], *device;
313 while ((ch = getopt(argc, argv, "fre:")) != -1)
323 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
340 if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
343 } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
346 } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
349 /* check trx file before erasing or writing anything */
350 trxfd = trx_check(argv[1], device, force);
358 while (erase[i] != NULL) {
359 mtd_unlock(erase[i]);
373 mtd_write(trxfd, device);
378 kill(1, 15); // send SIGTERM to init for reboot