link to specific header, just to be safe
[openwrt.git] / package / mtd / mtd.c
1 /*
2  * mtd - simple memory technology device manipulation tool
3  *
4  * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>,
5  *                    Felix Fietkau <nbd@vd-s.ath.cx>
6  *
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.
11  *
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.
16  *
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.
20  *
21  * $Id$
22  *
23  * code is based on linux-mtd example code
24  */
25
26 #include <limits.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <error.h>
34 #include <time.h>
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40 #include <sys/reboot.h>
41 #include <string.h>
42
43 #ifdef LINUX_24
44 #include <linux/mtd/mtd.h>
45 #else
46 #include <mtd/mtd-user.h>
47 #endif
48
49 #define TRX_MAGIC       0x30524448      /* "HDR0" */
50 #define BUFSIZE (10 * 1024)
51 #define MAX_ARGS 8
52
53 struct trx_header {
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 */
59 };
60
61 int
62 trx_check(const char *trxfile, const char *mtd, int force)
63 {
64         struct mtd_info_user mtdInfo;
65         int trxfd, fd;
66         size_t count;
67         struct trx_header trx;
68         struct stat trxstat;
69
70         trxfd = open(trxfile,O_RDONLY); 
71         if(trxfd < 0) {
72                 fprintf(stderr, "Could not open image: %s\n", trxfile);
73                 exit(1);
74         }
75
76         if (fstat(trxfd,&trxstat) < 0) {
77                 fprintf(stderr, "Could not get image file status: %s\n", trxfile);
78                 close(trxfd);
79                 exit(1);
80         }
81
82         if (force == 0) {
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);
86                         close(trxfd);
87                         exit(1);
88                 }
89
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");
95                         close(trxfd);
96                         exit(1);
97                 }
98         
99                 lseek(trxfd, 0, SEEK_SET);
100         }
101
102         /* check if image fits to mtd device */
103
104         fd = mtd_open(mtd, O_RDWR);
105         if(fd < 0) {
106                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
107                 exit(1);
108         }
109
110         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
111                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
112                 close(fd);
113                 exit(1);
114         }
115                 
116         if(mtdInfo.size < trxstat.st_size) {
117                 fprintf(stderr, "Image too big for partition: %s\n", mtd);
118                 close(trxfd);
119                 close(fd);
120                 exit(1);
121         }       
122         
123         printf("Writing %s to %s ...\n", trxfile, mtd);
124
125         close(fd);
126
127         return(trxfd);
128 }
129
130 int
131 mtd_unlock(const char *mtd)
132 {
133         int fd;
134         struct mtd_info_user mtdInfo;
135         struct erase_info_user mtdLockInfo;
136
137         fd = mtd_open(mtd, O_RDWR);
138         if(fd < 0) {
139                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
140                 exit(1);
141         }
142
143         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
144                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
145                 close(fd);
146                 exit(1);
147         }
148
149         printf("Unlocking %s ...\n", mtd);
150         mtdLockInfo.start = 0;
151         mtdLockInfo.length = mtdInfo.size;
152         if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
153                 close(fd);
154                 return 0;
155         }
156                 
157         close(fd);
158         return 0;
159 }
160
161 int
162 mtd_open(const char *mtd, int flags)
163 {
164         FILE *fp;
165         char dev[PATH_MAX];
166         int i;
167
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);
172                                 fclose(fp);
173                                 return open(dev, flags);
174                         }
175                 }
176                 fclose(fp);
177         }
178
179         return open(mtd, flags);
180 }
181
182 int
183 mtd_erase(const char *mtd)
184 {
185         int fd;
186         struct mtd_info_user mtdInfo;
187         struct erase_info_user mtdEraseInfo;
188
189         fd = mtd_open(mtd, O_RDWR);
190         if(fd < 0) {
191                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
192                 exit(1);
193         }
194
195         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
196                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
197                 close(fd);
198                 exit(1);
199         }
200
201         printf("Erasing %s ...\n", mtd);
202         mtdEraseInfo.length = mtdInfo.erasesize;
203
204         for (mtdEraseInfo.start = 0;
205                  mtdEraseInfo.start < mtdInfo.size;
206                  mtdEraseInfo.start += mtdInfo.erasesize) {
207                 
208                 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
209                 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
210                         fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
211                         close(fd);
212                         exit(1);
213                 }
214         }               
215
216         close(fd);
217         return 0;
218
219 }
220
221 int
222 mtd_write(int trxfd, const char *mtd)
223 {
224         int fd,i;
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];
229         struct stat trxstat;
230
231         if (fstat(trxfd,&trxstat) < 0) {
232                 fprintf(stderr, "Could not get trx image file status\n");
233                 close(trxfd);
234                 exit(1);
235         }
236
237         fd = mtd_open(mtd, O_RDWR);
238         if(fd < 0) {
239                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
240                 exit(1);
241         }
242
243         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
244                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
245                 close(fd);
246                 exit(1);
247         }
248                 
249         mtdEraseInfo.start = 0;
250         mtdEraseInfo.length = trxstat.st_size & ~(mtdInfo.erasesize -1);
251         if(trxstat.st_size % mtdInfo.erasesize) mtdEraseInfo.length += mtdInfo.erasesize;
252
253         /* erase the chunk */
254         if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
255                 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
256                 exit(1);
257         }
258         
259         size = trxstat.st_size;
260         i = BUFSIZE;
261         written = 0;
262
263         while (size) {
264                 if (size < BUFSIZE) i = size;
265                 read(trxfd,src,i);
266                 result = write(fd,src,i);
267                 if (i != result) {
268                         if (result < 0) {
269                                 fprintf(stderr,"Error while writing image");
270                                 exit(1);
271                         }
272                         fprintf(stderr,"Error writing image");
273                         exit(1);
274                 }
275                 written += i;
276                 size -= i;
277         }
278         
279         return 0;
280 }
281
282 void usage(void)
283 {
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");
296         exit(1);
297 }
298
299 int main (int argc, char **argv)
300 {
301         int ch, i, boot, unlock, trxfd, force;
302         char *erase[MAX_ARGS], *device;
303         enum {
304                 CMD_ERASE,
305                 CMD_WRITE,
306                 CMD_UNLOCK
307         } cmd;
308         
309         erase[0] = NULL;
310         boot = 0;
311         force = 0;
312
313         while ((ch = getopt(argc, argv, "fre:")) != -1)
314                 switch (ch) {
315                         case 'f':
316                                 force = 1;
317                                 break;
318                         case 'r':
319                                 boot = 1;
320                                 break;
321                         case 'e':
322                                 i = 0;
323                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
324                                         i++;
325                                         
326                                 erase[i++] = optarg;
327                                 erase[i] = NULL;
328                                 break;
329                         
330                         case '?':
331                         default:
332                                 usage();
333                 }
334         argc -= optind;
335         argv += optind;
336         
337         if (argc < 2)
338                 usage();
339
340         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
341                 cmd = CMD_UNLOCK;
342                 device = argv[1];
343         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
344                 cmd = CMD_ERASE;
345                 device = argv[1];
346         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
347                 cmd = CMD_WRITE;
348                 device = argv[2];
349                 /* check trx file before erasing or writing anything */
350                 trxfd = trx_check(argv[1], device, force);
351         } else {
352                 usage();
353         }
354
355         sync();
356
357         i = 0;
358         while (erase[i] != NULL) {
359                 mtd_unlock(erase[i]);
360                 mtd_erase(erase[i]);
361                 i++;
362         }
363         
364         mtd_unlock(device);
365
366         switch (cmd) {
367                 case CMD_UNLOCK:
368                         break;
369                 case CMD_ERASE:
370                         mtd_erase(device);
371                         break;
372                 case CMD_WRITE:
373                         mtd_write(trxfd, device);
374                         break;
375         }
376
377         if (boot)
378                 kill(1, 15); // send SIGTERM to init for reboot
379
380         return 0;
381 }