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_TL_MR10U_V1 0x00100101
34 #define HWID_TL_MR13U_V1 0x00130101
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_WA801ND_V2 0x08010002
46 #define HWID_TL_WA901ND_V1 0x09010001
47 #define HWID_TL_WA901ND_V2 0x09010002
48 #define HWID_TL_WDR4900_V1 0x49000001
49 #define HWID_TL_WR703N_V1 0x07030101
50 #define HWID_TL_WR720N_V3 0x07200103
51 #define HWID_TL_WR741ND_V1 0x07410001
52 #define HWID_TL_WR741ND_V4 0x07410004
53 #define HWID_TL_WR740N_V1 0x07400001
54 #define HWID_TL_WR740N_V3 0x07400003
55 #define HWID_TL_WR743ND_V1 0x07430001
56 #define HWID_TL_WR743ND_V2 0x07430002
57 #define HWID_TL_WR841N_V1_5 0x08410002
58 #define HWID_TL_WR841ND_V3 0x08410003
59 #define HWID_TL_WR841ND_V5 0x08410005
60 #define HWID_TL_WR841ND_V7 0x08410007
61 #define HWID_TL_WR941ND_V2 0x09410002
62 #define HWID_TL_WR941ND_V4 0x09410004
63 #define HWID_TL_WR1043ND_V1 0x10430001
64 #define HWID_TL_WR1043ND_V2 0x10430002
65 #define HWID_TL_WR1041N_V2 0x10410002
66 #define HWID_TL_WR2543N_V1 0x25430001
71 char *file_name
; /* name of the file */
72 uint32_t file_size
; /* length of the file */
76 uint32_t version
; /* header version */
79 uint32_t hw_id
; /* hardware id */
80 uint32_t hw_rev
; /* hardware revision */
82 uint8_t md5sum1
[MD5SUM_LEN
];
84 uint8_t md5sum2
[MD5SUM_LEN
];
86 uint32_t kernel_la
; /* kernel load address */
87 uint32_t kernel_ep
; /* kernel entry point */
88 uint32_t fw_length
; /* total length of the firmware */
89 uint32_t kernel_ofs
; /* kernel data offset */
90 uint32_t kernel_len
; /* kernel data length */
91 uint32_t rootfs_ofs
; /* rootfs data offset */
92 uint32_t rootfs_len
; /* rootfs data length */
93 uint32_t boot_ofs
; /* bootloader data offset */
94 uint32_t boot_len
; /* bootloader data length */
99 } __attribute__ ((packed
));
101 struct flash_layout
{
120 static char *progname
;
121 static char *vendor
= "TP-LINK Technologies";
122 static char *version
= "ver. 1.0";
123 static char *fw_ver
= "0.0.0";
125 static char *board_id
;
126 static struct board_info
*board
;
127 static char *layout_id
;
128 static struct flash_layout
*layout
;
129 static char *opt_hw_id
;
130 static uint32_t hw_id
;
131 static char *opt_hw_rev
;
132 static uint32_t hw_rev
;
133 static int fw_ver_lo
;
134 static int fw_ver_mid
;
135 static int fw_ver_hi
;
136 static struct file_info kernel_info
;
137 static uint32_t kernel_la
= 0;
138 static uint32_t kernel_ep
= 0;
139 static uint32_t kernel_len
= 0;
140 static struct file_info rootfs_info
;
141 static uint32_t rootfs_ofs
= 0;
142 static uint32_t rootfs_align
;
143 static struct file_info boot_info
;
145 static int strip_padding
;
146 static int add_jffs2_eof
;
147 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
149 static struct file_info inspect_info
;
150 static int extract
= 0;
152 char md5salt_normal
[MD5SUM_LEN
] = {
153 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
154 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
157 char md5salt_boot
[MD5SUM_LEN
] = {
158 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
159 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
162 static struct flash_layout layouts
[] = {
165 .fw_max_len
= 0x3c0000,
166 .kernel_la
= 0x80060000,
167 .kernel_ep
= 0x80060000,
168 .rootfs_ofs
= 0x140000,
171 .fw_max_len
= 0x3c0000,
172 .kernel_la
= 0x80060000,
173 .kernel_ep
= 0x80060000,
174 .rootfs_ofs
= 0x100000,
177 .fw_max_len
= 0x7c0000,
178 .kernel_la
= 0x80060000,
179 .kernel_ep
= 0x80060000,
180 .rootfs_ofs
= 0x140000,
183 .fw_max_len
= 0x7c0000,
184 .kernel_la
= 0x80060000,
185 .kernel_ep
= 0x80060000,
186 .rootfs_ofs
= 0x100000,
189 .fw_max_len
= 0xf80000,
190 .kernel_la
= 0x00000000,
191 .kernel_ep
= 0xc0000000,
192 .rootfs_ofs
= 0x2a0000,
194 /* terminating entry */
198 static struct board_info boards
[] = {
201 .hw_id
= HWID_TL_MR10U_V1
,
203 .layout_id
= "4Mlzma",
206 .hw_id
= HWID_TL_MR13U_V1
,
208 .layout_id
= "4Mlzma",
211 .hw_id
= HWID_TL_MR3020_V1
,
213 .layout_id
= "4Mlzma",
216 .hw_id
= HWID_TL_MR3220_V1
,
221 .hw_id
= HWID_TL_MR3220_V2
,
223 .layout_id
= "4Mlzma",
226 .hw_id
= HWID_TL_MR3420_V1
,
231 .hw_id
= HWID_TL_MR3420_V2
,
233 .layout_id
= "4Mlzma",
236 .hw_id
= HWID_TL_WA701N_V1
,
241 .hw_id
= HWID_TL_WA7510N_V1
,
245 .id
= "TL-WA801NDv1",
246 .hw_id
= HWID_TL_WA801ND_V1
,
250 .id
= "TL-WA830REv1",
251 .hw_id
= HWID_TL_WA830RE_V1
,
255 .id
= "TL-WA830REv2",
256 .hw_id
= HWID_TL_WA830RE_V2
,
260 .id
= "TL-WA801NDv2",
261 .hw_id
= HWID_TL_WA801ND_V2
,
263 .layout_id
= "4Mlzma",
265 .id
= "TL-WA901NDv1",
266 .hw_id
= HWID_TL_WA901ND_V1
,
270 .id
= "TL-WA901NDv2",
271 .hw_id
= HWID_TL_WA901ND_V2
,
275 .id
= "TL-WDR4900v1",
276 .hw_id
= HWID_TL_WDR4900_V1
,
278 .layout_id
= "16Mppc",
280 .id
= "TL-WR741NDv1",
281 .hw_id
= HWID_TL_WR741ND_V1
,
285 .id
= "TL-WR741NDv4",
286 .hw_id
= HWID_TL_WR741ND_V4
,
288 .layout_id
= "4Mlzma",
291 .hw_id
= HWID_TL_WR740N_V1
,
296 .hw_id
= HWID_TL_WR740N_V3
,
300 .id
= "TL-WR743NDv1",
301 .hw_id
= HWID_TL_WR743ND_V1
,
305 .id
= "TL-WR743NDv2",
306 .hw_id
= HWID_TL_WR743ND_V2
,
308 .layout_id
= "4Mlzma",
310 .id
= "TL-WR841Nv1.5",
311 .hw_id
= HWID_TL_WR841N_V1_5
,
315 .id
= "TL-WR841NDv3",
316 .hw_id
= HWID_TL_WR841ND_V3
,
320 .id
= "TL-WR841NDv5",
321 .hw_id
= HWID_TL_WR841ND_V5
,
325 .id
= "TL-WR841NDv7",
326 .hw_id
= HWID_TL_WR841ND_V7
,
330 .id
= "TL-WR941NDv2",
331 .hw_id
= HWID_TL_WR941ND_V2
,
335 .id
= "TL-WR941NDv4",
336 .hw_id
= HWID_TL_WR941ND_V4
,
340 .id
= "TL-WR1041Nv2",
341 .hw_id
= HWID_TL_WR1041N_V2
,
343 .layout_id
= "4Mlzma",
345 .id
= "TL-WR1043NDv1",
346 .hw_id
= HWID_TL_WR1043ND_V1
,
350 .id
= "TL-WR1043NDv2",
351 .hw_id
= HWID_TL_WR1043ND_V2
,
353 .layout_id
= "8Mlzma",
355 .id
= "TL-WR2543Nv1",
356 .hw_id
= HWID_TL_WR2543N_V1
,
358 .layout_id
= "8Mlzma",
361 .hw_id
= HWID_TL_WR703N_V1
,
363 .layout_id
= "4Mlzma",
366 .hw_id
= HWID_TL_WR720N_V3
,
368 .layout_id
= "4Mlzma",
370 /* terminating entry */
377 #define ERR(fmt, ...) do { \
379 fprintf(stderr, "[%s] *** error: " fmt "\n", \
380 progname, ## __VA_ARGS__ ); \
383 #define ERRS(fmt, ...) do { \
386 fprintf(stderr, "[%s] *** error: " fmt "\n", \
387 progname, ## __VA_ARGS__, strerror(save)); \
390 #define DBG(fmt, ...) do { \
391 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
394 static struct board_info
*find_board(char *id
)
396 struct board_info
*ret
;
397 struct board_info
*board
;
400 for (board
= boards
; board
->id
!= NULL
; board
++){
401 if (strcasecmp(id
, board
->id
) == 0) {
410 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
412 struct board_info
*board
;
414 for (board
= boards
; board
->id
!= NULL
; board
++) {
415 if (hw_id
== board
->hw_id
)
422 static struct flash_layout
*find_layout(char *id
)
424 struct flash_layout
*ret
;
425 struct flash_layout
*l
;
428 for (l
= layouts
; l
->id
!= NULL
; l
++){
429 if (strcasecmp(id
, l
->id
) == 0) {
438 static void usage(int status
)
440 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
441 struct board_info
*board
;
443 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
447 " -B <board> create image for the board specified with <board>\n"
448 " -c use combined kernel image\n"
449 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
450 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
451 " -H <hwid> use hardware id specified with <hwid>\n"
452 " -W <hwrev> use hardware revision specified with <hwrev>\n"
453 " -F <id> use flash layout specified with <id>\n"
454 " -k <file> read kernel image from the file <file>\n"
455 " -r <file> read rootfs image from the file <file>\n"
456 " -a <align> align the rootfs start on an <align> bytes boundary\n"
457 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
458 " -o <file> write output to the file <file>\n"
459 " -s strip padding from the end of the image\n"
460 " -j add jffs2 end-of-filesystem markers\n"
461 " -N <vendor> set image vendor to <vendor>\n"
462 " -V <version> set image version to <version>\n"
463 " -v <version> set firmware version to <version>\n"
464 " -i <file> inspect given firmware file <file>\n"
465 " -x extract kernel and rootfs while inspecting (requires -i)\n"
466 " -h show this screen\n"
472 static int get_md5(char *data
, int size
, char *md5
)
477 MD5_Update(&ctx
, data
, size
);
478 MD5_Final(md5
, &ctx
);
481 static int get_file_stat(struct file_info
*fdata
)
486 if (fdata
->file_name
== NULL
)
489 res
= stat(fdata
->file_name
, &st
);
491 ERRS("stat failed on %s", fdata
->file_name
);
495 fdata
->file_size
= st
.st_size
;
499 static int read_to_buf(struct file_info
*fdata
, char *buf
)
502 int ret
= EXIT_FAILURE
;
504 f
= fopen(fdata
->file_name
, "r");
506 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
511 fread(buf
, fdata
->file_size
, 1, f
);
513 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
525 static int check_options(void)
529 if (inspect_info
.file_name
) {
530 ret
= get_file_stat(&inspect_info
);
535 } else if (extract
) {
536 ERR("no firmware for inspection specified");
540 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
541 ERR("either board or hardware id must be specified");
546 board
= find_board(board_id
);
548 ERR("unknown/unsupported board id \"%s\"", board_id
);
551 if (layout_id
== NULL
)
552 layout_id
= board
->layout_id
;
554 hw_id
= board
->hw_id
;
555 hw_rev
= board
->hw_rev
;
557 if (layout_id
== NULL
) {
558 ERR("flash layout is not specified");
561 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
564 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
569 layout
= find_layout(layout_id
);
570 if (layout
== NULL
) {
571 ERR("unknown flash layout \"%s\"", layout_id
);
576 kernel_la
= layout
->kernel_la
;
578 kernel_ep
= layout
->kernel_ep
;
580 rootfs_ofs
= layout
->rootfs_ofs
;
582 if (kernel_info
.file_name
== NULL
) {
583 ERR("no kernel image specified");
587 ret
= get_file_stat(&kernel_info
);
591 kernel_len
= kernel_info
.file_size
;
594 if (kernel_info
.file_size
>
595 layout
->fw_max_len
- sizeof(struct fw_header
)) {
596 ERR("kernel image is too big");
600 if (rootfs_info
.file_name
== NULL
) {
601 ERR("no rootfs image specified");
605 ret
= get_file_stat(&rootfs_info
);
610 kernel_len
+= sizeof(struct fw_header
);
611 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
612 kernel_len
-= sizeof(struct fw_header
);
614 DBG("kernel length aligned to %u", kernel_len
);
616 if (kernel_len
+ rootfs_info
.file_size
>
617 layout
->fw_max_len
- sizeof(struct fw_header
)) {
618 ERR("images are too big");
622 if (kernel_info
.file_size
>
623 rootfs_ofs
- sizeof(struct fw_header
)) {
624 ERR("kernel image is too big");
628 if (rootfs_info
.file_size
>
629 (layout
->fw_max_len
- rootfs_ofs
)) {
630 ERR("rootfs image is too big");
636 if (ofname
== NULL
) {
637 ERR("no output file specified");
641 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
643 ERR("invalid firmware version '%s'", fw_ver
);
650 static void fill_header(char *buf
, int len
)
652 struct fw_header
*hdr
= (struct fw_header
*)buf
;
654 memset(hdr
, 0, sizeof(struct fw_header
));
656 hdr
->version
= htonl(HEADER_VERSION_V1
);
657 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
658 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
659 hdr
->hw_id
= htonl(hw_id
);
660 hdr
->hw_rev
= htonl(hw_rev
);
662 if (boot_info
.file_size
== 0)
663 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
665 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
667 hdr
->kernel_la
= htonl(kernel_la
);
668 hdr
->kernel_ep
= htonl(kernel_ep
);
669 hdr
->fw_length
= htonl(layout
->fw_max_len
);
670 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
671 hdr
->kernel_len
= htonl(kernel_len
);
673 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
674 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
677 hdr
->ver_hi
= htons(fw_ver_hi
);
678 hdr
->ver_mid
= htons(fw_ver_mid
);
679 hdr
->ver_lo
= htons(fw_ver_lo
);
681 get_md5(buf
, len
, hdr
->md5sum1
);
684 static int pad_jffs2(char *buf
, int currlen
)
690 pad_mask
= (64 * 1024);
691 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
695 for (i
= 10; i
< 32; i
++) {
701 len
= ALIGN(len
, mask
);
703 for (i
= 10; i
< 32; i
++) {
705 if ((len
& (mask
- 1)) == 0)
709 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
710 buf
[len
+ i
] = jffs2_eof_mark
[i
];
712 len
+= sizeof(jffs2_eof_mark
);
718 static int write_fw(char *data
, int len
)
721 int ret
= EXIT_FAILURE
;
723 f
= fopen(ofname
, "w");
725 ERRS("could not open \"%s\" for writing", ofname
);
730 fwrite(data
, len
, 1, f
);
732 ERRS("unable to write output file");
736 DBG("firmware file \"%s\" completed", ofname
);
743 if (ret
!= EXIT_SUCCESS
) {
750 static int build_fw(void)
755 int ret
= EXIT_FAILURE
;
758 buflen
= layout
->fw_max_len
;
760 buf
= malloc(buflen
);
762 ERR("no memory for buffer\n");
766 memset(buf
, 0xff, buflen
);
767 p
= buf
+ sizeof(struct fw_header
);
768 ret
= read_to_buf(&kernel_info
, p
);
772 writelen
= sizeof(struct fw_header
) + kernel_len
;
778 p
= buf
+ rootfs_ofs
;
780 ret
= read_to_buf(&rootfs_info
, p
);
785 writelen
+= rootfs_info
.file_size
;
787 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
790 writelen
= pad_jffs2(buf
, writelen
);
796 fill_header(buf
, writelen
);
797 ret
= write_fw(buf
, writelen
);
809 /* Helper functions to inspect_fw() representing different output formats */
810 static inline void inspect_fw_pstr(char *label
, char *str
)
812 printf("%-23s: %s\n", label
, str
);
815 static inline void inspect_fw_phex(char *label
, uint32_t val
)
817 printf("%-23s: 0x%08x\n", label
, val
);
820 static inline void inspect_fw_phexpost(char *label
,
821 uint32_t val
, char *post
)
823 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
826 static inline void inspect_fw_phexdef(char *label
,
827 uint32_t val
, uint32_t defval
)
829 printf("%-23s: 0x%08x ", label
, val
);
832 printf("(== OpenWrt default)\n");
834 printf("(OpenWrt default: 0x%08x)\n", defval
);
837 static inline void inspect_fw_phexexp(char *label
,
838 uint32_t val
, uint32_t expval
)
840 printf("%-23s: 0x%08x ", label
, val
);
845 printf("(expected: 0x%08x)\n", expval
);
848 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
850 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
853 static inline void inspect_fw_phexdecdef(char *label
,
854 uint32_t val
, uint32_t defval
)
856 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
859 printf("(== OpenWrt default)\n");
861 printf("(OpenWrt default: 0x%08x)\n", defval
);
864 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
868 printf("%-23s:", label
);
869 for (i
=0; i
<MD5SUM_LEN
; i
++)
870 printf(" %02x", val
[i
]);
871 printf(" %s\n", text
);
874 static int inspect_fw(void)
877 struct fw_header
*hdr
;
878 uint8_t md5sum
[MD5SUM_LEN
];
879 struct board_info
*board
;
880 int ret
= EXIT_FAILURE
;
882 buf
= malloc(inspect_info
.file_size
);
884 ERR("no memory for buffer!\n");
888 ret
= read_to_buf(&inspect_info
, buf
);
891 hdr
= (struct fw_header
*)buf
;
893 inspect_fw_pstr("File name", inspect_info
.file_name
);
894 inspect_fw_phexdec("File size", inspect_info
.file_size
);
896 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
897 ERR("file does not seem to have V1 header!\n");
901 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
903 if (ntohl(hdr
->unk1
) != 0)
904 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
906 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
907 if (ntohl(hdr
->boot_len
) == 0)
908 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
910 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
911 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
913 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
914 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
915 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
917 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
919 if (ntohl(hdr
->unk2
) != 0)
920 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
921 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
922 "(purpose yet unknown, unchecked here)");
923 if (ntohl(hdr
->unk3
) != 0)
924 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
928 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
929 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
930 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
932 layout
= find_layout(board
->layout_id
);
933 inspect_fw_phexpost("Hardware ID",
934 ntohl(hdr
->hw_id
), board
->id
);
935 inspect_fw_phexexp("Hardware Revision",
936 ntohl(hdr
->hw_rev
), board
->hw_rev
);
938 inspect_fw_phexpost("Hardware ID",
939 ntohl(hdr
->hw_id
), "unknown");
940 inspect_fw_phex("Hardware Revision",
946 inspect_fw_phexdec("Kernel data offset",
947 ntohl(hdr
->kernel_ofs
));
948 inspect_fw_phexdec("Kernel data length",
949 ntohl(hdr
->kernel_len
));
951 inspect_fw_phexdef("Kernel load address",
952 ntohl(hdr
->kernel_la
),
953 layout
? layout
->kernel_la
: 0xffffffff);
954 inspect_fw_phexdef("Kernel entry point",
955 ntohl(hdr
->kernel_ep
),
956 layout
? layout
->kernel_ep
: 0xffffffff);
957 inspect_fw_phexdecdef("Rootfs data offset",
958 ntohl(hdr
->rootfs_ofs
),
959 layout
? layout
->rootfs_ofs
: 0xffffffff);
961 inspect_fw_phex("Kernel load address",
962 ntohl(hdr
->kernel_la
));
963 inspect_fw_phex("Kernel entry point",
964 ntohl(hdr
->kernel_ep
));
965 inspect_fw_phexdec("Rootfs data offset",
966 ntohl(hdr
->rootfs_ofs
));
968 inspect_fw_phexdec("Rootfs data length",
969 ntohl(hdr
->rootfs_len
));
970 inspect_fw_phexdec("Boot loader data offset",
971 ntohl(hdr
->boot_ofs
));
972 inspect_fw_phexdec("Boot loader data length",
973 ntohl(hdr
->boot_len
));
974 inspect_fw_phexdec("Total firmware length",
975 ntohl(hdr
->fw_length
));
983 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
984 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
985 printf("Extracting kernel to \"%s\"...\n", filename
);
986 fp
= fopen(filename
, "w");
988 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
989 ntohl(hdr
->kernel_len
), 1, fp
)) {
990 ERR("error in fwrite(): %s", strerror(errno
));
994 ERR("error in fopen(): %s", strerror(errno
));
998 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
999 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
1000 printf("Extracting rootfs to \"%s\"...\n", filename
);
1001 fp
= fopen(filename
, "w");
1003 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
1004 ntohl(hdr
->rootfs_len
), 1, fp
)) {
1005 ERR("error in fwrite(): %s", strerror(errno
));
1009 ERR("error in fopen(): %s", strerror(errno
));
1020 int main(int argc
, char *argv
[])
1022 int ret
= EXIT_FAILURE
;
1027 progname
= basename(argv
[0]);
1032 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
1038 sscanf(optarg
, "0x%x", &rootfs_align
);
1047 sscanf(optarg
, "0x%x", &kernel_ep
);
1053 opt_hw_rev
= optarg
;
1056 sscanf(optarg
, "0x%x", &kernel_la
);
1071 kernel_info
.file_name
= optarg
;
1074 rootfs_info
.file_name
= optarg
;
1077 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1086 inspect_info
.file_name
= optarg
;
1095 usage(EXIT_SUCCESS
);
1098 usage(EXIT_FAILURE
);
1103 ret
= check_options();
1107 if (!inspect_info
.file_name
)