053ff0050b145ab02fff8ad1b7ca46030e848d18
[openwrt/staging/ldir.git] / tools / firmware-utils / src / mktplinkfw2.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 <stdbool.h>
24 #include <endian.h>
25 #include <sys/stat.h>
26
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29
30 #include "md5.h"
31 #include "mktplinkfw-lib.h"
32
33 struct fw_header {
34 uint32_t version; /* 0x00: header version */
35 char fw_version[48]; /* 0x04: fw version string */
36 uint32_t hw_id; /* 0x34: hardware id */
37 uint32_t hw_rev; /* 0x38: FIXME: hardware revision? */
38 uint32_t hw_ver_add; /* 0x3c: additional hardware version */
39 uint8_t md5sum1[MD5SUM_LEN]; /* 0x40 */
40 uint32_t unk2; /* 0x50: 0x00000000 */
41 uint8_t md5sum2[MD5SUM_LEN]; /* 0x54 */
42 uint32_t unk3; /* 0x64: 0xffffffff */
43
44 uint32_t kernel_la; /* 0x68: kernel load address */
45 uint32_t kernel_ep; /* 0x6c: kernel entry point */
46 uint32_t fw_length; /* 0x70: total length of the image */
47 uint32_t kernel_ofs; /* 0x74: kernel data offset */
48 uint32_t kernel_len; /* 0x78: kernel data length */
49 uint32_t rootfs_ofs; /* 0x7c: rootfs data offset */
50 uint32_t rootfs_len; /* 0x80: rootfs data length */
51 uint32_t boot_ofs; /* 0x84: bootloader offset */
52 uint32_t boot_len; /* 0x88: bootloader length */
53 uint16_t unk4; /* 0x8c: 0x55aa */
54 uint8_t sver_hi; /* 0x8e */
55 uint8_t sver_lo; /* 0x8f */
56 uint8_t unk5; /* 0x90: magic: 0xa5 */
57 uint8_t ver_hi; /* 0x91 */
58 uint8_t ver_mid; /* 0x92 */
59 uint8_t ver_lo; /* 0x93 */
60 uint8_t pad[364];
61 } __attribute__ ((packed));
62
63 #define FLAG_LE_KERNEL_LA_EP 0x00000001 /* Little-endian used for kernel load address & entry point */
64
65 struct board_info {
66 char *id;
67 uint32_t hw_id;
68 uint32_t hw_rev;
69 uint32_t hw_ver_add;
70 char *layout_id;
71 uint32_t hdr_ver;
72 uint32_t flags;
73 };
74
75 /*
76 * Globals
77 */
78 char *ofname;
79 char *progname;
80 static char *vendor = "TP-LINK Technologies";
81 static char *version = "ver. 1.0";
82 static char *fw_ver = "0.0.0";
83 static char *sver = "1.0";
84 static uint32_t hdr_ver = 2;
85
86 static struct board_info custom_board;
87
88 static struct board_info *board;
89 static char *layout_id;
90 struct flash_layout *layout;
91 static char *opt_hw_id;
92 static char *opt_hw_rev;
93 static char *opt_hw_ver_add;
94 static int fw_ver_lo;
95 static int fw_ver_mid;
96 static int fw_ver_hi;
97 static int sver_lo;
98 static int sver_hi;
99 struct file_info kernel_info;
100 static uint32_t kernel_la = 0;
101 static uint32_t kernel_ep = 0;
102 uint32_t kernel_len = 0;
103 struct file_info rootfs_info;
104 uint32_t rootfs_ofs = 0;
105 uint32_t rootfs_align;
106 static struct file_info boot_info;
107 int combined;
108 int strip_padding;
109 int add_jffs2_eof;
110
111 static struct file_info inspect_info;
112 static int extract = 0;
113
114 char md5salt_normal[MD5SUM_LEN] = {
115 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
116 0xdc, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x37,
117 };
118
119 char md5salt_boot[MD5SUM_LEN] = {
120 0x8c, 0xef, 0x33, 0x5f, 0xd5, 0xc5, 0xce, 0xfa,
121 0xac, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
122 };
123
124 static struct flash_layout layouts[] = {
125 {
126 .id = "4Mmtk",
127 .fw_max_len = 0x3c0000,
128 .kernel_la = 0x80000000,
129 .kernel_ep = 0x80000000,
130 .rootfs_ofs = 0x140000,
131 }, {
132 .id = "4MLmtk",
133 .fw_max_len = 0x3d0000,
134 .kernel_la = 0x80000000,
135 .kernel_ep = 0x80000000,
136 .rootfs_ofs = 0x140000,
137 }, {
138 .id = "8Mltq",
139 .fw_max_len = 0x7a0000,
140 .kernel_la = 0x80002000,
141 .kernel_ep = 0x80002000,
142 .rootfs_ofs = 0x140000,
143 }, {
144 .id = "16Mltq",
145 .fw_max_len = 0xf90000,
146 .kernel_la = 0x80002000,
147 .kernel_ep = 0x800061b0,
148 .rootfs_ofs = 0x140000,
149 }, {
150 .id = "8Mmtk",
151 .fw_max_len = 0x7a0000,
152 .kernel_la = 0x80000000,
153 .kernel_ep = 0x80000000,
154 .rootfs_ofs = 0x140000,
155 }, {
156 .id = "8MSUmtk", /* Split U-Boot OS */
157 .fw_max_len = 0x770000,
158 .kernel_la = 0x80000000,
159 .kernel_ep = 0x80000000,
160 .rootfs_ofs = 0x140000,
161 }, {
162 .id = "8MLmtk",
163 .fw_max_len = 0x7b0000,
164 .kernel_la = 0x80000000,
165 .kernel_ep = 0x80000000,
166 .rootfs_ofs = 0x140000,
167 }, {
168 .id = "8Mqca",
169 .fw_max_len = 0x7a0000,
170 .kernel_la = 0x80060000,
171 .kernel_ep = 0x80060000,
172 .rootfs_ofs = 0x140000,
173 }, {
174 .id = "16Mqca",
175 .fw_max_len = 0xf90000,
176 .kernel_la = 0x80060000,
177 .kernel_ep = 0x80060000,
178 .rootfs_ofs = 0x140000,
179 }, {
180 /* terminating entry */
181 }
182 };
183
184 static void usage(int status)
185 {
186 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
187
188 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
189 fprintf(stream,
190 "\n"
191 "Options:\n"
192 " -c use combined kernel image\n"
193 " -e swap endianness in kernel load address and entry point\n"
194 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
195 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
196 " -H <hwid> use hardware id specified with <hwid>\n"
197 " -W <hwrev> use hardware revision specified with <hwrev>\n"
198 " -w <hwveradd> use additional hardware version specified with <hwveradd>\n"
199 " -F <id> use flash layout specified with <id>\n"
200 " -k <file> read kernel image from the file <file>\n"
201 " -r <file> read rootfs image from the file <file>\n"
202 " -a <align> align the rootfs start on an <align> bytes boundary\n"
203 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
204 " -o <file> write output to the file <file>\n"
205 " -s strip padding from the end of the image\n"
206 " -j add jffs2 end-of-filesystem markers\n"
207 " -N <vendor> set image vendor to <vendor>\n"
208 " -T <version> set header version to <version>\n"
209 " -V <version> set image version to <version>\n"
210 " -v <version> set firmware version to <version>\n"
211 " -y <version> set secondary version to <version>\n"
212 " -i <file> inspect given firmware file <file>\n"
213 " -x extract kernel and rootfs while inspecting (requires -i)\n"
214 " -h show this screen\n"
215 );
216
217 exit(status);
218 }
219
220 static int check_options(void)
221 {
222 int ret;
223
224 if (inspect_info.file_name) {
225 ret = get_file_stat(&inspect_info);
226 if (ret)
227 return ret;
228
229 return 0;
230 } else if (extract) {
231 ERR("no firmware for inspection specified");
232 return -1;
233 }
234
235 if (opt_hw_id == NULL) {
236 ERR("hardware id must be specified");
237 return -1;
238 }
239
240 board = &custom_board;
241
242 if (layout_id == NULL) {
243 ERR("flash layout is not specified");
244 return -1;
245 }
246
247 board->hw_id = strtoul(opt_hw_id, NULL, 0);
248
249 board->hw_rev = 1;
250 board->hw_ver_add = 0;
251
252 if (opt_hw_rev)
253 board->hw_rev = strtoul(opt_hw_rev, NULL, 0);
254 if (opt_hw_ver_add)
255 board->hw_ver_add = strtoul(opt_hw_ver_add, NULL, 0);
256
257 layout = find_layout(layouts, layout_id);
258 if (layout == NULL) {
259 ERR("unknown flash layout \"%s\"", layout_id);
260 return -1;
261 }
262
263 if (!kernel_la)
264 kernel_la = layout->kernel_la;
265 if (!kernel_ep)
266 kernel_ep = layout->kernel_ep;
267 if (!rootfs_ofs)
268 rootfs_ofs = layout->rootfs_ofs;
269
270 if (kernel_info.file_name == NULL) {
271 ERR("no kernel image specified");
272 return -1;
273 }
274
275 ret = get_file_stat(&kernel_info);
276 if (ret)
277 return ret;
278
279 kernel_len = kernel_info.file_size;
280
281 if (combined) {
282 if (kernel_info.file_size >
283 layout->fw_max_len - sizeof(struct fw_header)) {
284 ERR("kernel image is too big");
285 return -1;
286 }
287 } else {
288 if (rootfs_info.file_name == NULL) {
289 ERR("no rootfs image specified");
290 return -1;
291 }
292
293 ret = get_file_stat(&rootfs_info);
294 if (ret)
295 return ret;
296
297 if (rootfs_align) {
298 kernel_len += sizeof(struct fw_header);
299 rootfs_ofs = ALIGN(kernel_len, rootfs_align);
300 kernel_len -= sizeof(struct fw_header);
301
302 DBG("rootfs offset aligned to 0x%u", rootfs_ofs);
303
304 if (kernel_len + rootfs_info.file_size >
305 layout->fw_max_len - sizeof(struct fw_header)) {
306 ERR("images are too big");
307 return -1;
308 }
309 } else {
310 if (kernel_info.file_size >
311 rootfs_ofs - sizeof(struct fw_header)) {
312 ERR("kernel image is too big");
313 return -1;
314 }
315
316 if (rootfs_info.file_size >
317 (layout->fw_max_len - rootfs_ofs)) {
318 ERR("rootfs image is too big");
319 return -1;
320 }
321 }
322 }
323
324 if (ofname == NULL) {
325 ERR("no output file specified");
326 return -1;
327 }
328
329 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
330 if (ret != 3) {
331 ERR("invalid firmware version '%s'", fw_ver);
332 return -1;
333 }
334
335 ret = sscanf(sver, "%d.%d", &sver_hi, &sver_lo);
336 if (ret != 2) {
337 ERR("invalid secondary version '%s'", sver);
338 return -1;
339 }
340
341 return 0;
342 }
343
344 void fill_header(char *buf, int len)
345 {
346 struct fw_header *hdr = (struct fw_header *)buf;
347 unsigned ver_len;
348
349 memset(hdr, '\xff', sizeof(struct fw_header));
350
351 hdr->version = htonl(bswap_32(hdr_ver));
352 ver_len = strlen(version);
353 if (ver_len > (sizeof(hdr->fw_version) - 1))
354 ver_len = sizeof(hdr->fw_version) - 1;
355
356 memcpy(hdr->fw_version, version, ver_len);
357 hdr->fw_version[ver_len] = 0;
358
359 hdr->hw_id = htonl(board->hw_id);
360 hdr->hw_rev = htonl(board->hw_rev);
361 hdr->hw_ver_add = htonl(board->hw_ver_add);
362
363 if (boot_info.file_size == 0) {
364 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
365 hdr->boot_ofs = htonl(0);
366 hdr->boot_len = htonl(0);
367 } else {
368 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
369 hdr->boot_ofs = htonl(rootfs_ofs + rootfs_info.file_size);
370 hdr->boot_len = htonl(rootfs_info.file_size);
371 }
372
373 hdr->kernel_la = htonl(kernel_la);
374 hdr->kernel_ep = htonl(kernel_ep);
375 hdr->fw_length = htonl(layout->fw_max_len);
376 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
377 hdr->kernel_len = htonl(kernel_len);
378 if (!combined) {
379 hdr->rootfs_ofs = htonl(rootfs_ofs);
380 hdr->rootfs_len = htonl(rootfs_info.file_size);
381 }
382
383 hdr->boot_ofs = htonl(0);
384 hdr->boot_len = htonl(boot_info.file_size);
385
386 hdr->unk2 = htonl(0);
387 hdr->unk3 = htonl(0xffffffff);
388 hdr->unk4 = htons(0x55aa);
389 hdr->unk5 = 0xa5;
390
391 hdr->sver_hi = sver_hi;
392 hdr->sver_lo = sver_lo;
393
394 hdr->ver_hi = fw_ver_hi;
395 hdr->ver_mid = fw_ver_mid;
396 hdr->ver_lo = fw_ver_lo;
397
398 if (board->flags & FLAG_LE_KERNEL_LA_EP) {
399 hdr->kernel_la = bswap_32(hdr->kernel_la);
400 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
401 }
402
403 get_md5(buf, len, hdr->md5sum1);
404 }
405
406 static int inspect_fw(void)
407 {
408 char *buf;
409 struct fw_header *hdr;
410 uint8_t md5sum[MD5SUM_LEN];
411 struct board_info *board;
412 int ret = EXIT_FAILURE;
413
414 buf = malloc(inspect_info.file_size);
415 if (!buf) {
416 ERR("no memory for buffer!\n");
417 goto out;
418 }
419
420 ret = read_to_buf(&inspect_info, buf);
421 if (ret)
422 goto out_free_buf;
423 hdr = (struct fw_header *)buf;
424
425 board = &custom_board;
426
427 if (board->flags & FLAG_LE_KERNEL_LA_EP) {
428 hdr->kernel_la = bswap_32(hdr->kernel_la);
429 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
430 }
431
432 inspect_fw_pstr("File name", inspect_info.file_name);
433 inspect_fw_phexdec("File size", inspect_info.file_size);
434
435 switch(bswap_32(ntohl(hdr->version))) {
436 case 2:
437 case 3:
438 break;
439 default:
440 ERR("file does not seem to have V2/V3 header!\n");
441 goto out_free_buf;
442 }
443
444 inspect_fw_phexdec("Version 2 Header size", sizeof(struct fw_header));
445
446 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
447 if (ntohl(hdr->boot_len) == 0)
448 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
449 else
450 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
451 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
452
453 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
454 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
455 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
456 } else {
457 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
458 }
459 if (ntohl(hdr->unk2) != 0)
460 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
461 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
462 "(purpose yet unknown, unchecked here)");
463
464 if (ntohl(hdr->unk3) != 0xffffffff)
465 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
466
467 if (ntohs(hdr->unk4) != 0x55aa)
468 inspect_fw_phexdec("Unknown value 4", hdr->unk4);
469
470 if (hdr->unk5 != 0xa5)
471 inspect_fw_phexdec("Unknown value 5", hdr->unk5);
472
473 printf("\n");
474
475 inspect_fw_pstr("Firmware version", hdr->fw_version);
476 inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id));
477 inspect_fw_phex("Hardware Revision",
478 ntohl(hdr->hw_rev));
479 inspect_fw_phex("Additional HW Version",
480 ntohl(hdr->hw_ver_add));
481
482 printf("%-23s: %d.%d.%d-%d.%d\n", "Software version",
483 hdr->ver_hi, hdr->ver_mid, hdr->ver_lo,
484 hdr->sver_hi, hdr->sver_lo);
485
486 printf("\n");
487
488 inspect_fw_phexdec("Kernel data offset",
489 ntohl(hdr->kernel_ofs));
490 inspect_fw_phexdec("Kernel data length",
491 ntohl(hdr->kernel_len));
492 inspect_fw_phex("Kernel load address",
493 ntohl(hdr->kernel_la));
494 inspect_fw_phex("Kernel entry point",
495 ntohl(hdr->kernel_ep));
496 inspect_fw_phexdec("Rootfs data offset",
497 ntohl(hdr->rootfs_ofs));
498 inspect_fw_phexdec("Rootfs data length",
499 ntohl(hdr->rootfs_len));
500 inspect_fw_phexdec("Boot loader data offset",
501 ntohl(hdr->boot_ofs));
502 inspect_fw_phexdec("Boot loader data length",
503 ntohl(hdr->boot_len));
504 inspect_fw_phexdec("Total firmware length",
505 ntohl(hdr->fw_length));
506
507 if (extract) {
508 FILE *fp;
509 char *filename;
510
511 printf("\n");
512
513 filename = malloc(strlen(inspect_info.file_name) + 8);
514 sprintf(filename, "%s-kernel", inspect_info.file_name);
515 printf("Extracting kernel to \"%s\"...\n", filename);
516 fp = fopen(filename, "w");
517 if (fp) {
518 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
519 ntohl(hdr->kernel_len), 1, fp)) {
520 ERR("error in fwrite(): %s", strerror(errno));
521 }
522 fclose(fp);
523 } else {
524 ERR("error in fopen(): %s", strerror(errno));
525 }
526 free(filename);
527
528 filename = malloc(strlen(inspect_info.file_name) + 8);
529 sprintf(filename, "%s-rootfs", inspect_info.file_name);
530 printf("Extracting rootfs to \"%s\"...\n", filename);
531 fp = fopen(filename, "w");
532 if (fp) {
533 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
534 ntohl(hdr->rootfs_len), 1, fp)) {
535 ERR("error in fwrite(): %s", strerror(errno));
536 }
537 fclose(fp);
538 } else {
539 ERR("error in fopen(): %s", strerror(errno));
540 }
541 free(filename);
542 }
543
544 out_free_buf:
545 free(buf);
546 out:
547 return ret;
548 }
549
550 int main(int argc, char *argv[])
551 {
552 int ret = EXIT_FAILURE;
553
554 progname = basename(argv[0]);
555
556 while ( 1 ) {
557 int c;
558
559 c = getopt(argc, argv, "a:H:E:F:L:V:N:W:w:ci:k:r:R:o:xhsjv:y:T:e");
560 if (c == -1)
561 break;
562
563 switch (c) {
564 case 'a':
565 sscanf(optarg, "0x%x", &rootfs_align);
566 break;
567 case 'H':
568 opt_hw_id = optarg;
569 break;
570 case 'E':
571 sscanf(optarg, "0x%x", &kernel_ep);
572 break;
573 case 'F':
574 layout_id = optarg;
575 break;
576 case 'W':
577 opt_hw_rev = optarg;
578 break;
579 case 'w':
580 opt_hw_ver_add = optarg;
581 break;
582 case 'L':
583 sscanf(optarg, "0x%x", &kernel_la);
584 break;
585 case 'V':
586 version = optarg;
587 break;
588 case 'v':
589 fw_ver = optarg;
590 break;
591 case 'y':
592 sver = optarg;
593 break;
594 case 'N':
595 vendor = optarg;
596 break;
597 case 'c':
598 combined++;
599 break;
600 case 'k':
601 kernel_info.file_name = optarg;
602 break;
603 case 'r':
604 rootfs_info.file_name = optarg;
605 break;
606 case 'R':
607 sscanf(optarg, "0x%x", &rootfs_ofs);
608 break;
609 case 'o':
610 ofname = optarg;
611 break;
612 case 's':
613 strip_padding = 1;
614 break;
615 case 'i':
616 inspect_info.file_name = optarg;
617 break;
618 case 'j':
619 add_jffs2_eof = 1;
620 break;
621 case 'x':
622 extract = 1;
623 break;
624 case 'T':
625 hdr_ver = atoi(optarg);
626 break;
627 case 'e':
628 custom_board.flags = FLAG_LE_KERNEL_LA_EP;
629 break;
630 case 'h':
631 usage(EXIT_SUCCESS);
632 break;
633 default:
634 usage(EXIT_FAILURE);
635 break;
636 }
637 }
638
639 ret = check_options();
640 if (ret)
641 goto out;
642
643 if (!inspect_info.file_name)
644 ret = build_fw(sizeof(struct fw_header));
645 else
646 ret = inspect_fw();
647
648 out:
649 return ret;
650 }
651