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