check if firmware is in trx format, before writing it to the flash
[openwrt.git] / package / openwrt / 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 #include <linux/mtd/mtd.h>
44
45 #define TRX_MAGIC       0x30524448      /* "HDR0" */
46 #define BUFSIZE (10 * 1024)
47 #define MAX_ARGS 8
48
49 struct trx_header {
50         uint32_t magic;         /* "HDR0" */
51         uint32_t len;           /* Length of file including header */
52         uint32_t crc32;         /* 32-bit CRC from flag_version to end of file */
53         uint32_t flag_version;  /* 0:15 flags, 16:31 version */
54         uint32_t offsets[3];    /* Offsets of partitions from start of header */
55 };
56
57 int
58 mtd_unlock(const char *mtd)
59 {
60         int fd;
61         struct mtd_info_user mtdInfo;
62         struct erase_info_user mtdLockInfo;
63
64         fd = mtd_open(mtd, O_RDWR);
65         if(fd < 0) {
66                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
67                 exit(1);
68         }
69
70         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
71                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
72                 close(fd);
73                 exit(1);
74         }
75
76         printf("Unlocking %s ...\n", mtd);
77         mtdLockInfo.start = 0;
78         mtdLockInfo.length = mtdInfo.size;
79         if(ioctl(fd, MEMUNLOCK, &mtdLockInfo)) {
80                 close(fd);
81                 return 0;
82         }
83                 
84         close(fd);
85         return 0;
86 }
87
88 int
89 mtd_open(const char *mtd, int flags)
90 {
91         FILE *fp;
92         char dev[PATH_MAX];
93         int i;
94
95         if ((fp = fopen("/proc/mtd", "r"))) {
96                 while (fgets(dev, sizeof(dev), fp)) {
97                         if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
98                                 snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
99                                 fclose(fp);
100                                 return open(dev, flags);
101                         }
102                 }
103                 fclose(fp);
104         }
105
106         return open(mtd, flags);
107 }
108
109 int
110 mtd_erase(const char *mtd)
111 {
112         int fd;
113         struct mtd_info_user mtdInfo;
114         struct erase_info_user mtdEraseInfo;
115
116         fd = mtd_open(mtd, O_RDWR);
117         if(fd < 0) {
118                 fprintf(stderr, "Could not open mtd device: %s\n", mtd);
119                 exit(1);
120         }
121
122         if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
123                 fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
124                 close(fd);
125                 exit(1);
126         }
127
128         printf("Erasing %s ...\n", mtd);
129         mtdEraseInfo.length = mtdInfo.erasesize;
130
131         for (mtdEraseInfo.start = 0;
132                  mtdEraseInfo.start < mtdInfo.size;
133                  mtdEraseInfo.start += mtdInfo.erasesize) {
134                 
135                 ioctl(fd, MEMUNLOCK, &mtdEraseInfo);
136                 if(ioctl(fd, MEMERASE, &mtdEraseInfo)) {
137                         fprintf(stderr, "Could not erase MTD device: %s\n", mtd);
138                         close(fd);
139                         exit(1);
140                 }
141         }               
142
143         close(fd);
144         return 0;
145
146 }
147
148 int
149 mtd_write(const char *trxfile, const char *mtd)
150 {
151         int fd,trxfd,i;
152         struct trx_header trx;
153         size_t count,result,size,written;
154         struct mtd_info_user mtdInfo;
155         struct erase_info_user mtdEraseInfo;
156         struct stat trxstat;
157         unsigned char src[BUFSIZE],dest[BUFSIZE];
158
159         trxfd = open(trxfile,O_RDONLY); 
160         if(trxfd < 0) {
161                 fprintf(stderr, "Could not open trx image: %s\n", trxfile);
162                 exit(1);
163         }
164
165         if (fstat(trxfd,&trxstat) < 0) {
166                 fprintf(stderr, "Could not get trx image file status: %s\n", trxfile);
167                 close(trxfd);
168                 exit(1);
169         }
170
171         count = read(trxfd, &trx, sizeof(struct trx_header));
172         if (count < sizeof(struct trx_header)) {
173                 fprintf(stderr, "Could not trx header, file too small (%ld bytes)\n", count);
174                 close(trxfd);
175                 exit(1);
176         }
177
178         if (trx.magic != TRX_MAGIC || trx.len < sizeof(struct trx_header)) {
179                 fprintf(stderr, "Bad trx header\n");
180                 fprintf(stderr, "If this is a firmware in bin format, like some of the\n"
181                                 "original firmware files are, use following command to convert to trx:\n"
182                                 "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
183                 close(trxfd);
184                 exit(1);
185         }
186         
187         lseek(trxfd, 0, SEEK_SET);
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         if(mtdInfo.size < trxstat.st_size) {
202                 fprintf(stderr, "Image too big for partition: %s\n", mtd);
203                 close(trxfd);
204                 exit(1);
205         }       
206         
207         printf("Writing %s to %s ...\n", trxfile, mtd);
208
209         mtdEraseInfo.start = 0;
210         mtdEraseInfo.length = trxstat.st_size & ~(mtdInfo.erasesize -1);
211         if(trxstat.st_size % mtdInfo.erasesize) mtdEraseInfo.length += mtdInfo.erasesize;
212
213         /* erase the chunk */
214         if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
215                 fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
216                 exit(1);
217         }
218         
219         size = trxstat.st_size;
220         i = BUFSIZE;
221         written = 0;
222
223         while (size) {
224                 if (size < BUFSIZE) i = size;
225                 read(trxfd,src,i);
226                 result = write(fd,src,i);
227                 if (i != result) {
228                         if (result < 0) {
229                                 fprintf(stderr,"Error while writing image");
230                                 exit(1);
231                         }
232                         fprintf(stderr,"Error writing image");
233                         exit(1);
234                 }
235                 written += i;
236                 size -= i;
237         }
238         
239         return 0;
240 }
241
242 void usage(void)
243 {
244         printf("Usage: mtd [<options> ...] <command> [<arguments> ...] <device>\n\n"
245         "The device is in the format of mtdX (eg: mtd4) or its label.\n"
246         "mtd recognizes these commands:\n"
247         "       unlock                  unlock the device\n"
248         "       erase                   erase all data on device\n"
249         "       write <imagefile>       write imagefile to device\n"
250         "Following options are available:\n"
251         "       -r                      reboot after successful command\n"
252         "       -e <device>             erase <device> before executing the command\n\n"
253         "Example: To write linux.trx to mtd4 labeled as linux and reboot afterwards\n"
254         "         mtd -r write linux.trx linux\n\n");
255         exit(1);
256 }
257
258 int main (int argc, char **argv)
259 {
260         int ch, i, boot, unlock;
261         char *erase[MAX_ARGS], *device;
262         enum {
263                 CMD_ERASE,
264                 CMD_WRITE,
265                 CMD_UNLOCK
266         } cmd;
267         
268         erase[0] = NULL;
269         boot = 0;
270
271         while ((ch = getopt(argc, argv, "re:")) != -1)
272                 switch (ch) {
273                         case 'r':
274                                 boot = 1;
275                                 break;
276                         case 'e':
277                                 i = 0;
278                                 while ((erase[i] != NULL) && ((i + 1) < MAX_ARGS))
279                                         i++;
280                                         
281                                 erase[i++] = optarg;
282                                 erase[i] = NULL;
283                                 break;
284                         
285                         case '?':
286                         default:
287                                 usage();
288                 }
289         argc -= optind;
290         argv += optind;
291         
292         if (argc < 2)
293                 usage();
294
295         if ((strcmp(argv[0], "unlock") == 0) && (argc == 2)) {
296                 cmd = CMD_UNLOCK;
297                 device = argv[1];
298         } else if ((strcmp(argv[0], "erase") == 0) && (argc == 2)) {
299                 cmd = CMD_ERASE;
300                 device = argv[1];
301         } else if ((strcmp(argv[0], "write") == 0) && (argc == 3)) {
302                 cmd = CMD_WRITE;
303                 device = argv[2];
304         } else {
305                 usage();
306         }
307
308         sync();
309
310         i = 0;
311         while (erase[i] != NULL) {
312                 mtd_unlock(erase[i]);
313                 mtd_erase(erase[i]);
314                 i++;
315         }
316         
317         mtd_unlock(device);
318
319         switch (cmd) {
320                 case CMD_UNLOCK:
321                         break;
322                 case CMD_ERASE:
323                         mtd_erase(device);
324                         break;
325                 case CMD_WRITE:
326                         mtd_write(argv[1], device);
327                         break;
328         }
329
330         if (boot)
331                 kill(1, 15); // send SIGTERM to init for reboot
332
333         return 0;
334 }