cfb03f881a7f7269897e8307c4aafe466096c205
[openwrt.git] / tools / firmware-utils / src / mktplinkfw.c
1 /*
2  * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
3  *
4  * This tool was based on:
5  *   TP-Link WR941 V2 firmware checksum fixing tool.
6  *   Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published
10  * by the Free Software Foundation.
11  *
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <unistd.h>     /* for unlink() */
19 #include <libgen.h>
20 #include <getopt.h>     /* for getopt() */
21 #include <stdarg.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24
25 #include "md5.h"
26
27 #if (__BYTE_ORDER == __BIG_ENDIAN)
28 #  define HOST_TO_BE32(x)       (x)
29 #  define BE32_TO_HOST(x)       (x)
30 #else
31 #  define HOST_TO_BE32(x)       bswap_32(x)
32 #  define BE32_TO_HOST(x)       bswap_32(x)
33 #endif
34
35 #define HEADER_VERSION_V1       0x01000000
36 #define HWID_TL_WR741ND_V1      0x07410001
37 #define HWID_TL_WR841N_V1_5     0x08410002
38 #define HWID_TL_WR841ND_V3      0x08410003
39 #define HWID_TL_WR841ND_V5      0x08410005
40 #define HWID_TL_WR941ND_V2      0x09410002
41 #define HWID_TL_WR941ND_V4      0x09410004
42 #define HWID_TL_WR1043ND_V1     0x10430001
43
44 #define MD5SUM_LEN      16
45
46 struct file_info {
47         char            *file_name;     /* name of the file */
48         uint32_t        file_size;      /* length of the file */
49 };
50
51 struct fw_header {
52         uint32_t        version;        /* header version */
53         char            vendor_name[24];
54         char            fw_version[36];
55         uint32_t        hw_id;          /* hardware id */
56         uint32_t        hw_rev;         /* hardware revision */
57         uint32_t        unk1;
58         uint8_t         md5sum1[MD5SUM_LEN];
59         uint32_t        unk2;
60         uint8_t         md5sum2[MD5SUM_LEN];
61         uint32_t        unk3;
62         uint32_t        kernel_la;      /* kernel load address */
63         uint32_t        kernel_ep;      /* kernel entry point */
64         uint32_t        fw_length;      /* total length of the firmware */
65         uint32_t        kernel_ofs;     /* kernel data offset */
66         uint32_t        kernel_len;     /* kernel data length */
67         uint32_t        rootfs_ofs;     /* rootfs data offset */
68         uint32_t        rootfs_len;     /* rootfs data length */
69         uint32_t        boot_ofs;       /* bootloader data offset */
70         uint32_t        boot_len;       /* bootloader data length */
71         uint8_t         pad[360];
72 } __attribute__ ((packed));
73
74 struct board_info {
75         char            *id;
76         uint32_t        hw_id;
77         uint32_t        hw_rev;
78         uint32_t        fw_max_len;
79         uint32_t        kernel_la;
80         uint32_t        kernel_ep;
81         uint32_t        rootfs_ofs;
82 };
83
84 /*
85  * Globals
86  */
87 static char *ofname;
88 static char *progname;
89 static char *vendor = "TP-LINK Technologies";
90 static char *version = "ver. 1.0";
91
92 static char *board_id;
93 static struct board_info *board;
94 static struct file_info kernel_info;
95 static struct file_info rootfs_info;
96 static struct file_info boot_info;
97 static int combined;
98 static int strip_padding;
99
100 char md5salt_normal[MD5SUM_LEN] = {
101         0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
102         0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
103 };
104
105 char md5salt_boot[MD5SUM_LEN] = {
106         0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
107         0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
108 };
109
110 static struct board_info boards[] = {
111         {
112                 .id             = "TL-WR741NDv1",
113                 .hw_id          = HWID_TL_WR741ND_V1,
114                 .hw_rev         = 1,
115                 .fw_max_len     = 0x3c0000,
116                 .kernel_la      = 0x80060000,
117                 .kernel_ep      = 0x80060000,
118                 .rootfs_ofs     = 0x140000,
119         }, {
120                 .id             = "TL-WR841Nv1.5",
121                 .hw_id          = HWID_TL_WR841N_V1_5,
122                 .hw_rev         = 2,
123                 .fw_max_len     = 0x3c0000,
124                 .kernel_la      = 0x80060000,
125                 .kernel_ep      = 0x80060000,
126                 .rootfs_ofs     = 0x140000,
127         }, {
128                 .id             = "TL-WR841NDv3",
129                 .hw_id          = HWID_TL_WR841ND_V3,
130                 .hw_rev         = 3,
131                 .fw_max_len     = 0x3c0000,
132                 .kernel_la      = 0x80060000,
133                 .kernel_ep      = 0x80060000,
134                 .rootfs_ofs     = 0x140000,
135         }, {
136                 .id             = "TL-WR841NDv5",
137                 .hw_id          = HWID_TL_WR841ND_V5,
138                 .hw_rev         = 1,
139                 .fw_max_len     = 0x3c0000,
140                 .kernel_la      = 0x80060000,
141                 .kernel_ep      = 0x80060000,
142                 .rootfs_ofs     = 0x140000,
143         }, {
144                 .id             = "TL-WR941NDv2",
145                 .hw_id          = HWID_TL_WR941ND_V2,
146                 .hw_rev         = 2,
147                 .fw_max_len     = 0x3c0000,
148                 .kernel_la      = 0x80060000,
149                 .kernel_ep      = 0x80060000,
150                 .rootfs_ofs     = 0x140000,
151         }, {
152                 .id             = "TL-WR941NDv4",
153                 .hw_id          = HWID_TL_WR941ND_V4,
154                 .hw_rev         = 1,
155                 .fw_max_len     = 0x3c0000,
156                 .kernel_la      = 0x80060000,
157                 .kernel_ep      = 0x80060000,
158                 .rootfs_ofs     = 0x140000,
159         }, {
160                 .id             = "TL-WR1043NDv1",
161                 .hw_id          = HWID_TL_WR1043ND_V1,
162                 .hw_rev         = 1,
163                 .fw_max_len     = 0x7c0000,
164                 .kernel_la      = 0x80060000,
165                 .kernel_ep      = 0x80060000,
166                 .rootfs_ofs     = 0x140000,
167         }, {
168                 /* terminating entry */
169         }
170 };
171
172 /*
173  * Message macros
174  */
175 #define ERR(fmt, ...) do { \
176         fflush(0); \
177         fprintf(stderr, "[%s] *** error: " fmt "\n", \
178                         progname, ## __VA_ARGS__ ); \
179 } while (0)
180
181 #define ERRS(fmt, ...) do { \
182         int save = errno; \
183         fflush(0); \
184         fprintf(stderr, "[%s] *** error: " fmt "\n", \
185                         progname, ## __VA_ARGS__, strerror(save)); \
186 } while (0)
187
188 #define DBG(fmt, ...) do { \
189         fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
190 } while (0)
191
192 static struct board_info *find_board(char *id)
193 {
194         struct board_info *ret;
195         struct board_info *board;
196
197         ret = NULL;
198         for (board = boards; board->id != NULL; board++){
199                 if (strcasecmp(id, board->id) == 0) {
200                         ret = board;
201                         break;
202                 }
203         };
204
205         return ret;
206 }
207
208 static void usage(int status)
209 {
210         FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
211         struct board_info *board;
212
213         fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
214         fprintf(stream,
215 "\n"
216 "Options:\n"
217 "  -B <board>      create image for the board specified with <board>\n"
218 "  -c              use combined kernel image\n"
219 "  -k <file>       read kernel image from the file <file>\n"
220 "  -r <file>       read rootfs image from the file <file>\n"
221 "  -o <file>       write output to the file <file>\n"
222 "  -s              strip padding from the end of the image\n"
223 "  -N <vendor>     set image vendor to <vendor>\n"
224 "  -V <version>    set image version to <version>\n"
225 "  -h              show this screen\n"
226         );
227
228         exit(status);
229 }
230
231 static int get_md5(char *data, int size, char *md5)
232 {
233         MD5_CTX ctx;
234
235         MD5_Init(&ctx);
236         MD5_Update(&ctx, data, size);
237         MD5_Final(md5, &ctx);
238 }
239
240 static int get_file_stat(struct file_info *fdata)
241 {
242         struct stat st;
243         int res;
244
245         if (fdata->file_name == NULL)
246                 return 0;
247
248         res = stat(fdata->file_name, &st);
249         if (res){
250                 ERRS("stat failed on %s", fdata->file_name);
251                 return res;
252         }
253
254         fdata->file_size = st.st_size;
255         return 0;
256 }
257
258 static int read_to_buf(struct file_info *fdata, char *buf)
259 {
260         FILE *f;
261         int ret = EXIT_FAILURE;
262
263         f = fopen(fdata->file_name, "r");
264         if (f == NULL) {
265                 ERRS("could not open \"%s\" for reading", fdata->file_name);
266                 goto out;
267         }
268
269         errno = 0;
270         fread(buf, fdata->file_size, 1, f);
271         if (errno != 0) {
272                 ERRS("unable to read from file \"%s\"", fdata->file_name);
273                 goto out_close;
274         }
275
276         ret = EXIT_SUCCESS;
277
278  out_close:
279         fclose(f);
280  out:
281         return ret;
282 }
283
284 static int check_options(void)
285 {
286         int ret;
287
288         if (board_id == NULL) {
289                 ERR("no board specified");
290                 return -1;
291         }
292
293         board = find_board(board_id);
294         if (board == NULL) {
295                 ERR("unknown/unsupported board id \"%s\"", board_id);
296                 return -1;
297         }
298
299         if (kernel_info.file_name == NULL) {
300                 ERR("no kernel image specified");
301                 return -1;
302         }
303
304         ret = get_file_stat(&kernel_info);
305         if (ret)
306                 return ret;
307
308         if (combined) {
309                 if (kernel_info.file_size >
310                     board->fw_max_len - sizeof(struct fw_header)) {
311                         ERR("kernel image is too big");
312                         return -1;
313                 }
314         } else {
315                 if (kernel_info.file_size >
316                     board->rootfs_ofs - sizeof(struct fw_header)) {
317                         ERR("kernel image is too big");
318                         return -1;
319                 }
320                 if (rootfs_info.file_name == NULL) {
321                         ERR("no rootfs image specified");
322                         return -1;
323                 }
324
325                 ret = get_file_stat(&rootfs_info);
326                 if (ret)
327                         return ret;
328
329                 if (rootfs_info.file_size >
330                     (board->fw_max_len - board->rootfs_ofs)) {
331                         ERR("rootfs image is too big");
332                         return -1;
333                 }
334         }
335
336         if (ofname == NULL) {
337                 ERR("no output file specified");
338                 return -1;
339         }
340
341         return 0;
342 }
343
344 static void fill_header(char *buf, int len)
345 {
346         struct fw_header *hdr = (struct fw_header *)buf;
347
348         memset(hdr, 0, sizeof(struct fw_header));
349
350         hdr->version = HOST_TO_BE32(HEADER_VERSION_V1);
351         strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
352         strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
353         hdr->hw_id = HOST_TO_BE32(board->hw_id);
354         hdr->hw_rev = HOST_TO_BE32(board->hw_rev);
355
356         if (boot_info.file_size == 0)
357                 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
358         else
359                 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
360
361         hdr->kernel_la = HOST_TO_BE32(board->kernel_la);
362         hdr->kernel_ep = HOST_TO_BE32(board->kernel_ep);
363         hdr->fw_length = HOST_TO_BE32(board->fw_max_len);
364         hdr->kernel_ofs = HOST_TO_BE32(sizeof(struct fw_header));
365         hdr->kernel_len = HOST_TO_BE32(kernel_info.file_size);
366         if (!combined) {
367                 hdr->rootfs_ofs = HOST_TO_BE32(board->rootfs_ofs);
368                 hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
369         }
370
371         get_md5(buf, len, hdr->md5sum1);
372 }
373
374 static int write_fw(char *data, int len)
375 {
376         FILE *f;
377         int ret = EXIT_FAILURE;
378
379         f = fopen(ofname, "w");
380         if (f == NULL) {
381                 ERRS("could not open \"%s\" for writing", ofname);
382                 goto out;
383         }
384
385         errno = 0;
386         fwrite(data, len, 1, f);
387         if (errno) {
388                 ERRS("unable to write output file");
389                 goto out_flush;
390         }
391
392         DBG("firmware file \"%s\" completed", ofname);
393
394         ret = EXIT_SUCCESS;
395
396  out_flush:
397         fflush(f);
398         fclose(f);
399         if (ret != EXIT_SUCCESS) {
400                 unlink(ofname);
401         }
402  out:
403         return ret;
404 }
405
406 static int build_fw(void)
407 {
408         int buflen;
409         char *buf;
410         char *p;
411         int ret = EXIT_FAILURE;
412         int writelen = 0;
413
414         buflen = board->fw_max_len;
415
416         buf = malloc(buflen);
417         if (!buf) {
418                 ERR("no memory for buffer\n");
419                 goto out;
420         }
421
422         memset(buf, 0xff, buflen);
423         p = buf + sizeof(struct fw_header);
424         ret = read_to_buf(&kernel_info, p);
425         if (ret)
426                 goto out_free_buf;
427
428         writelen = kernel_info.file_size;
429
430         if (!combined) {
431                 p = buf + board->rootfs_ofs;
432                 ret = read_to_buf(&rootfs_info, p);
433                 if (ret)
434                         goto out_free_buf;
435
436                 writelen = board->rootfs_ofs + rootfs_info.file_size;
437         }
438
439         if (!strip_padding)
440                 writelen = buflen;
441
442         fill_header(buf, writelen);
443         ret = write_fw(buf, writelen);
444         if (ret)
445                 goto out_free_buf;
446
447         ret = EXIT_SUCCESS;
448
449  out_free_buf:
450         free(buf);
451  out:
452         return ret;
453 }
454
455 int main(int argc, char *argv[])
456 {
457         int ret = EXIT_FAILURE;
458         int err;
459
460         FILE *outfile;
461
462         progname = basename(argv[0]);
463
464         while ( 1 ) {
465                 int c;
466
467                 c = getopt(argc, argv, "B:V:N:ck:r:o:hs");
468                 if (c == -1)
469                         break;
470
471                 switch (c) {
472                 case 'B':
473                         board_id = optarg;
474                         break;
475                 case 'V':
476                         version = optarg;
477                         break;
478                 case 'N':
479                         vendor = optarg;
480                         break;
481                 case 'c':
482                         combined++;
483                         break;
484                 case 'k':
485                         kernel_info.file_name = optarg;
486                         break;
487                 case 'r':
488                         rootfs_info.file_name = optarg;
489                         break;
490                 case 'o':
491                         ofname = optarg;
492                         break;
493                 case 's':
494                         strip_padding = 1;
495                         break;
496                 case 'h':
497                         usage(EXIT_SUCCESS);
498                         break;
499                 default:
500                         usage(EXIT_FAILURE);
501                         break;
502                 }
503         }
504
505         ret = check_options();
506         if (ret)
507                 goto out;
508
509         ret = build_fw();
510
511  out:
512         return ret;
513 }
514