2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
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>
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.
18 #include <unistd.h> /* for unlink() */
20 #include <getopt.h> /* for getopt() */
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
30 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
32 #define HEADER_VERSION_V1 0x01000000
33 #define HEADER_VERSION_V2 0x02000000
34 #define HWID_TL_MR10U_V1 0x00100101
35 #define HWID_TL_MR3020_V1 0x30200001
36 #define HWID_TL_MR3220_V1 0x32200001
37 #define HWID_TL_MR3220_V2 0x32200002
38 #define HWID_TL_MR3420_V1 0x34200001
39 #define HWID_TL_MR3420_V2 0x34200002
40 #define HWID_TL_WA701N_V1 0x07010001
41 #define HWID_TL_WA7510N_V1 0x75100001
42 #define HWID_TL_WA801ND_V1 0x08010001
43 #define HWID_TL_WA830RE_V1 0x08300010
44 #define HWID_TL_WA830RE_V2 0x08300002
45 #define HWID_TL_WA901ND_V1 0x09010001
46 #define HWID_TL_WA901ND_V2 0x09010002
47 #define HWID_TL_WDR4900_V1 0x49000001
48 #define HWID_TL_WR703N_V1 0x07030101
49 #define HWID_TL_WR720N_V3 0x07200103
50 #define HWID_TL_WR741ND_V1 0x07410001
51 #define HWID_TL_WR741ND_V4 0x07410004
52 #define HWID_TL_WR740N_V1 0x07400001
53 #define HWID_TL_WR740N_V3 0x07400003
54 #define HWID_TL_WR743ND_V1 0x07430001
55 #define HWID_TL_WR743ND_V2 0x07430002
56 #define HWID_TL_WR841N_V1_5 0x08410002
57 #define HWID_TL_WR841ND_V3 0x08410003
58 #define HWID_TL_WR841ND_V5 0x08410005
59 #define HWID_TL_WR841ND_V7 0x08410007
60 #define HWID_TL_WR941ND_V2 0x09410002
61 #define HWID_TL_WR941ND_V4 0x09410004
62 #define HWID_TL_WR1043ND_V1 0x10430001
63 #define HWID_TL_WR1041N_V2 0x10410002
64 #define HWID_TL_WR2543N_V1 0x25430001
69 char *file_name
; /* name of the file */
70 uint32_t file_size
; /* length of the file */
74 uint32_t version
; /* header version */
77 uint32_t hw_id
; /* hardware id */
78 uint32_t hw_rev
; /* hardware revision */
80 uint8_t md5sum1
[MD5SUM_LEN
];
82 uint8_t md5sum2
[MD5SUM_LEN
];
84 uint32_t kernel_la
; /* kernel load address */
85 uint32_t kernel_ep
; /* kernel entry point */
86 uint32_t fw_length
; /* total length of the firmware */
87 uint32_t kernel_ofs
; /* kernel data offset */
88 uint32_t kernel_len
; /* kernel data length */
89 uint32_t rootfs_ofs
; /* rootfs data offset */
90 uint32_t rootfs_len
; /* rootfs data length */
91 uint32_t boot_ofs
; /* bootloader data offset */
92 uint32_t boot_len
; /* bootloader data length */
97 } __attribute__ ((packed
));
100 uint32_t version
; /* header version */
102 uint32_t hw_id
; /* hardware id */
103 uint32_t hw_rev
; /* hardware revision */
105 uint8_t md5sum1
[MD5SUM_LEN
];
107 uint8_t md5sum2
[MD5SUM_LEN
];
109 uint32_t kernel_la
; /* kernel load address */
110 uint32_t kernel_ep
; /* kernel entry point */
111 uint32_t fw_length
; /* total length of the firmware */
112 uint32_t kernel_ofs
; /* kernel data offset */
113 uint32_t kernel_len
; /* kernel data length */
114 uint32_t rootfs_ofs
; /* rootfs data offset */
115 uint32_t rootfs_len
; /* rootfs data length */
116 uint32_t boot_ofs
; /* bootloader data offset */
117 uint32_t boot_len
; /* bootloader data length */
122 } __attribute__ ((packed
));
124 struct flash_layout
{
144 static char *progname
;
145 static char *vendor
= "TP-LINK Technologies";
146 static char *version
= "ver. 1.0";
147 static char *fw_ver
= "0.0.0";
149 static char *board_id
;
150 static struct board_info
*board
;
151 static char *layout_id
;
152 static struct flash_layout
*layout
;
153 static char *opt_hw_id
;
154 static uint32_t hw_id
;
155 static char *opt_hw_rev
;
156 static uint32_t hw_rev
;
157 static int fw_ver_lo
;
158 static int fw_ver_mid
;
159 static int fw_ver_hi
;
160 static struct file_info kernel_info
;
161 static uint32_t kernel_la
= 0;
162 static uint32_t kernel_ep
= 0;
163 static uint32_t kernel_len
= 0;
164 static struct file_info rootfs_info
;
165 static uint32_t rootfs_ofs
= 0;
166 static uint32_t rootfs_align
;
167 static struct file_info boot_info
;
169 static int strip_padding
;
170 static int add_jffs2_eof
;
171 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
173 static struct file_info inspect_info
;
174 static int extract
= 0;
176 char md5salt_normal
[MD5SUM_LEN
] = {
177 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
178 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
181 char md5salt_boot
[MD5SUM_LEN
] = {
182 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
183 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
186 static struct flash_layout layouts
[] = {
189 .fw_max_len
= 0x3c0000,
190 .kernel_la
= 0x80060000,
191 .kernel_ep
= 0x80060000,
192 .rootfs_ofs
= 0x140000,
195 .fw_max_len
= 0x3c0000,
196 .kernel_la
= 0x80060000,
197 .kernel_ep
= 0x80060000,
198 .rootfs_ofs
= 0x100000,
201 .fw_max_len
= 0x7c0000,
202 .kernel_la
= 0x80060000,
203 .kernel_ep
= 0x80060000,
204 .rootfs_ofs
= 0x140000,
207 .fw_max_len
= 0x7c0000,
208 .kernel_la
= 0x80060000,
209 .kernel_ep
= 0x80060000,
210 .rootfs_ofs
= 0x100000,
213 .fw_max_len
= 0xf80000,
214 .kernel_la
= 0x00000000,
215 .kernel_ep
= 0xc0000000,
216 .rootfs_ofs
= 0x2a0000,
218 /* terminating entry */
222 static struct board_info boards
[] = {
225 .hw_id
= HWID_TL_MR10U_V1
,
227 .layout_id
= "4Mlzma",
230 .hw_id
= HWID_TL_MR3020_V1
,
232 .layout_id
= "4Mlzma",
235 .hw_id
= HWID_TL_MR3220_V1
,
240 .hw_id
= HWID_TL_MR3220_V2
,
242 .layout_id
= "4Mlzma",
245 .hw_id
= HWID_TL_MR3420_V1
,
250 .hw_id
= HWID_TL_MR3420_V2
,
252 .layout_id
= "4Mlzma",
255 .hw_id
= HWID_TL_WA701N_V1
,
260 .hw_id
= HWID_TL_WA7510N_V1
,
264 .id
= "TL-WA801NDv1",
265 .hw_id
= HWID_TL_WA801ND_V1
,
269 .id
= "TL-WA830REv1",
270 .hw_id
= HWID_TL_WA830RE_V1
,
274 .id
= "TL-WA830REv2",
275 .hw_id
= HWID_TL_WA830RE_V2
,
279 .id
= "TL-WA901NDv1",
280 .hw_id
= HWID_TL_WA901ND_V1
,
284 .id
= "TL-WA901NDv2",
285 .hw_id
= HWID_TL_WA901ND_V2
,
289 .id
= "TL-WDR4900v1",
290 .hw_id
= HWID_TL_WDR4900_V1
,
292 .layout_id
= "16Mppc",
294 .id
= "TL-WR741NDv1",
295 .hw_id
= HWID_TL_WR741ND_V1
,
299 .id
= "TL-WR741NDv4",
300 .hw_id
= HWID_TL_WR741ND_V4
,
302 .layout_id
= "4Mlzma",
305 .hw_id
= HWID_TL_WR740N_V1
,
310 .hw_id
= HWID_TL_WR740N_V3
,
314 .id
= "TL-WR743NDv1",
315 .hw_id
= HWID_TL_WR743ND_V1
,
319 .id
= "TL-WR743NDv2",
320 .hw_id
= HWID_TL_WR743ND_V2
,
322 .layout_id
= "4Mlzma",
324 .id
= "TL-WR841Nv1.5",
325 .hw_id
= HWID_TL_WR841N_V1_5
,
329 .id
= "TL-WR841NDv3",
330 .hw_id
= HWID_TL_WR841ND_V3
,
334 .id
= "TL-WR841NDv5",
335 .hw_id
= HWID_TL_WR841ND_V5
,
339 .id
= "TL-WR841NDv7",
340 .hw_id
= HWID_TL_WR841ND_V7
,
344 .id
= "TL-WR941NDv2",
345 .hw_id
= HWID_TL_WR941ND_V2
,
349 .id
= "TL-WR941NDv4",
350 .hw_id
= HWID_TL_WR941ND_V4
,
354 .id
= "TL-WR1041Nv2",
355 .hw_id
= HWID_TL_WR1041N_V2
,
357 .layout_id
= "4Mlzma",
359 .id
= "TL-WR1043NDv1",
360 .hw_id
= HWID_TL_WR1043ND_V1
,
364 .id
= "TL-WR2543Nv1",
365 .hw_id
= HWID_TL_WR2543N_V1
,
367 .layout_id
= "8Mlzma",
370 .hw_id
= HWID_TL_WR703N_V1
,
372 .layout_id
= "4Mlzma",
375 .hw_id
= HWID_TL_WR720N_V3
,
377 .layout_id
= "4Mlzma",
379 /* terminating entry */
386 #define ERR(fmt, ...) do { \
388 fprintf(stderr, "[%s] *** error: " fmt "\n", \
389 progname, ## __VA_ARGS__ ); \
392 #define ERRS(fmt, ...) do { \
395 fprintf(stderr, "[%s] *** error: " fmt "\n", \
396 progname, ## __VA_ARGS__, strerror(save)); \
399 #define DBG(fmt, ...) do { \
400 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
403 static struct board_info
*find_board(char *id
)
405 struct board_info
*ret
;
406 struct board_info
*board
;
409 for (board
= boards
; board
->id
!= NULL
; board
++){
410 if (strcasecmp(id
, board
->id
) == 0) {
419 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
421 struct board_info
*board
;
423 for (board
= boards
; board
->id
!= NULL
; board
++) {
424 if (hw_id
== board
->hw_id
)
431 static struct flash_layout
*find_layout(char *id
)
433 struct flash_layout
*ret
;
434 struct flash_layout
*l
;
437 for (l
= layouts
; l
->id
!= NULL
; l
++){
438 if (strcasecmp(id
, l
->id
) == 0) {
447 static void usage(int status
)
449 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
450 struct board_info
*board
;
452 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
456 " -B <board> create image for the board specified with <board>\n"
457 " -c use combined kernel image\n"
458 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
459 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
460 " -H <hwid> use hardware id specified with <hwid>\n"
461 " -W <hwrev> use hardware revision specified with <hwrev>\n"
462 " -F <id> use flash layout specified with <id>\n"
463 " -k <file> read kernel image from the file <file>\n"
464 " -r <file> read rootfs image from the file <file>\n"
465 " -a <align> align the rootfs start on an <align> bytes boundary\n"
466 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
467 " -o <file> write output to the file <file>\n"
468 " -s strip padding from the end of the image\n"
469 " -j add jffs2 end-of-filesystem markers\n"
470 " -N <vendor> set image vendor to <vendor>\n"
471 " -V <version> set image version to <version>\n"
472 " -v <version> set firmware version to <version>\n"
473 " -i <file> inspect given firmware file <file>\n"
474 " -x extract kernel and rootfs while inspecting (requires -i)\n"
475 " -h show this screen\n"
481 static int get_md5(char *data
, int size
, char *md5
)
486 MD5_Update(&ctx
, data
, size
);
487 MD5_Final(md5
, &ctx
);
490 static int get_file_stat(struct file_info
*fdata
)
495 if (fdata
->file_name
== NULL
)
498 res
= stat(fdata
->file_name
, &st
);
500 ERRS("stat failed on %s", fdata
->file_name
);
504 fdata
->file_size
= st
.st_size
;
508 static int read_to_buf(struct file_info
*fdata
, char *buf
)
511 int ret
= EXIT_FAILURE
;
513 f
= fopen(fdata
->file_name
, "r");
515 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
520 fread(buf
, fdata
->file_size
, 1, f
);
522 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
534 static int check_options(void)
538 if (inspect_info
.file_name
) {
539 ret
= get_file_stat(&inspect_info
);
544 } else if (extract
) {
545 ERR("no firmware for inspection specified");
549 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
550 ERR("either board or hardware id must be specified");
555 board
= find_board(board_id
);
557 ERR("unknown/unsupported board id \"%s\"", board_id
);
560 if (layout_id
== NULL
)
561 layout_id
= board
->layout_id
;
563 hw_id
= board
->hw_id
;
564 hw_rev
= board
->hw_rev
;
566 if (layout_id
== NULL
) {
567 ERR("flash layout is not specified");
570 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
573 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
578 layout
= find_layout(layout_id
);
579 if (layout
== NULL
) {
580 ERR("unknown flash layout \"%s\"", layout_id
);
585 kernel_la
= layout
->kernel_la
;
587 kernel_ep
= layout
->kernel_ep
;
589 rootfs_ofs
= layout
->rootfs_ofs
;
591 if (kernel_info
.file_name
== NULL
) {
592 ERR("no kernel image specified");
596 ret
= get_file_stat(&kernel_info
);
600 kernel_len
= kernel_info
.file_size
;
603 if (kernel_info
.file_size
>
604 layout
->fw_max_len
- sizeof(struct fw_header
)) {
605 ERR("kernel image is too big");
609 if (rootfs_info
.file_name
== NULL
) {
610 ERR("no rootfs image specified");
614 ret
= get_file_stat(&rootfs_info
);
619 kernel_len
+= sizeof(struct fw_header
);
620 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
621 kernel_len
-= sizeof(struct fw_header
);
623 DBG("kernel length aligned to %u", kernel_len
);
625 if (kernel_len
+ rootfs_info
.file_size
>
626 layout
->fw_max_len
- sizeof(struct fw_header
)) {
627 ERR("images are too big");
631 if (kernel_info
.file_size
>
632 rootfs_ofs
- sizeof(struct fw_header
)) {
633 ERR("kernel image is too big");
637 if (rootfs_info
.file_size
>
638 (layout
->fw_max_len
- rootfs_ofs
)) {
639 ERR("rootfs image is too big");
645 if (ofname
== NULL
) {
646 ERR("no output file specified");
650 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
652 ERR("invalid firmware version '%s'", fw_ver
);
659 static void fill_header(char *buf
, int len
)
661 struct fw_header
*hdr
= (struct fw_header
*)buf
;
663 memset(hdr
, 0, sizeof(struct fw_header
));
665 hdr
->version
= htonl(HEADER_VERSION_V1
);
666 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
667 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
668 hdr
->hw_id
= htonl(hw_id
);
669 hdr
->hw_rev
= htonl(hw_rev
);
671 if (boot_info
.file_size
== 0)
672 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
674 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
676 hdr
->kernel_la
= htonl(kernel_la
);
677 hdr
->kernel_ep
= htonl(kernel_ep
);
678 hdr
->fw_length
= htonl(layout
->fw_max_len
);
679 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
680 hdr
->kernel_len
= htonl(kernel_len
);
682 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
683 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
686 hdr
->ver_hi
= htons(fw_ver_hi
);
687 hdr
->ver_mid
= htons(fw_ver_mid
);
688 hdr
->ver_lo
= htons(fw_ver_lo
);
690 get_md5(buf
, len
, hdr
->md5sum1
);
693 static void fill_header_v2(char *buf
, int len
)
695 struct fw_header_v2
*hdr
= (struct fw_header_v2
*)buf
;
697 memset(hdr
, 0, sizeof(struct fw_header_v2
));
699 hdr
->version
= htonl(HEADER_VERSION_V2
);
700 memset(hdr
->fw_version
, 0xff, sizeof(hdr
->fw_version
));
701 strncpy(hdr
->fw_version
, version
, strlen(version
));
702 hdr
->hw_id
= htonl(hw_id
);
703 hdr
->hw_rev
= htonl(hw_rev
);
705 if (boot_info
.file_size
== 0)
706 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
708 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
710 hdr
->kernel_la
= htonl(kernel_la
);
711 hdr
->kernel_ep
= htonl(kernel_ep
);
712 hdr
->fw_length
= htonl(layout
->fw_max_len
);
713 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header_v2
));
714 hdr
->kernel_len
= htonl(kernel_len
);
716 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
717 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
720 hdr
->ver_hi
= htons(fw_ver_hi
);
721 hdr
->ver_mid
= htons(fw_ver_mid
);
722 hdr
->ver_lo
= htons(fw_ver_lo
);
724 get_md5(buf
, len
, hdr
->md5sum1
);
727 static int pad_jffs2(char *buf
, int currlen
)
733 pad_mask
= (64 * 1024);
734 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
738 for (i
= 10; i
< 32; i
++) {
744 len
= ALIGN(len
, mask
);
746 for (i
= 10; i
< 32; i
++) {
748 if ((len
& (mask
- 1)) == 0)
752 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
753 buf
[len
+ i
] = jffs2_eof_mark
[i
];
755 len
+= sizeof(jffs2_eof_mark
);
761 static int write_fw(char *data
, int len
)
764 int ret
= EXIT_FAILURE
;
766 f
= fopen(ofname
, "w");
768 ERRS("could not open \"%s\" for writing", ofname
);
773 fwrite(data
, len
, 1, f
);
775 ERRS("unable to write output file");
779 DBG("firmware file \"%s\" completed", ofname
);
786 if (ret
!= EXIT_SUCCESS
) {
793 static int build_fw(void)
798 int ret
= EXIT_FAILURE
;
801 if (board
->hdr_version
== HEADER_VERSION_V2
)
802 hdr_len
= sizeof(struct fw_header_v2
);
804 hdr_len
= sizeof(struct fw_header
);
806 buflen
= layout
->fw_max_len
;
808 buf
= malloc(buflen
);
810 ERR("no memory for buffer\n");
814 memset(buf
, 0xff, buflen
);
816 ret
= read_to_buf(&kernel_info
, p
);
820 writelen
= hdr_len
+ kernel_len
;
826 p
= buf
+ rootfs_ofs
;
828 ret
= read_to_buf(&rootfs_info
, p
);
833 writelen
+= rootfs_info
.file_size
;
835 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
838 writelen
= pad_jffs2(buf
, writelen
);
844 if (board
->hdr_version
== HEADER_VERSION_V2
)
845 fill_header_v2(buf
, writelen
);
847 fill_header(buf
, writelen
);
848 ret
= write_fw(buf
, writelen
);
860 /* Helper functions to inspect_fw() representing different output formats */
861 static inline void inspect_fw_pstr(char *label
, char *str
)
863 printf("%-23s: %s\n", label
, str
);
866 static inline void inspect_fw_phex(char *label
, uint32_t val
)
868 printf("%-23s: 0x%08x\n", label
, val
);
871 static inline void inspect_fw_phexpost(char *label
,
872 uint32_t val
, char *post
)
874 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
877 static inline void inspect_fw_phexdef(char *label
,
878 uint32_t val
, uint32_t defval
)
880 printf("%-23s: 0x%08x ", label
, val
);
883 printf("(== OpenWrt default)\n");
885 printf("(OpenWrt default: 0x%08x)\n", defval
);
888 static inline void inspect_fw_phexexp(char *label
,
889 uint32_t val
, uint32_t expval
)
891 printf("%-23s: 0x%08x ", label
, val
);
896 printf("(expected: 0x%08x)\n", expval
);
899 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
901 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
904 static inline void inspect_fw_phexdecdef(char *label
,
905 uint32_t val
, uint32_t defval
)
907 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
910 printf("(== OpenWrt default)\n");
912 printf("(OpenWrt default: 0x%08x)\n", defval
);
915 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
919 printf("%-23s:", label
);
920 for (i
=0; i
<MD5SUM_LEN
; i
++)
921 printf(" %02x", val
[i
]);
922 printf(" %s\n", text
);
925 static int inspect_fw(void)
928 struct fw_header
*hdr
;
929 uint8_t md5sum
[MD5SUM_LEN
];
930 struct board_info
*board
;
931 int ret
= EXIT_FAILURE
;
933 buf
= malloc(inspect_info
.file_size
);
935 ERR("no memory for buffer!\n");
939 ret
= read_to_buf(&inspect_info
, buf
);
942 hdr
= (struct fw_header
*)buf
;
944 inspect_fw_pstr("File name", inspect_info
.file_name
);
945 inspect_fw_phexdec("File size", inspect_info
.file_size
);
947 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
948 ERR("file does not seem to have V1 header!\n");
952 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
954 if (ntohl(hdr
->unk1
) != 0)
955 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
957 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
958 if (ntohl(hdr
->boot_len
) == 0)
959 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
961 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
962 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
964 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
965 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
966 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
968 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
970 if (ntohl(hdr
->unk2
) != 0)
971 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
972 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
973 "(purpose yet unknown, unchecked here)");
974 if (ntohl(hdr
->unk3
) != 0)
975 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
979 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
980 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
981 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
983 layout
= find_layout(board
->layout_id
);
984 inspect_fw_phexpost("Hardware ID",
985 ntohl(hdr
->hw_id
), board
->id
);
986 inspect_fw_phexexp("Hardware Revision",
987 ntohl(hdr
->hw_rev
), board
->hw_rev
);
989 inspect_fw_phexpost("Hardware ID",
990 ntohl(hdr
->hw_id
), "unknown");
991 inspect_fw_phex("Hardware Revision",
997 inspect_fw_phexdec("Kernel data offset",
998 ntohl(hdr
->kernel_ofs
));
999 inspect_fw_phexdec("Kernel data length",
1000 ntohl(hdr
->kernel_len
));
1002 inspect_fw_phexdef("Kernel load address",
1003 ntohl(hdr
->kernel_la
),
1004 layout
? layout
->kernel_la
: 0xffffffff);
1005 inspect_fw_phexdef("Kernel entry point",
1006 ntohl(hdr
->kernel_ep
),
1007 layout
? layout
->kernel_ep
: 0xffffffff);
1008 inspect_fw_phexdecdef("Rootfs data offset",
1009 ntohl(hdr
->rootfs_ofs
),
1010 layout
? layout
->rootfs_ofs
: 0xffffffff);
1012 inspect_fw_phex("Kernel load address",
1013 ntohl(hdr
->kernel_la
));
1014 inspect_fw_phex("Kernel entry point",
1015 ntohl(hdr
->kernel_ep
));
1016 inspect_fw_phexdec("Rootfs data offset",
1017 ntohl(hdr
->rootfs_ofs
));
1019 inspect_fw_phexdec("Rootfs data length",
1020 ntohl(hdr
->rootfs_len
));
1021 inspect_fw_phexdec("Boot loader data offset",
1022 ntohl(hdr
->boot_ofs
));
1023 inspect_fw_phexdec("Boot loader data length",
1024 ntohl(hdr
->boot_len
));
1025 inspect_fw_phexdec("Total firmware length",
1026 ntohl(hdr
->fw_length
));
1034 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
1035 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
1036 printf("Extracting kernel to \"%s\"...\n", filename
);
1037 fp
= fopen(filename
, "w");
1039 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
1040 ntohl(hdr
->kernel_len
), 1, fp
)) {
1041 ERR("error in fwrite(): %s", strerror(errno
));
1045 ERR("error in fopen(): %s", strerror(errno
));
1049 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
1050 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
1051 printf("Extracting rootfs to \"%s\"...\n", filename
);
1052 fp
= fopen(filename
, "w");
1054 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
1055 ntohl(hdr
->rootfs_len
), 1, fp
)) {
1056 ERR("error in fwrite(): %s", strerror(errno
));
1060 ERR("error in fopen(): %s", strerror(errno
));
1071 int main(int argc
, char *argv
[])
1073 int ret
= EXIT_FAILURE
;
1078 progname
= basename(argv
[0]);
1083 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
1089 sscanf(optarg
, "0x%x", &rootfs_align
);
1098 sscanf(optarg
, "0x%x", &kernel_ep
);
1104 opt_hw_rev
= optarg
;
1107 sscanf(optarg
, "0x%x", &kernel_la
);
1122 kernel_info
.file_name
= optarg
;
1125 rootfs_info
.file_name
= optarg
;
1128 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1137 inspect_info
.file_name
= optarg
;
1146 usage(EXIT_SUCCESS
);
1149 usage(EXIT_FAILURE
);
1154 ret
= check_options();
1158 if (!inspect_info
.file_name
)