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