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 HWID_ANTMINER_S1 0x04440001
34 #define HWID_ANTMINER_S3 0x04440003
35 #define HWID_GL_INET_V1 0x08000001
36 #define HWID_GS_OOLITE_V1 0x3C000101
37 #define HWID_TL_MR10U_V1 0x00100101
38 #define HWID_TL_MR13U_V1 0x00130101
39 #define HWID_TL_MR3020_V1 0x30200001
40 #define HWID_TL_MR3220_V1 0x32200001
41 #define HWID_TL_MR3220_V2 0x32200002
42 #define HWID_TL_MR3420_V1 0x34200001
43 #define HWID_TL_MR3420_V2 0x34200002
44 #define HWID_TL_WA701N_V1 0x07010001
45 #define HWID_TL_WA701N_V2 0x07010002
46 #define HWID_TL_WA7210N_V2 0x72100002
47 #define HWID_TL_WA7510N_V1 0x75100001
48 #define HWID_TL_WA801ND_V1 0x08010001
49 #define HWID_TL_WA830RE_V1 0x08300010
50 #define HWID_TL_WA830RE_V2 0x08300002
51 #define HWID_TL_WA801ND_V2 0x08010002
52 #define HWID_TL_WA901ND_V1 0x09010001
53 #define HWID_TL_WA901ND_V2 0x09010002
54 #define HWID_TL_WDR4300_V1_IL 0x43008001
55 #define HWID_TL_WDR4900_V1 0x49000001
56 #define HWID_TL_WR703N_V1 0x07030101
57 #define HWID_TL_WR720N_V3 0x07200103
58 #define HWID_TL_WR720N_V4 0x07200104
59 #define HWID_TL_WR741ND_V1 0x07410001
60 #define HWID_TL_WR741ND_V4 0x07410004
61 #define HWID_TL_WR740N_V1 0x07400001
62 #define HWID_TL_WR740N_V3 0x07400003
63 #define HWID_TL_WR743ND_V1 0x07430001
64 #define HWID_TL_WR743ND_V2 0x07430002
65 #define HWID_TL_WR841N_V1_5 0x08410002
66 #define HWID_TL_WR841ND_V3 0x08410003
67 #define HWID_TL_WR841ND_V5 0x08410005
68 #define HWID_TL_WR841ND_V7 0x08410007
69 #define HWID_TL_WR941ND_V2 0x09410002
70 #define HWID_TL_WR941ND_V4 0x09410004
71 #define HWID_TL_WR1043ND_V1 0x10430001
72 #define HWID_TL_WR1043ND_V2 0x10430002
73 #define HWID_TL_WR1041N_V2 0x10410002
74 #define HWID_TL_WR2543N_V1 0x25430001
79 char *file_name
; /* name of the file */
80 uint32_t file_size
; /* length of the file */
84 uint32_t version
; /* header version */
87 uint32_t hw_id
; /* hardware id */
88 uint32_t hw_rev
; /* hardware revision */
90 uint8_t md5sum1
[MD5SUM_LEN
];
92 uint8_t md5sum2
[MD5SUM_LEN
];
94 uint32_t kernel_la
; /* kernel load address */
95 uint32_t kernel_ep
; /* kernel entry point */
96 uint32_t fw_length
; /* total length of the firmware */
97 uint32_t kernel_ofs
; /* kernel data offset */
98 uint32_t kernel_len
; /* kernel data length */
99 uint32_t rootfs_ofs
; /* rootfs data offset */
100 uint32_t rootfs_len
; /* rootfs data length */
101 uint32_t boot_ofs
; /* bootloader data offset */
102 uint32_t boot_len
; /* bootloader data length */
107 } __attribute__ ((packed
));
109 struct flash_layout
{
128 static char *progname
;
129 static char *vendor
= "TP-LINK Technologies";
130 static char *version
= "ver. 1.0";
131 static char *fw_ver
= "0.0.0";
133 static char *board_id
;
134 static struct board_info
*board
;
135 static char *layout_id
;
136 static struct flash_layout
*layout
;
137 static char *opt_hw_id
;
138 static uint32_t hw_id
;
139 static char *opt_hw_rev
;
140 static uint32_t hw_rev
;
141 static int fw_ver_lo
;
142 static int fw_ver_mid
;
143 static int fw_ver_hi
;
144 static struct file_info kernel_info
;
145 static uint32_t kernel_la
= 0;
146 static uint32_t kernel_ep
= 0;
147 static uint32_t kernel_len
= 0;
148 static struct file_info rootfs_info
;
149 static uint32_t rootfs_ofs
= 0;
150 static uint32_t rootfs_align
;
151 static struct file_info boot_info
;
153 static int strip_padding
;
154 static int ignore_size
;
155 static int add_jffs2_eof
;
156 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
157 static uint32_t fw_max_len
;
158 static uint32_t reserved_space
;
160 static struct file_info inspect_info
;
161 static int extract
= 0;
163 char md5salt_normal
[MD5SUM_LEN
] = {
164 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
165 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
168 char md5salt_boot
[MD5SUM_LEN
] = {
169 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
170 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
173 static struct flash_layout layouts
[] = {
176 .fw_max_len
= 0x3c0000,
177 .kernel_la
= 0x80060000,
178 .kernel_ep
= 0x80060000,
179 .rootfs_ofs
= 0x140000,
182 .fw_max_len
= 0x3c0000,
183 .kernel_la
= 0x80060000,
184 .kernel_ep
= 0x80060000,
185 .rootfs_ofs
= 0x100000,
188 .fw_max_len
= 0x7c0000,
189 .kernel_la
= 0x80060000,
190 .kernel_ep
= 0x80060000,
191 .rootfs_ofs
= 0x140000,
194 .fw_max_len
= 0x7c0000,
195 .kernel_la
= 0x80060000,
196 .kernel_ep
= 0x80060000,
197 .rootfs_ofs
= 0x100000,
200 .fw_max_len
= 0xf80000,
201 .kernel_la
= 0x80060000,
202 .kernel_ep
= 0x80060000,
203 .rootfs_ofs
= 0x140000,
206 .fw_max_len
= 0xf80000,
207 .kernel_la
= 0x80060000,
208 .kernel_ep
= 0x80060000,
209 .rootfs_ofs
= 0x100000,
212 .fw_max_len
= 0xf80000,
213 .kernel_la
= 0x00000000,
214 .kernel_ep
= 0xc0000000,
215 .rootfs_ofs
= 0x2a0000,
217 /* terminating entry */
221 static struct board_info boards
[] = {
224 .hw_id
= HWID_TL_MR10U_V1
,
226 .layout_id
= "4Mlzma",
229 .hw_id
= HWID_TL_MR13U_V1
,
231 .layout_id
= "4Mlzma",
234 .hw_id
= HWID_TL_MR3020_V1
,
236 .layout_id
= "4Mlzma",
239 .hw_id
= HWID_TL_MR3220_V1
,
244 .hw_id
= HWID_TL_MR3220_V2
,
246 .layout_id
= "4Mlzma",
249 .hw_id
= HWID_TL_MR3420_V1
,
254 .hw_id
= HWID_TL_MR3420_V2
,
256 .layout_id
= "4Mlzma",
259 .hw_id
= HWID_TL_WA701N_V1
,
264 .hw_id
= HWID_TL_WA701N_V2
,
266 .layout_id
= "4Mlzma",
269 .hw_id
= HWID_TL_WA7210N_V2
,
271 .layout_id
= "4Mlzma",
274 .hw_id
= HWID_TL_WA7510N_V1
,
278 .id
= "TL-WA801NDv1",
279 .hw_id
= HWID_TL_WA801ND_V1
,
283 .id
= "TL-WA830REv1",
284 .hw_id
= HWID_TL_WA830RE_V1
,
288 .id
= "TL-WA830REv2",
289 .hw_id
= HWID_TL_WA830RE_V2
,
293 .id
= "TL-WA801NDv2",
294 .hw_id
= HWID_TL_WA801ND_V2
,
296 .layout_id
= "4Mlzma",
298 .id
= "TL-WA901NDv1",
299 .hw_id
= HWID_TL_WA901ND_V1
,
303 .id
= "TL-WA901NDv2",
304 .hw_id
= HWID_TL_WA901ND_V2
,
308 .id
= "TL-WDR4300v1",
309 .hw_id
= HWID_TL_WDR4300_V1_IL
,
311 .layout_id
= "8Mlzma",
313 .id
= "TL-WDR4900v1",
314 .hw_id
= HWID_TL_WDR4900_V1
,
316 .layout_id
= "16Mppc",
318 .id
= "TL-WR741NDv1",
319 .hw_id
= HWID_TL_WR741ND_V1
,
323 .id
= "TL-WR741NDv4",
324 .hw_id
= HWID_TL_WR741ND_V4
,
326 .layout_id
= "4Mlzma",
329 .hw_id
= HWID_TL_WR740N_V1
,
334 .hw_id
= HWID_TL_WR740N_V3
,
338 .id
= "TL-WR743NDv1",
339 .hw_id
= HWID_TL_WR743ND_V1
,
343 .id
= "TL-WR743NDv2",
344 .hw_id
= HWID_TL_WR743ND_V2
,
346 .layout_id
= "4Mlzma",
348 .id
= "TL-WR841Nv1.5",
349 .hw_id
= HWID_TL_WR841N_V1_5
,
353 .id
= "TL-WR841NDv3",
354 .hw_id
= HWID_TL_WR841ND_V3
,
358 .id
= "TL-WR841NDv5",
359 .hw_id
= HWID_TL_WR841ND_V5
,
363 .id
= "TL-WR841NDv7",
364 .hw_id
= HWID_TL_WR841ND_V7
,
368 .id
= "TL-WR941NDv2",
369 .hw_id
= HWID_TL_WR941ND_V2
,
373 .id
= "TL-WR941NDv4",
374 .hw_id
= HWID_TL_WR941ND_V4
,
378 .id
= "TL-WR1041Nv2",
379 .hw_id
= HWID_TL_WR1041N_V2
,
381 .layout_id
= "4Mlzma",
383 .id
= "TL-WR1043NDv1",
384 .hw_id
= HWID_TL_WR1043ND_V1
,
388 .id
= "TL-WR1043NDv2",
389 .hw_id
= HWID_TL_WR1043ND_V2
,
391 .layout_id
= "8Mlzma",
393 .id
= "TL-WR2543Nv1",
394 .hw_id
= HWID_TL_WR2543N_V1
,
396 .layout_id
= "8Mlzma",
399 .hw_id
= HWID_TL_WR703N_V1
,
401 .layout_id
= "4Mlzma",
404 .hw_id
= HWID_TL_WR720N_V3
,
406 .layout_id
= "4Mlzma",
409 .hw_id
= HWID_TL_WR720N_V4
,
411 .layout_id
= "4Mlzma",
414 .hw_id
= HWID_GL_INET_V1
,
416 .layout_id
= "8Mlzma",
419 .hw_id
= HWID_GS_OOLITE_V1
,
421 .layout_id
= "16Mlzma",
424 .hw_id
= HWID_ANTMINER_S1
,
426 .layout_id
= "8Mlzma",
428 /* terminating entry */
435 #define ERR(fmt, ...) do { \
437 fprintf(stderr, "[%s] *** error: " fmt "\n", \
438 progname, ## __VA_ARGS__ ); \
441 #define ERRS(fmt, ...) do { \
444 fprintf(stderr, "[%s] *** error: " fmt "\n", \
445 progname, ## __VA_ARGS__, strerror(save)); \
448 #define DBG(fmt, ...) do { \
449 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
452 static struct board_info
*find_board(char *id
)
454 struct board_info
*ret
;
455 struct board_info
*board
;
458 for (board
= boards
; board
->id
!= NULL
; board
++){
459 if (strcasecmp(id
, board
->id
) == 0) {
468 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
470 struct board_info
*board
;
472 for (board
= boards
; board
->id
!= NULL
; board
++) {
473 if (hw_id
== board
->hw_id
)
480 static struct flash_layout
*find_layout(char *id
)
482 struct flash_layout
*ret
;
483 struct flash_layout
*l
;
486 for (l
= layouts
; l
->id
!= NULL
; l
++){
487 if (strcasecmp(id
, l
->id
) == 0) {
496 static void usage(int status
)
498 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
499 struct board_info
*board
;
501 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
505 " -B <board> create image for the board specified with <board>\n"
506 " -c use combined kernel image\n"
507 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
508 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
509 " -H <hwid> use hardware id specified with <hwid>\n"
510 " -W <hwrev> use hardware revision specified with <hwrev>\n"
511 " -F <id> use flash layout specified with <id>\n"
512 " -k <file> read kernel image from the file <file>\n"
513 " -r <file> read rootfs image from the file <file>\n"
514 " -a <align> align the rootfs start on an <align> bytes boundary\n"
515 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
516 " -o <file> write output to the file <file>\n"
517 " -s strip padding from the end of the image\n"
518 " -S ignore firmware size limit (only for combined images)\n"
519 " -j add jffs2 end-of-filesystem markers\n"
520 " -N <vendor> set image vendor to <vendor>\n"
521 " -V <version> set image version to <version>\n"
522 " -v <version> set firmware version to <version>\n"
523 " -i <file> inspect given firmware file <file>\n"
524 " -x extract kernel and rootfs while inspecting (requires -i)\n"
525 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
526 " -h show this screen\n"
532 static int get_md5(char *data
, int size
, char *md5
)
537 MD5_Update(&ctx
, data
, size
);
538 MD5_Final(md5
, &ctx
);
541 static int get_file_stat(struct file_info
*fdata
)
546 if (fdata
->file_name
== NULL
)
549 res
= stat(fdata
->file_name
, &st
);
551 ERRS("stat failed on %s", fdata
->file_name
);
555 fdata
->file_size
= st
.st_size
;
559 static int read_to_buf(struct file_info
*fdata
, char *buf
)
562 int ret
= EXIT_FAILURE
;
564 f
= fopen(fdata
->file_name
, "r");
566 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
571 fread(buf
, fdata
->file_size
, 1, f
);
573 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
585 static int check_options(void)
589 if (inspect_info
.file_name
) {
590 ret
= get_file_stat(&inspect_info
);
595 } else if (extract
) {
596 ERR("no firmware for inspection specified");
600 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
601 ERR("either board or hardware id must be specified");
606 board
= find_board(board_id
);
608 ERR("unknown/unsupported board id \"%s\"", board_id
);
611 if (layout_id
== NULL
)
612 layout_id
= board
->layout_id
;
614 hw_id
= board
->hw_id
;
615 hw_rev
= board
->hw_rev
;
617 if (layout_id
== NULL
) {
618 ERR("flash layout is not specified");
621 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
624 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
629 layout
= find_layout(layout_id
);
630 if (layout
== NULL
) {
631 ERR("unknown flash layout \"%s\"", layout_id
);
636 kernel_la
= layout
->kernel_la
;
638 kernel_ep
= layout
->kernel_ep
;
640 rootfs_ofs
= layout
->rootfs_ofs
;
642 if (reserved_space
> layout
->fw_max_len
) {
643 ERR("reserved space is not valid");
647 fw_max_len
= layout
->fw_max_len
- reserved_space
;
649 if (kernel_info
.file_name
== NULL
) {
650 ERR("no kernel image specified");
654 ret
= get_file_stat(&kernel_info
);
658 kernel_len
= kernel_info
.file_size
;
661 if (kernel_info
.file_size
>
662 fw_max_len
- sizeof(struct fw_header
)) {
664 ERR("kernel image is too big");
667 layout
->fw_max_len
= sizeof(struct fw_header
) +
668 kernel_info
.file_size
+
672 if (rootfs_info
.file_name
== NULL
) {
673 ERR("no rootfs image specified");
677 ret
= get_file_stat(&rootfs_info
);
682 kernel_len
+= sizeof(struct fw_header
);
683 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
684 kernel_len
-= sizeof(struct fw_header
);
686 DBG("kernel length aligned to %u", kernel_len
);
688 if (kernel_len
+ rootfs_info
.file_size
>
689 fw_max_len
- sizeof(struct fw_header
)) {
690 ERR("images are too big");
694 if (kernel_info
.file_size
>
695 rootfs_ofs
- sizeof(struct fw_header
)) {
696 ERR("kernel image is too big");
700 if (rootfs_info
.file_size
>
701 (fw_max_len
- rootfs_ofs
)) {
702 ERR("rootfs image is too big");
708 if (ofname
== NULL
) {
709 ERR("no output file specified");
713 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
715 ERR("invalid firmware version '%s'", fw_ver
);
722 static void fill_header(char *buf
, int len
)
724 struct fw_header
*hdr
= (struct fw_header
*)buf
;
726 memset(hdr
, 0, sizeof(struct fw_header
));
728 hdr
->version
= htonl(HEADER_VERSION_V1
);
729 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
730 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
731 hdr
->hw_id
= htonl(hw_id
);
732 hdr
->hw_rev
= htonl(hw_rev
);
734 if (boot_info
.file_size
== 0)
735 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
737 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
739 hdr
->kernel_la
= htonl(kernel_la
);
740 hdr
->kernel_ep
= htonl(kernel_ep
);
741 hdr
->fw_length
= htonl(layout
->fw_max_len
);
742 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
743 hdr
->kernel_len
= htonl(kernel_len
);
745 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
746 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
749 hdr
->ver_hi
= htons(fw_ver_hi
);
750 hdr
->ver_mid
= htons(fw_ver_mid
);
751 hdr
->ver_lo
= htons(fw_ver_lo
);
753 get_md5(buf
, len
, hdr
->md5sum1
);
756 static int pad_jffs2(char *buf
, int currlen
)
762 pad_mask
= (64 * 1024);
763 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
767 for (i
= 10; i
< 32; i
++) {
773 len
= ALIGN(len
, mask
);
775 for (i
= 10; i
< 32; i
++) {
777 if ((len
& (mask
- 1)) == 0)
781 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
782 buf
[len
+ i
] = jffs2_eof_mark
[i
];
784 len
+= sizeof(jffs2_eof_mark
);
790 static int write_fw(char *data
, int len
)
793 int ret
= EXIT_FAILURE
;
795 f
= fopen(ofname
, "w");
797 ERRS("could not open \"%s\" for writing", ofname
);
802 fwrite(data
, len
, 1, f
);
804 ERRS("unable to write output file");
808 DBG("firmware file \"%s\" completed", ofname
);
815 if (ret
!= EXIT_SUCCESS
) {
822 static int build_fw(void)
827 int ret
= EXIT_FAILURE
;
830 buflen
= layout
->fw_max_len
;
832 buf
= malloc(buflen
);
834 ERR("no memory for buffer\n");
838 memset(buf
, 0xff, buflen
);
839 p
= buf
+ sizeof(struct fw_header
);
840 ret
= read_to_buf(&kernel_info
, p
);
844 writelen
= sizeof(struct fw_header
) + kernel_len
;
850 p
= buf
+ rootfs_ofs
;
852 ret
= read_to_buf(&rootfs_info
, p
);
857 writelen
+= rootfs_info
.file_size
;
859 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
862 writelen
= pad_jffs2(buf
, writelen
);
868 fill_header(buf
, writelen
);
869 ret
= write_fw(buf
, writelen
);
881 /* Helper functions to inspect_fw() representing different output formats */
882 static inline void inspect_fw_pstr(char *label
, char *str
)
884 printf("%-23s: %s\n", label
, str
);
887 static inline void inspect_fw_phex(char *label
, uint32_t val
)
889 printf("%-23s: 0x%08x\n", label
, val
);
892 static inline void inspect_fw_phexpost(char *label
,
893 uint32_t val
, char *post
)
895 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
898 static inline void inspect_fw_phexdef(char *label
,
899 uint32_t val
, uint32_t defval
)
901 printf("%-23s: 0x%08x ", label
, val
);
904 printf("(== OpenWrt default)\n");
906 printf("(OpenWrt default: 0x%08x)\n", defval
);
909 static inline void inspect_fw_phexexp(char *label
,
910 uint32_t val
, uint32_t expval
)
912 printf("%-23s: 0x%08x ", label
, val
);
917 printf("(expected: 0x%08x)\n", expval
);
920 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
922 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
925 static inline void inspect_fw_phexdecdef(char *label
,
926 uint32_t val
, uint32_t defval
)
928 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
931 printf("(== OpenWrt default)\n");
933 printf("(OpenWrt default: 0x%08x)\n", defval
);
936 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
940 printf("%-23s:", label
);
941 for (i
=0; i
<MD5SUM_LEN
; i
++)
942 printf(" %02x", val
[i
]);
943 printf(" %s\n", text
);
946 static int inspect_fw(void)
949 struct fw_header
*hdr
;
950 uint8_t md5sum
[MD5SUM_LEN
];
951 struct board_info
*board
;
952 int ret
= EXIT_FAILURE
;
954 buf
= malloc(inspect_info
.file_size
);
956 ERR("no memory for buffer!\n");
960 ret
= read_to_buf(&inspect_info
, buf
);
963 hdr
= (struct fw_header
*)buf
;
965 inspect_fw_pstr("File name", inspect_info
.file_name
);
966 inspect_fw_phexdec("File size", inspect_info
.file_size
);
968 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
969 ERR("file does not seem to have V1 header!\n");
973 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
975 if (ntohl(hdr
->unk1
) != 0)
976 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
978 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
979 if (ntohl(hdr
->boot_len
) == 0)
980 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
982 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
983 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
985 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
986 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
987 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
989 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
991 if (ntohl(hdr
->unk2
) != 0)
992 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
993 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
994 "(purpose yet unknown, unchecked here)");
995 if (ntohl(hdr
->unk3
) != 0)
996 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
1000 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
1001 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
1002 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
1004 layout
= find_layout(board
->layout_id
);
1005 inspect_fw_phexpost("Hardware ID",
1006 ntohl(hdr
->hw_id
), board
->id
);
1007 inspect_fw_phexexp("Hardware Revision",
1008 ntohl(hdr
->hw_rev
), board
->hw_rev
);
1010 inspect_fw_phexpost("Hardware ID",
1011 ntohl(hdr
->hw_id
), "unknown");
1012 inspect_fw_phex("Hardware Revision",
1013 ntohl(hdr
->hw_rev
));
1018 inspect_fw_phexdec("Kernel data offset",
1019 ntohl(hdr
->kernel_ofs
));
1020 inspect_fw_phexdec("Kernel data length",
1021 ntohl(hdr
->kernel_len
));
1023 inspect_fw_phexdef("Kernel load address",
1024 ntohl(hdr
->kernel_la
),
1025 layout
? layout
->kernel_la
: 0xffffffff);
1026 inspect_fw_phexdef("Kernel entry point",
1027 ntohl(hdr
->kernel_ep
),
1028 layout
? layout
->kernel_ep
: 0xffffffff);
1029 inspect_fw_phexdecdef("Rootfs data offset",
1030 ntohl(hdr
->rootfs_ofs
),
1031 layout
? layout
->rootfs_ofs
: 0xffffffff);
1033 inspect_fw_phex("Kernel load address",
1034 ntohl(hdr
->kernel_la
));
1035 inspect_fw_phex("Kernel entry point",
1036 ntohl(hdr
->kernel_ep
));
1037 inspect_fw_phexdec("Rootfs data offset",
1038 ntohl(hdr
->rootfs_ofs
));
1040 inspect_fw_phexdec("Rootfs data length",
1041 ntohl(hdr
->rootfs_len
));
1042 inspect_fw_phexdec("Boot loader data offset",
1043 ntohl(hdr
->boot_ofs
));
1044 inspect_fw_phexdec("Boot loader data length",
1045 ntohl(hdr
->boot_len
));
1046 inspect_fw_phexdec("Total firmware length",
1047 ntohl(hdr
->fw_length
));
1055 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
1056 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
1057 printf("Extracting kernel to \"%s\"...\n", filename
);
1058 fp
= fopen(filename
, "w");
1060 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
1061 ntohl(hdr
->kernel_len
), 1, fp
)) {
1062 ERR("error in fwrite(): %s", strerror(errno
));
1066 ERR("error in fopen(): %s", strerror(errno
));
1070 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
1071 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
1072 printf("Extracting rootfs to \"%s\"...\n", filename
);
1073 fp
= fopen(filename
, "w");
1075 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
1076 ntohl(hdr
->rootfs_len
), 1, fp
)) {
1077 ERR("error in fwrite(): %s", strerror(errno
));
1081 ERR("error in fopen(): %s", strerror(errno
));
1092 int main(int argc
, char *argv
[])
1094 int ret
= EXIT_FAILURE
;
1099 progname
= basename(argv
[0]);
1104 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsSjv:");
1110 sscanf(optarg
, "0x%x", &rootfs_align
);
1119 sscanf(optarg
, "0x%x", &kernel_ep
);
1125 opt_hw_rev
= optarg
;
1128 sscanf(optarg
, "0x%x", &kernel_la
);
1143 kernel_info
.file_name
= optarg
;
1146 rootfs_info
.file_name
= optarg
;
1149 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1161 inspect_info
.file_name
= optarg
;
1170 usage(EXIT_SUCCESS
);
1173 sscanf(optarg
, "0x%x", &reserved_space
);
1176 usage(EXIT_FAILURE
);
1181 ret
= check_options();
1185 if (!inspect_info
.file_name
)