c3d982768eb882b0fb16342caf437030e0212131
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_MR3020_V1 0x30200001
34 #define HWID_TL_MR3220_V1 0x32200001
35 #define HWID_TL_MR3220_V2 0x32200002
36 #define HWID_TL_MR3420_V1 0x34200001
37 #define HWID_TL_MR3420_V2 0x34200002
38 #define HWID_TL_WA701N_V1 0x07010001
39 #define HWID_TL_WA7510N_V1 0x75100001
40 #define HWID_TL_WA801ND_V1 0x08010001
41 #define HWID_TL_WA830RE_V1 0x08300010
42 #define HWID_TL_WA830RE_V2 0x08300002
43 #define HWID_TL_WA901ND_V1 0x09010001
44 #define HWID_TL_WA901ND_V2 0x09010002
45 #define HWID_TL_WDR4900_V1 0x49000001
46 #define HWID_TL_WR703N_V1 0x07030101
47 #define HWID_TL_WR720N_V3 0x07200103
48 #define HWID_TL_WR741ND_V1 0x07410001
49 #define HWID_TL_WR741ND_V4 0x07410004
50 #define HWID_TL_WR740N_V1 0x07400001
51 #define HWID_TL_WR740N_V3 0x07400003
52 #define HWID_TL_WR743ND_V1 0x07430001
53 #define HWID_TL_WR743ND_V2 0x07430002
54 #define HWID_TL_WR841N_V1_5 0x08410002
55 #define HWID_TL_WR841ND_V3 0x08410003
56 #define HWID_TL_WR841ND_V5 0x08410005
57 #define HWID_TL_WR841ND_V7 0x08410007
58 #define HWID_TL_WR941ND_V2 0x09410002
59 #define HWID_TL_WR941ND_V4 0x09410004
60 #define HWID_TL_WR1043ND_V1 0x10430001
61 #define HWID_TL_WR1041N_V2 0x10410002
62 #define HWID_TL_WR2543N_V1 0x25430001
67 char *file_name
; /* name of the file */
68 uint32_t file_size
; /* length of the file */
72 uint32_t version
; /* header version */
75 uint32_t hw_id
; /* hardware id */
76 uint32_t hw_rev
; /* hardware revision */
78 uint8_t md5sum1
[MD5SUM_LEN
];
80 uint8_t md5sum2
[MD5SUM_LEN
];
82 uint32_t kernel_la
; /* kernel load address */
83 uint32_t kernel_ep
; /* kernel entry point */
84 uint32_t fw_length
; /* total length of the firmware */
85 uint32_t kernel_ofs
; /* kernel data offset */
86 uint32_t kernel_len
; /* kernel data length */
87 uint32_t rootfs_ofs
; /* rootfs data offset */
88 uint32_t rootfs_len
; /* rootfs data length */
89 uint32_t boot_ofs
; /* bootloader data offset */
90 uint32_t boot_len
; /* bootloader data length */
95 } __attribute__ ((packed
));
116 static char *progname
;
117 static char *vendor
= "TP-LINK Technologies";
118 static char *version
= "ver. 1.0";
119 static char *fw_ver
= "0.0.0";
121 static char *board_id
;
122 static struct board_info
*board
;
123 static char *layout_id
;
124 static struct flash_layout
*layout
;
125 static char *opt_hw_id
;
126 static uint32_t hw_id
;
127 static char *opt_hw_rev
;
128 static uint32_t hw_rev
;
129 static int fw_ver_lo
;
130 static int fw_ver_mid
;
131 static int fw_ver_hi
;
132 static struct file_info kernel_info
;
133 static uint32_t kernel_la
= 0;
134 static uint32_t kernel_ep
= 0;
135 static uint32_t kernel_len
= 0;
136 static struct file_info rootfs_info
;
137 static uint32_t rootfs_ofs
= 0;
138 static uint32_t rootfs_align
;
139 static struct file_info boot_info
;
141 static int strip_padding
;
142 static int add_jffs2_eof
;
143 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
145 static struct file_info inspect_info
;
146 static int extract
= 0;
148 char md5salt_normal
[MD5SUM_LEN
] = {
149 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
150 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
153 char md5salt_boot
[MD5SUM_LEN
] = {
154 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
155 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
158 static struct flash_layout layouts
[] = {
161 .fw_max_len
= 0x3c0000,
162 .kernel_la
= 0x80060000,
163 .kernel_ep
= 0x80060000,
164 .rootfs_ofs
= 0x140000,
167 .fw_max_len
= 0x3c0000,
168 .kernel_la
= 0x80060000,
169 .kernel_ep
= 0x80060000,
170 .rootfs_ofs
= 0x100000,
173 .fw_max_len
= 0x7c0000,
174 .kernel_la
= 0x80060000,
175 .kernel_ep
= 0x80060000,
176 .rootfs_ofs
= 0x140000,
179 .fw_max_len
= 0x7c0000,
180 .kernel_la
= 0x80060000,
181 .kernel_ep
= 0x80060000,
182 .rootfs_ofs
= 0x100000,
185 .fw_max_len
= 0xf80000,
186 .kernel_la
= 0x00000000,
187 .kernel_ep
= 0xc0000000,
188 .rootfs_ofs
= 0x2a0000,
190 /* terminating entry */
194 static struct board_info boards
[] = {
197 .hw_id
= HWID_TL_MR3020_V1
,
199 .layout_id
= "4Mlzma",
202 .hw_id
= HWID_TL_MR3220_V1
,
207 .hw_id
= HWID_TL_MR3220_V2
,
209 .layout_id
= "4Mlzma",
212 .hw_id
= HWID_TL_MR3420_V1
,
217 .hw_id
= HWID_TL_MR3420_V2
,
219 .layout_id
= "4Mlzma",
222 .hw_id
= HWID_TL_WA701N_V1
,
227 .hw_id
= HWID_TL_WA7510N_V1
,
231 .id
= "TL-WA801NDv1",
232 .hw_id
= HWID_TL_WA801ND_V1
,
236 .id
= "TL-WA830REv1",
237 .hw_id
= HWID_TL_WA830RE_V1
,
241 .id
= "TL-WA830REv2",
242 .hw_id
= HWID_TL_WA830RE_V2
,
246 .id
= "TL-WA901NDv1",
247 .hw_id
= HWID_TL_WA901ND_V1
,
251 .id
= "TL-WA901NDv2",
252 .hw_id
= HWID_TL_WA901ND_V2
,
256 .id
= "TL-WDR4900v1",
257 .hw_id
= HWID_TL_WDR4900_V1
,
259 .layout_id
= "16Mppc",
261 .id
= "TL-WR741NDv1",
262 .hw_id
= HWID_TL_WR741ND_V1
,
266 .id
= "TL-WR741NDv4",
267 .hw_id
= HWID_TL_WR741ND_V4
,
269 .layout_id
= "4Mlzma",
272 .hw_id
= HWID_TL_WR740N_V1
,
277 .hw_id
= HWID_TL_WR740N_V3
,
281 .id
= "TL-WR743NDv1",
282 .hw_id
= HWID_TL_WR743ND_V1
,
286 .id
= "TL-WR743NDv2",
287 .hw_id
= HWID_TL_WR743ND_V2
,
289 .layout_id
= "4Mlzma",
291 .id
= "TL-WR841Nv1.5",
292 .hw_id
= HWID_TL_WR841N_V1_5
,
296 .id
= "TL-WR841NDv3",
297 .hw_id
= HWID_TL_WR841ND_V3
,
301 .id
= "TL-WR841NDv5",
302 .hw_id
= HWID_TL_WR841ND_V5
,
306 .id
= "TL-WR841NDv7",
307 .hw_id
= HWID_TL_WR841ND_V7
,
311 .id
= "TL-WR941NDv2",
312 .hw_id
= HWID_TL_WR941ND_V2
,
316 .id
= "TL-WR941NDv4",
317 .hw_id
= HWID_TL_WR941ND_V4
,
321 .id
= "TL-WR1041Nv2",
322 .hw_id
= HWID_TL_WR1041N_V2
,
324 .layout_id
= "4Mlzma",
326 .id
= "TL-WR1043NDv1",
327 .hw_id
= HWID_TL_WR1043ND_V1
,
331 .id
= "TL-WR2543Nv1",
332 .hw_id
= HWID_TL_WR2543N_V1
,
334 .layout_id
= "8Mlzma",
337 .hw_id
= HWID_TL_WR703N_V1
,
339 .layout_id
= "4Mlzma",
342 .hw_id
= HWID_TL_WR720N_V3
,
344 .layout_id
= "4Mlzma",
346 /* terminating entry */
353 #define ERR(fmt, ...) do { \
355 fprintf(stderr, "[%s] *** error: " fmt "\n", \
356 progname, ## __VA_ARGS__ ); \
359 #define ERRS(fmt, ...) do { \
362 fprintf(stderr, "[%s] *** error: " fmt "\n", \
363 progname, ## __VA_ARGS__, strerror(save)); \
366 #define DBG(fmt, ...) do { \
367 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
370 static struct board_info
*find_board(char *id
)
372 struct board_info
*ret
;
373 struct board_info
*board
;
376 for (board
= boards
; board
->id
!= NULL
; board
++){
377 if (strcasecmp(id
, board
->id
) == 0) {
386 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
388 struct board_info
*board
;
390 for (board
= boards
; board
->id
!= NULL
; board
++) {
391 if (hw_id
== board
->hw_id
)
398 static struct flash_layout
*find_layout(char *id
)
400 struct flash_layout
*ret
;
401 struct flash_layout
*l
;
404 for (l
= layouts
; l
->id
!= NULL
; l
++){
405 if (strcasecmp(id
, l
->id
) == 0) {
414 static void usage(int status
)
416 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
417 struct board_info
*board
;
419 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
423 " -B <board> create image for the board specified with <board>\n"
424 " -c use combined kernel image\n"
425 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
426 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
427 " -H <hwid> use hardware id specified with <hwid>\n"
428 " -F <id> use flash layout specified with <id>\n"
429 " -k <file> read kernel image from the file <file>\n"
430 " -r <file> read rootfs image from the file <file>\n"
431 " -a <align> align the rootfs start on an <align> bytes boundary\n"
432 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
433 " -o <file> write output to the file <file>\n"
434 " -s strip padding from the end of the image\n"
435 " -j add jffs2 end-of-filesystem markers\n"
436 " -N <vendor> set image vendor to <vendor>\n"
437 " -V <version> set image version to <version>\n"
438 " -v <version> set firmware version to <version>\n"
439 " -i <file> inspect given firmware file <file>\n"
440 " -x extract kernel and rootfs while inspecting (requires -i)\n"
441 " -h show this screen\n"
447 static int get_md5(char *data
, int size
, char *md5
)
452 MD5_Update(&ctx
, data
, size
);
453 MD5_Final(md5
, &ctx
);
456 static int get_file_stat(struct file_info
*fdata
)
461 if (fdata
->file_name
== NULL
)
464 res
= stat(fdata
->file_name
, &st
);
466 ERRS("stat failed on %s", fdata
->file_name
);
470 fdata
->file_size
= st
.st_size
;
474 static int read_to_buf(struct file_info
*fdata
, char *buf
)
477 int ret
= EXIT_FAILURE
;
479 f
= fopen(fdata
->file_name
, "r");
481 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
486 fread(buf
, fdata
->file_size
, 1, f
);
488 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
500 static int check_options(void)
504 if (inspect_info
.file_name
) {
505 ret
= get_file_stat(&inspect_info
);
510 } else if (extract
) {
511 ERR("no firmware for inspection specified");
515 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
516 ERR("either board or hardware id must be specified");
521 board
= find_board(board_id
);
523 ERR("unknown/unsupported board id \"%s\"", board_id
);
526 if (layout_id
== NULL
)
527 layout_id
= board
->layout_id
;
529 hw_id
= board
->hw_id
;
530 hw_rev
= board
->hw_rev
;
532 if (layout_id
== NULL
) {
533 ERR("flash layout is not specified");
536 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
539 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
544 layout
= find_layout(layout_id
);
545 if (layout
== NULL
) {
546 ERR("unknown flash layout \"%s\"", layout_id
);
551 kernel_la
= layout
->kernel_la
;
553 kernel_ep
= layout
->kernel_ep
;
555 rootfs_ofs
= layout
->rootfs_ofs
;
557 if (kernel_info
.file_name
== NULL
) {
558 ERR("no kernel image specified");
562 ret
= get_file_stat(&kernel_info
);
566 kernel_len
= kernel_info
.file_size
;
569 if (kernel_info
.file_size
>
570 layout
->fw_max_len
- sizeof(struct fw_header
)) {
571 ERR("kernel image is too big");
575 if (rootfs_info
.file_name
== NULL
) {
576 ERR("no rootfs image specified");
580 ret
= get_file_stat(&rootfs_info
);
585 kernel_len
+= sizeof(struct fw_header
);
586 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
587 kernel_len
-= sizeof(struct fw_header
);
589 DBG("kernel length aligned to %u", kernel_len
);
591 if (kernel_len
+ rootfs_info
.file_size
>
592 layout
->fw_max_len
- sizeof(struct fw_header
)) {
593 ERR("images are too big");
597 if (kernel_info
.file_size
>
598 rootfs_ofs
- sizeof(struct fw_header
)) {
599 ERR("kernel image is too big");
603 if (rootfs_info
.file_size
>
604 (layout
->fw_max_len
- rootfs_ofs
)) {
605 ERR("rootfs image is too big");
611 if (ofname
== NULL
) {
612 ERR("no output file specified");
616 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
618 ERR("invalid firmware version '%s'", fw_ver
);
625 static void fill_header(char *buf
, int len
)
627 struct fw_header
*hdr
= (struct fw_header
*)buf
;
629 memset(hdr
, 0, sizeof(struct fw_header
));
631 hdr
->version
= htonl(HEADER_VERSION_V1
);
632 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
633 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
634 hdr
->hw_id
= htonl(hw_id
);
635 hdr
->hw_rev
= htonl(hw_rev
);
637 if (boot_info
.file_size
== 0)
638 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
640 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
642 hdr
->kernel_la
= htonl(kernel_la
);
643 hdr
->kernel_ep
= htonl(kernel_ep
);
644 hdr
->fw_length
= htonl(layout
->fw_max_len
);
645 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
646 hdr
->kernel_len
= htonl(kernel_len
);
648 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
649 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
652 hdr
->ver_hi
= htons(fw_ver_hi
);
653 hdr
->ver_mid
= htons(fw_ver_mid
);
654 hdr
->ver_lo
= htons(fw_ver_lo
);
656 get_md5(buf
, len
, hdr
->md5sum1
);
659 static int pad_jffs2(char *buf
, int currlen
)
665 pad_mask
= (64 * 1024);
666 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
670 for (i
= 10; i
< 32; i
++) {
676 len
= ALIGN(len
, mask
);
678 for (i
= 10; i
< 32; i
++) {
680 if ((len
& (mask
- 1)) == 0)
684 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
685 buf
[len
+ i
] = jffs2_eof_mark
[i
];
687 len
+= sizeof(jffs2_eof_mark
);
693 static int write_fw(char *data
, int len
)
696 int ret
= EXIT_FAILURE
;
698 f
= fopen(ofname
, "w");
700 ERRS("could not open \"%s\" for writing", ofname
);
705 fwrite(data
, len
, 1, f
);
707 ERRS("unable to write output file");
711 DBG("firmware file \"%s\" completed", ofname
);
718 if (ret
!= EXIT_SUCCESS
) {
725 static int build_fw(void)
730 int ret
= EXIT_FAILURE
;
733 buflen
= layout
->fw_max_len
;
735 buf
= malloc(buflen
);
737 ERR("no memory for buffer\n");
741 memset(buf
, 0xff, buflen
);
742 p
= buf
+ sizeof(struct fw_header
);
743 ret
= read_to_buf(&kernel_info
, p
);
747 writelen
= sizeof(struct fw_header
) + kernel_len
;
753 p
= buf
+ rootfs_ofs
;
755 ret
= read_to_buf(&rootfs_info
, p
);
760 writelen
+= rootfs_info
.file_size
;
762 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
765 writelen
= pad_jffs2(buf
, writelen
);
771 fill_header(buf
, writelen
);
772 ret
= write_fw(buf
, writelen
);
784 /* Helper functions to inspect_fw() representing different output formats */
785 static inline void inspect_fw_pstr(char *label
, char *str
)
787 printf("%-23s: %s\n", label
, str
);
790 static inline void inspect_fw_phex(char *label
, uint32_t val
)
792 printf("%-23s: 0x%08x\n", label
, val
);
795 static inline void inspect_fw_phexpost(char *label
,
796 uint32_t val
, char *post
)
798 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
801 static inline void inspect_fw_phexdef(char *label
,
802 uint32_t val
, uint32_t defval
)
804 printf("%-23s: 0x%08x ", label
, val
);
807 printf("(== OpenWrt default)\n");
809 printf("(OpenWrt default: 0x%08x)\n", defval
);
812 static inline void inspect_fw_phexexp(char *label
,
813 uint32_t val
, uint32_t expval
)
815 printf("%-23s: 0x%08x ", label
, val
);
820 printf("(expected: 0x%08x)\n", expval
);
823 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
825 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
828 static inline void inspect_fw_phexdecdef(char *label
,
829 uint32_t val
, uint32_t defval
)
831 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
834 printf("(== OpenWrt default)\n");
836 printf("(OpenWrt default: 0x%08x)\n", defval
);
839 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
843 printf("%-23s:", label
);
844 for (i
=0; i
<MD5SUM_LEN
; i
++)
845 printf(" %02x", val
[i
]);
846 printf(" %s\n", text
);
849 static int inspect_fw(void)
852 struct fw_header
*hdr
;
853 uint8_t md5sum
[MD5SUM_LEN
];
854 struct board_info
*board
;
855 int ret
= EXIT_FAILURE
;
857 buf
= malloc(inspect_info
.file_size
);
859 ERR("no memory for buffer!\n");
863 ret
= read_to_buf(&inspect_info
, buf
);
866 hdr
= (struct fw_header
*)buf
;
868 inspect_fw_pstr("File name", inspect_info
.file_name
);
869 inspect_fw_phexdec("File size", inspect_info
.file_size
);
871 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
872 ERR("file does not seem to have V1 header!\n");
876 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
878 if (ntohl(hdr
->unk1
) != 0)
879 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
881 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
882 if (ntohl(hdr
->boot_len
) == 0)
883 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
885 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
886 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
888 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
889 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
890 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
892 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
894 if (ntohl(hdr
->unk2
) != 0)
895 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
896 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
897 "(purpose yet unknown, unchecked here)");
898 if (ntohl(hdr
->unk3
) != 0)
899 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
903 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
904 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
905 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
907 layout
= find_layout(board
->layout_id
);
908 inspect_fw_phexpost("Hardware ID",
909 ntohl(hdr
->hw_id
), board
->id
);
910 inspect_fw_phexexp("Hardware Revision",
911 ntohl(hdr
->hw_rev
), board
->hw_rev
);
913 inspect_fw_phexpost("Hardware ID",
914 ntohl(hdr
->hw_id
), "unknown");
915 inspect_fw_phex("Hardware Revision",
921 inspect_fw_phexdec("Kernel data offset",
922 ntohl(hdr
->kernel_ofs
));
923 inspect_fw_phexdec("Kernel data length",
924 ntohl(hdr
->kernel_len
));
926 inspect_fw_phexdef("Kernel load address",
927 ntohl(hdr
->kernel_la
),
928 layout
? layout
->kernel_la
: 0xffffffff);
929 inspect_fw_phexdef("Kernel entry point",
930 ntohl(hdr
->kernel_ep
),
931 layout
? layout
->kernel_ep
: 0xffffffff);
932 inspect_fw_phexdecdef("Rootfs data offset",
933 ntohl(hdr
->rootfs_ofs
),
934 layout
? layout
->rootfs_ofs
: 0xffffffff);
936 inspect_fw_phex("Kernel load address",
937 ntohl(hdr
->kernel_la
));
938 inspect_fw_phex("Kernel entry point",
939 ntohl(hdr
->kernel_ep
));
940 inspect_fw_phexdec("Rootfs data offset",
941 ntohl(hdr
->rootfs_ofs
));
943 inspect_fw_phexdec("Rootfs data length",
944 ntohl(hdr
->rootfs_len
));
945 inspect_fw_phexdec("Boot loader data offset",
946 ntohl(hdr
->boot_ofs
));
947 inspect_fw_phexdec("Boot loader data length",
948 ntohl(hdr
->boot_len
));
949 inspect_fw_phexdec("Total firmware length",
950 ntohl(hdr
->fw_length
));
958 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
959 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
960 printf("Extracting kernel to \"%s\"...\n", filename
);
961 fp
= fopen(filename
, "w");
963 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
964 ntohl(hdr
->kernel_len
), 1, fp
)) {
965 ERR("error in fwrite(): %s", strerror(errno
));
969 ERR("error in fopen(): %s", strerror(errno
));
973 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
974 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
975 printf("Extracting rootfs to \"%s\"...\n", filename
);
976 fp
= fopen(filename
, "w");
978 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
979 ntohl(hdr
->rootfs_len
), 1, fp
)) {
980 ERR("error in fwrite(): %s", strerror(errno
));
984 ERR("error in fopen(): %s", strerror(errno
));
995 int main(int argc
, char *argv
[])
997 int ret
= EXIT_FAILURE
;
1002 progname
= basename(argv
[0]);
1007 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
1013 sscanf(optarg
, "0x%x", &rootfs_align
);
1022 sscanf(optarg
, "0x%x", &kernel_ep
);
1028 opt_hw_rev
= optarg
;
1031 sscanf(optarg
, "0x%x", &kernel_la
);
1046 kernel_info
.file_name
= optarg
;
1049 rootfs_info
.file_name
= optarg
;
1052 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1061 inspect_info
.file_name
= optarg
;
1070 usage(EXIT_SUCCESS
);
1073 usage(EXIT_FAILURE
);
1078 ret
= check_options();
1082 if (!inspect_info
.file_name
)