firmware-utils/mktplinkfw: add board definitions for TL-WR941ND v4
[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
99 char md5salt_normal[MD5SUM_LEN] = {
100         0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
101         0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
102 };
103
104 char md5salt_boot[MD5SUM_LEN] = {
105         0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
106         0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
107 };
108
109 static struct board_info boards[] = {
110         {
111                 .id             = "TL-WR741NDv1",
112                 .hw_id          = HWID_TL_WR741ND_V1,
113                 .hw_rev         = 1,
114                 .fw_max_len     = 0x3c0000,
115                 .kernel_la      = 0x80060000,
116                 .kernel_ep      = 0x80060000,
117                 .rootfs_ofs     = 0x140000,
118         }, {
119                 .id             = "TL-WR841Nv1.5",
120                 .hw_id          = HWID_TL_WR841N_V1_5,
121                 .hw_rev         = 2,
122                 .fw_max_len     = 0x3c0000,
123                 .kernel_la      = 0x80060000,
124                 .kernel_ep      = 0x80060000,
125                 .rootfs_ofs     = 0x140000,
126         }, {
127                 .id             = "TL-WR841NDv3",
128                 .hw_id          = HWID_TL_WR841ND_V3,
129                 .hw_rev         = 3,
130                 .fw_max_len     = 0x3c0000,
131                 .kernel_la      = 0x80060000,
132                 .kernel_ep      = 0x80060000,
133                 .rootfs_ofs     = 0x140000,
134         }, {
135                 .id             = "TL-WR841NDv5",
136                 .hw_id          = HWID_TL_WR841ND_V5,
137                 .hw_rev         = 1,
138                 .fw_max_len     = 0x3c0000,
139                 .kernel_la      = 0x80060000,
140                 .kernel_ep      = 0x80060000,
141                 .rootfs_ofs     = 0x140000,
142         }, {
143                 .id             = "TL-WR941NDv2",
144                 .hw_id          = HWID_TL_WR941ND_V2,
145                 .hw_rev         = 2,
146                 .fw_max_len     = 0x3c0000,
147                 .kernel_la      = 0x80060000,
148                 .kernel_ep      = 0x80060000,
149                 .rootfs_ofs     = 0x140000,
150         }, {
151                 .id             = "TL-WR941NDv4",
152                 .hw_id          = HWID_TL_WR941ND_V4,
153                 .hw_rev         = 1,
154                 .fw_max_len     = 0x3c0000,
155                 .kernel_la      = 0x80060000,
156                 .kernel_ep      = 0x80060000,
157                 .rootfs_ofs     = 0x140000,
158         }, {
159                 .id             = "TL-WR1043NDv1",
160                 .hw_id          = HWID_TL_WR1043ND_V1,
161                 .hw_rev         = 1,
162                 .fw_max_len     = 0x7c0000,
163                 .kernel_la      = 0x80060000,
164                 .kernel_ep      = 0x80060000,
165                 .rootfs_ofs     = 0x140000,
166         }, {
167                 /* terminating entry */
168         }
169 };
170
171 /*
172  * Message macros
173  */
174 #define ERR(fmt, ...) do { \
175         fflush(0); \
176         fprintf(stderr, "[%s] *** error: " fmt "\n", \
177                         progname, ## __VA_ARGS__ ); \
178 } while (0)
179
180 #define ERRS(fmt, ...) do { \
181         int save = errno; \
182         fflush(0); \
183         fprintf(stderr, "[%s] *** error: " fmt "\n", \
184                         progname, ## __VA_ARGS__, strerror(save)); \
185 } while (0)
186
187 #define DBG(fmt, ...) do { \
188         fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
189 } while (0)
190
191 static struct board_info *find_board(char *id)
192 {
193         struct board_info *ret;
194         struct board_info *board;
195
196         ret = NULL;
197         for (board = boards; board->id != NULL; board++){
198                 if (strcasecmp(id, board->id) == 0) {
199                         ret = board;
200                         break;
201                 }
202         };
203
204         return ret;
205 }
206
207 static void usage(int status)
208 {
209         FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
210         struct board_info *board;
211
212         fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
213         fprintf(stream,
214 "\n"
215 "Options:\n"
216 "  -B <board>      create image for the board specified with <board>\n"
217 "  -c              use combined kernel image\n"
218 "  -k <file>       read kernel image from the file <file>\n"
219 "  -r <file>       read rootfs image from the file <file>\n"
220 "  -o <file>       write output to the file <file>\n"
221 "  -v <version>    set image version to <version>\n"
222 "  -h              show this screen\n"
223         );
224
225         exit(status);
226 }
227
228 static int get_md5(char *data, int size, char *md5)
229 {
230         MD5_CTX ctx;
231
232         MD5_Init(&ctx);
233         MD5_Update(&ctx, data, size);
234         MD5_Final(md5, &ctx);
235 }
236
237 static int get_file_stat(struct file_info *fdata)
238 {
239         struct stat st;
240         int res;
241
242         if (fdata->file_name == NULL)
243                 return 0;
244
245         res = stat(fdata->file_name, &st);
246         if (res){
247                 ERRS("stat failed on %s", fdata->file_name);
248                 return res;
249         }
250
251         fdata->file_size = st.st_size;
252         return 0;
253 }
254
255 static int read_to_buf(struct file_info *fdata, char *buf)
256 {
257         FILE *f;
258         int ret = EXIT_FAILURE;
259
260         f = fopen(fdata->file_name, "r");
261         if (f == NULL) {
262                 ERRS("could not open \"%s\" for reading", fdata->file_name);
263                 goto out;
264         }
265
266         errno = 0;
267         fread(buf, fdata->file_size, 1, f);
268         if (errno != 0) {
269                 ERRS("unable to read from file \"%s\"", fdata->file_name);
270                 goto out_close;
271         }
272
273         ret = EXIT_SUCCESS;
274
275  out_close:
276         fclose(f);
277  out:
278         return ret;
279 }
280
281 static int check_options(void)
282 {
283         int ret;
284
285         if (board_id == NULL) {
286                 ERR("no board specified");
287                 return -1;
288         }
289
290         board = find_board(board_id);
291         if (board == NULL) {
292                 ERR("unknown/unsupported board id \"%s\"", board_id);
293                 return -1;
294         }
295
296         if (kernel_info.file_name == NULL) {
297                 ERR("no kernel image specified");
298                 return -1;
299         }
300
301         ret = get_file_stat(&kernel_info);
302         if (ret)
303                 return ret;
304
305         if (combined) {
306                 if (kernel_info.file_size >
307                     board->fw_max_len - sizeof(struct fw_header)) {
308                         ERR("kernel image is too big");
309                         return -1;
310                 }
311         } else {
312                 if (kernel_info.file_size >
313                     board->rootfs_ofs - sizeof(struct fw_header)) {
314                         ERR("kernel image is too big");
315                         return -1;
316                 }
317                 if (rootfs_info.file_name == NULL) {
318                         ERR("no rootfs image specified");
319                         return -1;
320                 }
321
322                 ret = get_file_stat(&rootfs_info);
323                 if (ret)
324                         return ret;
325
326                 if (rootfs_info.file_size >
327                     (board->fw_max_len - board->rootfs_ofs)) {
328                         ERR("rootfs image is too big");
329                         return -1;
330                 }
331         }
332
333         if (ofname == NULL) {
334                 ERR("no output file specified");
335                 return -1;
336         }
337
338         return 0;
339 }
340
341 static void fill_header(char *buf, int len)
342 {
343         struct fw_header *hdr = (struct fw_header *)buf;
344
345         memset(hdr, 0, sizeof(struct fw_header));
346
347         hdr->version = HOST_TO_BE32(HEADER_VERSION_V1);
348         strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
349         strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
350         hdr->hw_id = HOST_TO_BE32(board->hw_id);
351         hdr->hw_rev = HOST_TO_BE32(board->hw_rev);
352
353         if (boot_info.file_size == 0)
354                 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
355         else
356                 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
357
358         hdr->kernel_la = HOST_TO_BE32(board->kernel_la);
359         hdr->kernel_ep = HOST_TO_BE32(board->kernel_ep);
360         hdr->fw_length = HOST_TO_BE32(board->fw_max_len);
361         hdr->kernel_ofs = HOST_TO_BE32(sizeof(struct fw_header));
362         hdr->kernel_len = HOST_TO_BE32(kernel_info.file_size);
363         if (!combined) {
364                 hdr->rootfs_ofs = HOST_TO_BE32(board->rootfs_ofs);
365                 hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
366         }
367
368         get_md5(buf, len, hdr->md5sum1);
369 }
370
371 static int write_fw(char *data, int len)
372 {
373         FILE *f;
374         int ret = EXIT_FAILURE;
375
376         f = fopen(ofname, "w");
377         if (f == NULL) {
378                 ERRS("could not open \"%s\" for writing", ofname);
379                 goto out;
380         }
381
382         errno = 0;
383         fwrite(data, len, 1, f);
384         if (errno) {
385                 ERRS("unable to write output file");
386                 goto out_flush;
387         }
388
389         DBG("firmware file \"%s\" completed", ofname);
390
391         ret = EXIT_SUCCESS;
392
393  out_flush:
394         fflush(f);
395         fclose(f);
396         if (ret != EXIT_SUCCESS) {
397                 unlink(ofname);
398         }
399  out:
400         return ret;
401 }
402
403 static int build_fw(void)
404 {
405         int buflen;
406         char *buf;
407         char *p;
408         int ret = EXIT_FAILURE;
409
410         buflen = board->fw_max_len;
411
412         buf = malloc(buflen);
413         if (!buf) {
414                 ERR("no memory for buffer\n");
415                 goto out;
416         }
417
418         memset(buf, 0xff, buflen);
419         p = buf + sizeof(struct fw_header);
420         ret = read_to_buf(&kernel_info, p);
421         if (ret)
422                 goto out_free_buf;
423
424         if (!combined) {
425                 p = buf + board->rootfs_ofs;
426                 ret = read_to_buf(&rootfs_info, p);
427                 if (ret)
428                         goto out_free_buf;
429         }
430
431         fill_header(buf, buflen);
432
433         ret = write_fw(buf, buflen);
434         if (ret)
435                 goto out_free_buf;
436
437         ret = EXIT_SUCCESS;
438
439  out_free_buf:
440         free(buf);
441  out:
442         return ret;
443 }
444
445 int main(int argc, char *argv[])
446 {
447         int ret = EXIT_FAILURE;
448         int err;
449
450         FILE *outfile;
451
452         progname = basename(argv[0]);
453
454         while ( 1 ) {
455                 int c;
456
457                 c = getopt(argc, argv, "B:V:N:ck:r:o:v:h:");
458                 if (c == -1)
459                         break;
460
461                 switch (c) {
462                 case 'B':
463                         board_id = optarg;
464                         break;
465                 case 'V':
466                         version = optarg;
467                         break;
468                 case 'N':
469                         vendor = optarg;
470                         break;
471                 case 'c':
472                         combined++;
473                         break;
474                 case 'k':
475                         kernel_info.file_name = optarg;
476                         break;
477                 case 'r':
478                         rootfs_info.file_name = optarg;
479                         break;
480                 case 'o':
481                         ofname = optarg;
482                         break;
483                 case 'v':
484                         version = optarg;
485                         break;
486                 case 'h':
487                         usage(EXIT_SUCCESS);
488                         break;
489                 default:
490                         usage(EXIT_FAILURE);
491                         break;
492                 }
493         }
494
495         ret = check_options();
496         if (ret)
497                 goto out;
498
499         ret = build_fw();
500
501  out:
502         return ret;
503 }
504