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