99338d4d6787531b8a5094c39e0debaf9dfede53
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_ONION_OMEGA 0x04700001
38 #define HWID_TL_MR10U_V1 0x00100101
39 #define HWID_TL_MR13U_V1 0x00130101
40 #define HWID_TL_MR3020_V1 0x30200001
41 #define HWID_TL_MR3220_V1 0x32200001
42 #define HWID_TL_MR3220_V2 0x32200002
43 #define HWID_TL_MR3420_V1 0x34200001
44 #define HWID_TL_MR3420_V2 0x34200002
45 #define HWID_TL_WA701N_V1 0x07010001
46 #define HWID_TL_WA701N_V2 0x07010002
47 #define HWID_TL_WA7210N_V2 0x72100002
48 #define HWID_TL_WA7510N_V1 0x75100001
49 #define HWID_TL_WA801ND_V1 0x08010001
50 #define HWID_TL_WA830RE_V1 0x08300010
51 #define HWID_TL_WA830RE_V2 0x08300002
52 #define HWID_TL_WA801ND_V2 0x08010002
53 #define HWID_TL_WA901ND_V1 0x09010001
54 #define HWID_TL_WA901ND_V2 0x09010002
55 #define HWID_TL_WDR4300_V1_IL 0x43008001
56 #define HWID_TL_WDR4900_V1 0x49000001
57 #define HWID_TL_WR703N_V1 0x07030101
58 #define HWID_TL_WR720N_V3 0x07200103
59 #define HWID_TL_WR720N_V4 0x07200104
60 #define HWID_TL_WR741ND_V1 0x07410001
61 #define HWID_TL_WR741ND_V4 0x07410004
62 #define HWID_TL_WR740N_V1 0x07400001
63 #define HWID_TL_WR740N_V3 0x07400003
64 #define HWID_TL_WR743ND_V1 0x07430001
65 #define HWID_TL_WR743ND_V2 0x07430002
66 #define HWID_TL_WR841N_V1_5 0x08410002
67 #define HWID_TL_WR841ND_V3 0x08410003
68 #define HWID_TL_WR841ND_V5 0x08410005
69 #define HWID_TL_WR841ND_V7 0x08410007
70 #define HWID_TL_WR941ND_V2 0x09410002
71 #define HWID_TL_WR941ND_V4 0x09410004
72 #define HWID_TL_WR1043ND_V1 0x10430001
73 #define HWID_TL_WR1043ND_V2 0x10430002
74 #define HWID_TL_WR1041N_V2 0x10410002
75 #define HWID_TL_WR2543N_V1 0x25430001
80 char *file_name
; /* name of the file */
81 uint32_t file_size
; /* length of the file */
85 uint32_t version
; /* header version */
88 uint32_t hw_id
; /* hardware id */
89 uint32_t hw_rev
; /* hardware revision */
91 uint8_t md5sum1
[MD5SUM_LEN
];
93 uint8_t md5sum2
[MD5SUM_LEN
];
95 uint32_t kernel_la
; /* kernel load address */
96 uint32_t kernel_ep
; /* kernel entry point */
97 uint32_t fw_length
; /* total length of the firmware */
98 uint32_t kernel_ofs
; /* kernel data offset */
99 uint32_t kernel_len
; /* kernel data length */
100 uint32_t rootfs_ofs
; /* rootfs data offset */
101 uint32_t rootfs_len
; /* rootfs data length */
102 uint32_t boot_ofs
; /* bootloader data offset */
103 uint32_t boot_len
; /* bootloader data length */
108 } __attribute__ ((packed
));
110 struct flash_layout
{
129 static char *progname
;
130 static char *vendor
= "TP-LINK Technologies";
131 static char *version
= "ver. 1.0";
132 static char *fw_ver
= "0.0.0";
134 static char *board_id
;
135 static struct board_info
*board
;
136 static char *layout_id
;
137 static struct flash_layout
*layout
;
138 static char *opt_hw_id
;
139 static uint32_t hw_id
;
140 static char *opt_hw_rev
;
141 static uint32_t hw_rev
;
142 static int fw_ver_lo
;
143 static int fw_ver_mid
;
144 static int fw_ver_hi
;
145 static struct file_info kernel_info
;
146 static uint32_t kernel_la
= 0;
147 static uint32_t kernel_ep
= 0;
148 static uint32_t kernel_len
= 0;
149 static struct file_info rootfs_info
;
150 static uint32_t rootfs_ofs
= 0;
151 static uint32_t rootfs_align
;
152 static struct file_info boot_info
;
154 static int strip_padding
;
155 static int ignore_size
;
156 static int add_jffs2_eof
;
157 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
158 static uint32_t fw_max_len
;
159 static uint32_t reserved_space
;
161 static struct file_info inspect_info
;
162 static int extract
= 0;
164 char md5salt_normal
[MD5SUM_LEN
] = {
165 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
166 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
169 char md5salt_boot
[MD5SUM_LEN
] = {
170 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
171 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
174 static struct flash_layout layouts
[] = {
177 .fw_max_len
= 0x3c0000,
178 .kernel_la
= 0x80060000,
179 .kernel_ep
= 0x80060000,
180 .rootfs_ofs
= 0x140000,
183 .fw_max_len
= 0x3c0000,
184 .kernel_la
= 0x80060000,
185 .kernel_ep
= 0x80060000,
186 .rootfs_ofs
= 0x100000,
189 .fw_max_len
= 0x7c0000,
190 .kernel_la
= 0x80060000,
191 .kernel_ep
= 0x80060000,
192 .rootfs_ofs
= 0x140000,
195 .fw_max_len
= 0x7c0000,
196 .kernel_la
= 0x80060000,
197 .kernel_ep
= 0x80060000,
198 .rootfs_ofs
= 0x100000,
201 .fw_max_len
= 0xf80000,
202 .kernel_la
= 0x80060000,
203 .kernel_ep
= 0x80060000,
204 .rootfs_ofs
= 0x140000,
207 .fw_max_len
= 0xf80000,
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_MR13U_V1
,
232 .layout_id
= "4Mlzma",
235 .hw_id
= HWID_TL_MR3020_V1
,
237 .layout_id
= "4Mlzma",
240 .hw_id
= HWID_TL_MR3220_V1
,
245 .hw_id
= HWID_TL_MR3220_V2
,
247 .layout_id
= "4Mlzma",
250 .hw_id
= HWID_TL_MR3420_V1
,
255 .hw_id
= HWID_TL_MR3420_V2
,
257 .layout_id
= "4Mlzma",
260 .hw_id
= HWID_TL_WA701N_V1
,
265 .hw_id
= HWID_TL_WA701N_V2
,
267 .layout_id
= "4Mlzma",
270 .hw_id
= HWID_TL_WA7210N_V2
,
272 .layout_id
= "4Mlzma",
275 .hw_id
= HWID_TL_WA7510N_V1
,
279 .id
= "TL-WA801NDv1",
280 .hw_id
= HWID_TL_WA801ND_V1
,
284 .id
= "TL-WA830REv1",
285 .hw_id
= HWID_TL_WA830RE_V1
,
289 .id
= "TL-WA830REv2",
290 .hw_id
= HWID_TL_WA830RE_V2
,
294 .id
= "TL-WA801NDv2",
295 .hw_id
= HWID_TL_WA801ND_V2
,
297 .layout_id
= "4Mlzma",
299 .id
= "TL-WA901NDv1",
300 .hw_id
= HWID_TL_WA901ND_V1
,
304 .id
= "TL-WA901NDv2",
305 .hw_id
= HWID_TL_WA901ND_V2
,
309 .id
= "TL-WDR4300v1",
310 .hw_id
= HWID_TL_WDR4300_V1_IL
,
312 .layout_id
= "8Mlzma",
314 .id
= "TL-WDR4900v1",
315 .hw_id
= HWID_TL_WDR4900_V1
,
317 .layout_id
= "16Mppc",
319 .id
= "TL-WR741NDv1",
320 .hw_id
= HWID_TL_WR741ND_V1
,
324 .id
= "TL-WR741NDv4",
325 .hw_id
= HWID_TL_WR741ND_V4
,
327 .layout_id
= "4Mlzma",
330 .hw_id
= HWID_TL_WR740N_V1
,
335 .hw_id
= HWID_TL_WR740N_V3
,
339 .id
= "TL-WR743NDv1",
340 .hw_id
= HWID_TL_WR743ND_V1
,
344 .id
= "TL-WR743NDv2",
345 .hw_id
= HWID_TL_WR743ND_V2
,
347 .layout_id
= "4Mlzma",
349 .id
= "TL-WR841Nv1.5",
350 .hw_id
= HWID_TL_WR841N_V1_5
,
354 .id
= "TL-WR841NDv3",
355 .hw_id
= HWID_TL_WR841ND_V3
,
359 .id
= "TL-WR841NDv5",
360 .hw_id
= HWID_TL_WR841ND_V5
,
364 .id
= "TL-WR841NDv7",
365 .hw_id
= HWID_TL_WR841ND_V7
,
369 .id
= "TL-WR941NDv2",
370 .hw_id
= HWID_TL_WR941ND_V2
,
374 .id
= "TL-WR941NDv4",
375 .hw_id
= HWID_TL_WR941ND_V4
,
379 .id
= "TL-WR1041Nv2",
380 .hw_id
= HWID_TL_WR1041N_V2
,
382 .layout_id
= "4Mlzma",
384 .id
= "TL-WR1043NDv1",
385 .hw_id
= HWID_TL_WR1043ND_V1
,
389 .id
= "TL-WR1043NDv2",
390 .hw_id
= HWID_TL_WR1043ND_V2
,
392 .layout_id
= "8Mlzma",
394 .id
= "TL-WR2543Nv1",
395 .hw_id
= HWID_TL_WR2543N_V1
,
397 .layout_id
= "8Mlzma",
400 .hw_id
= HWID_TL_WR703N_V1
,
402 .layout_id
= "4Mlzma",
405 .hw_id
= HWID_TL_WR720N_V3
,
407 .layout_id
= "4Mlzma",
410 .hw_id
= HWID_TL_WR720N_V4
,
412 .layout_id
= "4Mlzma",
415 .hw_id
= HWID_GL_INET_V1
,
417 .layout_id
= "8Mlzma",
420 .hw_id
= HWID_GS_OOLITE_V1
,
422 .layout_id
= "16Mlzma",
425 .hw_id
= HWID_ONION_OMEGA
,
427 .layout_id
= "16Mlzma",
430 .hw_id
= HWID_ANTMINER_S1
,
432 .layout_id
= "8Mlzma",
434 /* terminating entry */
441 #define ERR(fmt, ...) do { \
443 fprintf(stderr, "[%s] *** error: " fmt "\n", \
444 progname, ## __VA_ARGS__ ); \
447 #define ERRS(fmt, ...) do { \
450 fprintf(stderr, "[%s] *** error: " fmt "\n", \
451 progname, ## __VA_ARGS__, strerror(save)); \
454 #define DBG(fmt, ...) do { \
455 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
458 static struct board_info
*find_board(char *id
)
460 struct board_info
*ret
;
461 struct board_info
*board
;
464 for (board
= boards
; board
->id
!= NULL
; board
++){
465 if (strcasecmp(id
, board
->id
) == 0) {
474 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
476 struct board_info
*board
;
478 for (board
= boards
; board
->id
!= NULL
; board
++) {
479 if (hw_id
== board
->hw_id
)
486 static struct flash_layout
*find_layout(char *id
)
488 struct flash_layout
*ret
;
489 struct flash_layout
*l
;
492 for (l
= layouts
; l
->id
!= NULL
; l
++){
493 if (strcasecmp(id
, l
->id
) == 0) {
502 static void usage(int status
)
504 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
505 struct board_info
*board
;
507 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
511 " -B <board> create image for the board specified with <board>\n"
512 " -c use combined kernel image\n"
513 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
514 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
515 " -H <hwid> use hardware id specified with <hwid>\n"
516 " -W <hwrev> use hardware revision specified with <hwrev>\n"
517 " -F <id> use flash layout specified with <id>\n"
518 " -k <file> read kernel image from the file <file>\n"
519 " -r <file> read rootfs image from the file <file>\n"
520 " -a <align> align the rootfs start on an <align> bytes boundary\n"
521 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
522 " -o <file> write output to the file <file>\n"
523 " -s strip padding from the end of the image\n"
524 " -S ignore firmware size limit (only for combined images)\n"
525 " -j add jffs2 end-of-filesystem markers\n"
526 " -N <vendor> set image vendor to <vendor>\n"
527 " -V <version> set image version to <version>\n"
528 " -v <version> set firmware version to <version>\n"
529 " -i <file> inspect given firmware file <file>\n"
530 " -x extract kernel and rootfs while inspecting (requires -i)\n"
531 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
532 " -h show this screen\n"
538 static int get_md5(char *data
, int size
, char *md5
)
543 MD5_Update(&ctx
, data
, size
);
544 MD5_Final(md5
, &ctx
);
547 static int get_file_stat(struct file_info
*fdata
)
552 if (fdata
->file_name
== NULL
)
555 res
= stat(fdata
->file_name
, &st
);
557 ERRS("stat failed on %s", fdata
->file_name
);
561 fdata
->file_size
= st
.st_size
;
565 static int read_to_buf(struct file_info
*fdata
, char *buf
)
568 int ret
= EXIT_FAILURE
;
570 f
= fopen(fdata
->file_name
, "r");
572 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
577 fread(buf
, fdata
->file_size
, 1, f
);
579 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
591 static int check_options(void)
595 if (inspect_info
.file_name
) {
596 ret
= get_file_stat(&inspect_info
);
601 } else if (extract
) {
602 ERR("no firmware for inspection specified");
606 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
607 ERR("either board or hardware id must be specified");
612 board
= find_board(board_id
);
614 ERR("unknown/unsupported board id \"%s\"", board_id
);
617 if (layout_id
== NULL
)
618 layout_id
= board
->layout_id
;
620 hw_id
= board
->hw_id
;
621 hw_rev
= board
->hw_rev
;
623 if (layout_id
== NULL
) {
624 ERR("flash layout is not specified");
627 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
630 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
635 layout
= find_layout(layout_id
);
636 if (layout
== NULL
) {
637 ERR("unknown flash layout \"%s\"", layout_id
);
642 kernel_la
= layout
->kernel_la
;
644 kernel_ep
= layout
->kernel_ep
;
646 rootfs_ofs
= layout
->rootfs_ofs
;
648 if (reserved_space
> layout
->fw_max_len
) {
649 ERR("reserved space is not valid");
653 fw_max_len
= layout
->fw_max_len
- reserved_space
;
655 if (kernel_info
.file_name
== NULL
) {
656 ERR("no kernel image specified");
660 ret
= get_file_stat(&kernel_info
);
664 kernel_len
= kernel_info
.file_size
;
667 if (kernel_info
.file_size
>
668 fw_max_len
- sizeof(struct fw_header
)) {
670 ERR("kernel image is too big");
673 layout
->fw_max_len
= sizeof(struct fw_header
) +
674 kernel_info
.file_size
+
678 if (rootfs_info
.file_name
== NULL
) {
679 ERR("no rootfs image specified");
683 ret
= get_file_stat(&rootfs_info
);
688 kernel_len
+= sizeof(struct fw_header
);
689 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
690 kernel_len
-= sizeof(struct fw_header
);
692 DBG("kernel length aligned to %u", kernel_len
);
694 if (kernel_len
+ rootfs_info
.file_size
>
695 fw_max_len
- sizeof(struct fw_header
)) {
696 ERR("images are too big");
700 if (kernel_info
.file_size
>
701 rootfs_ofs
- sizeof(struct fw_header
)) {
702 ERR("kernel image is too big");
706 if (rootfs_info
.file_size
>
707 (fw_max_len
- rootfs_ofs
)) {
708 ERR("rootfs image is too big");
714 if (ofname
== NULL
) {
715 ERR("no output file specified");
719 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
721 ERR("invalid firmware version '%s'", fw_ver
);
728 static void fill_header(char *buf
, int len
)
730 struct fw_header
*hdr
= (struct fw_header
*)buf
;
732 memset(hdr
, 0, sizeof(struct fw_header
));
734 hdr
->version
= htonl(HEADER_VERSION_V1
);
735 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
736 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
737 hdr
->hw_id
= htonl(hw_id
);
738 hdr
->hw_rev
= htonl(hw_rev
);
740 if (boot_info
.file_size
== 0)
741 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
743 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
745 hdr
->kernel_la
= htonl(kernel_la
);
746 hdr
->kernel_ep
= htonl(kernel_ep
);
747 hdr
->fw_length
= htonl(layout
->fw_max_len
);
748 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
749 hdr
->kernel_len
= htonl(kernel_len
);
751 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
752 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
755 hdr
->ver_hi
= htons(fw_ver_hi
);
756 hdr
->ver_mid
= htons(fw_ver_mid
);
757 hdr
->ver_lo
= htons(fw_ver_lo
);
759 get_md5(buf
, len
, hdr
->md5sum1
);
762 static int pad_jffs2(char *buf
, int currlen
)
768 pad_mask
= (64 * 1024);
769 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
773 for (i
= 10; i
< 32; i
++) {
779 len
= ALIGN(len
, mask
);
781 for (i
= 10; i
< 32; i
++) {
783 if ((len
& (mask
- 1)) == 0)
787 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
788 buf
[len
+ i
] = jffs2_eof_mark
[i
];
790 len
+= sizeof(jffs2_eof_mark
);
796 static int write_fw(char *data
, int len
)
799 int ret
= EXIT_FAILURE
;
801 f
= fopen(ofname
, "w");
803 ERRS("could not open \"%s\" for writing", ofname
);
808 fwrite(data
, len
, 1, f
);
810 ERRS("unable to write output file");
814 DBG("firmware file \"%s\" completed", ofname
);
821 if (ret
!= EXIT_SUCCESS
) {
828 static int build_fw(void)
833 int ret
= EXIT_FAILURE
;
836 buflen
= layout
->fw_max_len
;
838 buf
= malloc(buflen
);
840 ERR("no memory for buffer\n");
844 memset(buf
, 0xff, buflen
);
845 p
= buf
+ sizeof(struct fw_header
);
846 ret
= read_to_buf(&kernel_info
, p
);
850 writelen
= sizeof(struct fw_header
) + kernel_len
;
856 p
= buf
+ rootfs_ofs
;
858 ret
= read_to_buf(&rootfs_info
, p
);
863 writelen
+= rootfs_info
.file_size
;
865 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
868 writelen
= pad_jffs2(buf
, writelen
);
874 fill_header(buf
, writelen
);
875 ret
= write_fw(buf
, writelen
);
887 /* Helper functions to inspect_fw() representing different output formats */
888 static inline void inspect_fw_pstr(char *label
, char *str
)
890 printf("%-23s: %s\n", label
, str
);
893 static inline void inspect_fw_phex(char *label
, uint32_t val
)
895 printf("%-23s: 0x%08x\n", label
, val
);
898 static inline void inspect_fw_phexpost(char *label
,
899 uint32_t val
, char *post
)
901 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
904 static inline void inspect_fw_phexdef(char *label
,
905 uint32_t val
, uint32_t defval
)
907 printf("%-23s: 0x%08x ", label
, val
);
910 printf("(== OpenWrt default)\n");
912 printf("(OpenWrt default: 0x%08x)\n", defval
);
915 static inline void inspect_fw_phexexp(char *label
,
916 uint32_t val
, uint32_t expval
)
918 printf("%-23s: 0x%08x ", label
, val
);
923 printf("(expected: 0x%08x)\n", expval
);
926 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
928 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
931 static inline void inspect_fw_phexdecdef(char *label
,
932 uint32_t val
, uint32_t defval
)
934 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
937 printf("(== OpenWrt default)\n");
939 printf("(OpenWrt default: 0x%08x)\n", defval
);
942 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
946 printf("%-23s:", label
);
947 for (i
=0; i
<MD5SUM_LEN
; i
++)
948 printf(" %02x", val
[i
]);
949 printf(" %s\n", text
);
952 static int inspect_fw(void)
955 struct fw_header
*hdr
;
956 uint8_t md5sum
[MD5SUM_LEN
];
957 struct board_info
*board
;
958 int ret
= EXIT_FAILURE
;
960 buf
= malloc(inspect_info
.file_size
);
962 ERR("no memory for buffer!\n");
966 ret
= read_to_buf(&inspect_info
, buf
);
969 hdr
= (struct fw_header
*)buf
;
971 inspect_fw_pstr("File name", inspect_info
.file_name
);
972 inspect_fw_phexdec("File size", inspect_info
.file_size
);
974 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
975 ERR("file does not seem to have V1 header!\n");
979 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
981 if (ntohl(hdr
->unk1
) != 0)
982 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
984 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
985 if (ntohl(hdr
->boot_len
) == 0)
986 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
988 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
989 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
991 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
992 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
993 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
995 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
997 if (ntohl(hdr
->unk2
) != 0)
998 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
999 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
1000 "(purpose yet unknown, unchecked here)");
1001 if (ntohl(hdr
->unk3
) != 0)
1002 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
1006 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
1007 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
1008 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
1010 layout
= find_layout(board
->layout_id
);
1011 inspect_fw_phexpost("Hardware ID",
1012 ntohl(hdr
->hw_id
), board
->id
);
1013 inspect_fw_phexexp("Hardware Revision",
1014 ntohl(hdr
->hw_rev
), board
->hw_rev
);
1016 inspect_fw_phexpost("Hardware ID",
1017 ntohl(hdr
->hw_id
), "unknown");
1018 inspect_fw_phex("Hardware Revision",
1019 ntohl(hdr
->hw_rev
));
1024 inspect_fw_phexdec("Kernel data offset",
1025 ntohl(hdr
->kernel_ofs
));
1026 inspect_fw_phexdec("Kernel data length",
1027 ntohl(hdr
->kernel_len
));
1029 inspect_fw_phexdef("Kernel load address",
1030 ntohl(hdr
->kernel_la
),
1031 layout
? layout
->kernel_la
: 0xffffffff);
1032 inspect_fw_phexdef("Kernel entry point",
1033 ntohl(hdr
->kernel_ep
),
1034 layout
? layout
->kernel_ep
: 0xffffffff);
1035 inspect_fw_phexdecdef("Rootfs data offset",
1036 ntohl(hdr
->rootfs_ofs
),
1037 layout
? layout
->rootfs_ofs
: 0xffffffff);
1039 inspect_fw_phex("Kernel load address",
1040 ntohl(hdr
->kernel_la
));
1041 inspect_fw_phex("Kernel entry point",
1042 ntohl(hdr
->kernel_ep
));
1043 inspect_fw_phexdec("Rootfs data offset",
1044 ntohl(hdr
->rootfs_ofs
));
1046 inspect_fw_phexdec("Rootfs data length",
1047 ntohl(hdr
->rootfs_len
));
1048 inspect_fw_phexdec("Boot loader data offset",
1049 ntohl(hdr
->boot_ofs
));
1050 inspect_fw_phexdec("Boot loader data length",
1051 ntohl(hdr
->boot_len
));
1052 inspect_fw_phexdec("Total firmware length",
1053 ntohl(hdr
->fw_length
));
1061 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
1062 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
1063 printf("Extracting kernel to \"%s\"...\n", filename
);
1064 fp
= fopen(filename
, "w");
1066 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
1067 ntohl(hdr
->kernel_len
), 1, fp
)) {
1068 ERR("error in fwrite(): %s", strerror(errno
));
1072 ERR("error in fopen(): %s", strerror(errno
));
1076 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
1077 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
1078 printf("Extracting rootfs to \"%s\"...\n", filename
);
1079 fp
= fopen(filename
, "w");
1081 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
1082 ntohl(hdr
->rootfs_len
), 1, fp
)) {
1083 ERR("error in fwrite(): %s", strerror(errno
));
1087 ERR("error in fopen(): %s", strerror(errno
));
1098 int main(int argc
, char *argv
[])
1100 int ret
= EXIT_FAILURE
;
1105 progname
= basename(argv
[0]);
1110 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsSjv:");
1116 sscanf(optarg
, "0x%x", &rootfs_align
);
1125 sscanf(optarg
, "0x%x", &kernel_ep
);
1131 opt_hw_rev
= optarg
;
1134 sscanf(optarg
, "0x%x", &kernel_la
);
1149 kernel_info
.file_name
= optarg
;
1152 rootfs_info
.file_name
= optarg
;
1155 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1167 inspect_info
.file_name
= optarg
;
1176 usage(EXIT_SUCCESS
);
1179 sscanf(optarg
, "0x%x", &reserved_space
);
1182 usage(EXIT_FAILURE
);
1187 ret
= check_options();
1191 if (!inspect_info
.file_name
)