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