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_WA701N_V1 0x07010001
38 #define HWID_TL_WA7510N_V1 0x75100001
39 #define HWID_TL_WA801ND_V1 0x08010001
40 #define HWID_TL_WA830RE_V1 0x08300010
41 #define HWID_TL_WA830RE_V2 0x08300002
42 #define HWID_TL_WA901ND_V1 0x09010001
43 #define HWID_TL_WA901ND_V2 0x09010002
44 #define HWID_TL_WDR4900_V1 0x49000001
45 #define HWID_TL_WR703N_V1 0x07030101
46 #define HWID_TL_WR741ND_V1 0x07410001
47 #define HWID_TL_WR741ND_V4 0x07410004
48 #define HWID_TL_WR740N_V1 0x07400001
49 #define HWID_TL_WR740N_V3 0x07400003
50 #define HWID_TL_WR743ND_V1 0x07430001
51 #define HWID_TL_WR743ND_V2 0x07430002
52 #define HWID_TL_WR841N_V1_5 0x08410002
53 #define HWID_TL_WR841ND_V3 0x08410003
54 #define HWID_TL_WR841ND_V5 0x08410005
55 #define HWID_TL_WR841ND_V7 0x08410007
56 #define HWID_TL_WR941ND_V2 0x09410002
57 #define HWID_TL_WR941ND_V4 0x09410004
58 #define HWID_TL_WR1043ND_V1 0x10430001
59 #define HWID_TL_WR1041N_V2 0x10410002
60 #define HWID_TL_WR2543N_V1 0x25430001
65 char *file_name
; /* name of the file */
66 uint32_t file_size
; /* length of the file */
70 uint32_t version
; /* header version */
73 uint32_t hw_id
; /* hardware id */
74 uint32_t hw_rev
; /* hardware revision */
76 uint8_t md5sum1
[MD5SUM_LEN
];
78 uint8_t md5sum2
[MD5SUM_LEN
];
80 uint32_t kernel_la
; /* kernel load address */
81 uint32_t kernel_ep
; /* kernel entry point */
82 uint32_t fw_length
; /* total length of the firmware */
83 uint32_t kernel_ofs
; /* kernel data offset */
84 uint32_t kernel_len
; /* kernel data length */
85 uint32_t rootfs_ofs
; /* rootfs data offset */
86 uint32_t rootfs_len
; /* rootfs data length */
87 uint32_t boot_ofs
; /* bootloader data offset */
88 uint32_t boot_len
; /* bootloader data length */
93 } __attribute__ ((packed
));
114 static char *progname
;
115 static char *vendor
= "TP-LINK Technologies";
116 static char *version
= "ver. 1.0";
117 static char *fw_ver
= "0.0.0";
119 static char *board_id
;
120 static struct board_info
*board
;
121 static char *layout_id
;
122 static struct flash_layout
*layout
;
123 static char *opt_hw_id
;
124 static uint32_t hw_id
;
125 static char *opt_hw_rev
;
126 static uint32_t hw_rev
;
127 static int fw_ver_lo
;
128 static int fw_ver_mid
;
129 static int fw_ver_hi
;
130 static struct file_info kernel_info
;
131 static uint32_t kernel_la
= 0;
132 static uint32_t kernel_ep
= 0;
133 static uint32_t kernel_len
= 0;
134 static struct file_info rootfs_info
;
135 static uint32_t rootfs_ofs
= 0;
136 static uint32_t rootfs_align
;
137 static struct file_info boot_info
;
139 static int strip_padding
;
140 static int add_jffs2_eof
;
141 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
143 static struct file_info inspect_info
;
144 static int extract
= 0;
146 char md5salt_normal
[MD5SUM_LEN
] = {
147 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
148 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
151 char md5salt_boot
[MD5SUM_LEN
] = {
152 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
153 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
156 static struct flash_layout layouts
[] = {
159 .fw_max_len
= 0x3c0000,
160 .kernel_la
= 0x80060000,
161 .kernel_ep
= 0x80060000,
162 .rootfs_ofs
= 0x140000,
165 .fw_max_len
= 0x3c0000,
166 .kernel_la
= 0x80060000,
167 .kernel_ep
= 0x80060000,
168 .rootfs_ofs
= 0x100000,
171 .fw_max_len
= 0x7c0000,
172 .kernel_la
= 0x80060000,
173 .kernel_ep
= 0x80060000,
174 .rootfs_ofs
= 0x140000,
177 .fw_max_len
= 0x7c0000,
178 .kernel_la
= 0x80060000,
179 .kernel_ep
= 0x80060000,
180 .rootfs_ofs
= 0x100000,
183 .fw_max_len
= 0xf80000,
184 .kernel_la
= 0x00000000,
185 .kernel_ep
= 0xc0000000,
186 .rootfs_ofs
= 0x2a0000,
188 /* terminating entry */
192 static struct board_info boards
[] = {
195 .hw_id
= HWID_TL_MR3020_V1
,
197 .layout_id
= "4Mlzma",
200 .hw_id
= HWID_TL_MR3220_V1
,
205 .hw_id
= HWID_TL_MR3220_V2
,
207 .layout_id
= "4Mlzma",
210 .hw_id
= HWID_TL_MR3420_V1
,
215 .hw_id
= HWID_TL_WA701N_V1
,
220 .hw_id
= HWID_TL_WA7510N_V1
,
224 .id
= "TL-WA801NDv1",
225 .hw_id
= HWID_TL_WA801ND_V1
,
229 .id
= "TL-WA830REv1",
230 .hw_id
= HWID_TL_WA830RE_V1
,
234 .id
= "TL-WA830REv2",
235 .hw_id
= HWID_TL_WA830RE_V2
,
239 .id
= "TL-WA901NDv1",
240 .hw_id
= HWID_TL_WA901ND_V1
,
244 .id
= "TL-WA901NDv2",
245 .hw_id
= HWID_TL_WA901ND_V2
,
249 .id
= "TL-WDR4900v1",
250 .hw_id
= HWID_TL_WDR4900_V1
,
252 .layout_id
= "16Mppc",
254 .id
= "TL-WR741NDv1",
255 .hw_id
= HWID_TL_WR741ND_V1
,
259 .id
= "TL-WR741NDv4",
260 .hw_id
= HWID_TL_WR741ND_V4
,
262 .layout_id
= "4Mlzma",
265 .hw_id
= HWID_TL_WR740N_V1
,
270 .hw_id
= HWID_TL_WR740N_V3
,
274 .id
= "TL-WR743NDv1",
275 .hw_id
= HWID_TL_WR743ND_V1
,
279 .id
= "TL-WR743NDv2",
280 .hw_id
= HWID_TL_WR743ND_V2
,
282 .layout_id
= "4Mlzma",
284 .id
= "TL-WR841Nv1.5",
285 .hw_id
= HWID_TL_WR841N_V1_5
,
289 .id
= "TL-WR841NDv3",
290 .hw_id
= HWID_TL_WR841ND_V3
,
294 .id
= "TL-WR841NDv5",
295 .hw_id
= HWID_TL_WR841ND_V5
,
299 .id
= "TL-WR841NDv7",
300 .hw_id
= HWID_TL_WR841ND_V7
,
304 .id
= "TL-WR941NDv2",
305 .hw_id
= HWID_TL_WR941ND_V2
,
309 .id
= "TL-WR941NDv4",
310 .hw_id
= HWID_TL_WR941ND_V4
,
314 .id
= "TL-WR1041Nv2",
315 .hw_id
= HWID_TL_WR1041N_V2
,
317 .layout_id
= "4Mlzma",
319 .id
= "TL-WR1043NDv1",
320 .hw_id
= HWID_TL_WR1043ND_V1
,
324 .id
= "TL-WR2543Nv1",
325 .hw_id
= HWID_TL_WR2543N_V1
,
327 .layout_id
= "8Mlzma",
330 .hw_id
= HWID_TL_WR703N_V1
,
332 .layout_id
= "4Mlzma",
334 /* terminating entry */
341 #define ERR(fmt, ...) do { \
343 fprintf(stderr, "[%s] *** error: " fmt "\n", \
344 progname, ## __VA_ARGS__ ); \
347 #define ERRS(fmt, ...) do { \
350 fprintf(stderr, "[%s] *** error: " fmt "\n", \
351 progname, ## __VA_ARGS__, strerror(save)); \
354 #define DBG(fmt, ...) do { \
355 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
358 static struct board_info
*find_board(char *id
)
360 struct board_info
*ret
;
361 struct board_info
*board
;
364 for (board
= boards
; board
->id
!= NULL
; board
++){
365 if (strcasecmp(id
, board
->id
) == 0) {
374 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
376 struct board_info
*board
;
378 for (board
= boards
; board
->id
!= NULL
; board
++) {
379 if (hw_id
== board
->hw_id
)
386 static struct flash_layout
*find_layout(char *id
)
388 struct flash_layout
*ret
;
389 struct flash_layout
*l
;
392 for (l
= layouts
; l
->id
!= NULL
; l
++){
393 if (strcasecmp(id
, l
->id
) == 0) {
402 static void usage(int status
)
404 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
405 struct board_info
*board
;
407 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
411 " -B <board> create image for the board specified with <board>\n"
412 " -c use combined kernel image\n"
413 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
414 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
415 " -H <hwid> use hardware id specified with <hwid>\n"
416 " -F <id> use flash layout specified with <id>\n"
417 " -k <file> read kernel image from the file <file>\n"
418 " -r <file> read rootfs image from the file <file>\n"
419 " -a <align> align the rootfs start on an <align> bytes boundary\n"
420 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
421 " -o <file> write output to the file <file>\n"
422 " -s strip padding from the end of the image\n"
423 " -j add jffs2 end-of-filesystem markers\n"
424 " -N <vendor> set image vendor to <vendor>\n"
425 " -V <version> set image version to <version>\n"
426 " -v <version> set firmware version to <version>\n"
427 " -i <file> inspect given firmware file <file>\n"
428 " -x extract kernel and rootfs while inspecting (requires -i)\n"
429 " -h show this screen\n"
435 static int get_md5(char *data
, int size
, char *md5
)
440 MD5_Update(&ctx
, data
, size
);
441 MD5_Final(md5
, &ctx
);
444 static int get_file_stat(struct file_info
*fdata
)
449 if (fdata
->file_name
== NULL
)
452 res
= stat(fdata
->file_name
, &st
);
454 ERRS("stat failed on %s", fdata
->file_name
);
458 fdata
->file_size
= st
.st_size
;
462 static int read_to_buf(struct file_info
*fdata
, char *buf
)
465 int ret
= EXIT_FAILURE
;
467 f
= fopen(fdata
->file_name
, "r");
469 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
474 fread(buf
, fdata
->file_size
, 1, f
);
476 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
488 static int check_options(void)
492 if (inspect_info
.file_name
) {
493 ret
= get_file_stat(&inspect_info
);
498 } else if (extract
) {
499 ERR("no firmware for inspection specified");
503 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
504 ERR("either board or hardware id must be specified");
509 board
= find_board(board_id
);
511 ERR("unknown/unsupported board id \"%s\"", board_id
);
514 if (layout_id
== NULL
)
515 layout_id
= board
->layout_id
;
517 hw_id
= board
->hw_id
;
518 hw_rev
= board
->hw_rev
;
520 if (layout_id
== NULL
) {
521 ERR("flash layout is not specified");
524 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
527 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
532 layout
= find_layout(layout_id
);
533 if (layout
== NULL
) {
534 ERR("unknown flash layout \"%s\"", layout_id
);
539 kernel_la
= layout
->kernel_la
;
541 kernel_ep
= layout
->kernel_ep
;
543 rootfs_ofs
= layout
->rootfs_ofs
;
545 if (kernel_info
.file_name
== NULL
) {
546 ERR("no kernel image specified");
550 ret
= get_file_stat(&kernel_info
);
554 kernel_len
= kernel_info
.file_size
;
557 if (kernel_info
.file_size
>
558 layout
->fw_max_len
- sizeof(struct fw_header
)) {
559 ERR("kernel image is too big");
563 if (rootfs_info
.file_name
== NULL
) {
564 ERR("no rootfs image specified");
568 ret
= get_file_stat(&rootfs_info
);
573 kernel_len
+= sizeof(struct fw_header
);
574 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
575 kernel_len
-= sizeof(struct fw_header
);
577 DBG("kernel length aligned to %u", kernel_len
);
579 if (kernel_len
+ rootfs_info
.file_size
>
580 layout
->fw_max_len
- sizeof(struct fw_header
)) {
581 ERR("images are too big");
585 if (kernel_info
.file_size
>
586 rootfs_ofs
- sizeof(struct fw_header
)) {
587 ERR("kernel image is too big");
591 if (rootfs_info
.file_size
>
592 (layout
->fw_max_len
- rootfs_ofs
)) {
593 ERR("rootfs image is too big");
599 if (ofname
== NULL
) {
600 ERR("no output file specified");
604 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
606 ERR("invalid firmware version '%s'", fw_ver
);
613 static void fill_header(char *buf
, int len
)
615 struct fw_header
*hdr
= (struct fw_header
*)buf
;
617 memset(hdr
, 0, sizeof(struct fw_header
));
619 hdr
->version
= htonl(HEADER_VERSION_V1
);
620 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
621 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
622 hdr
->hw_id
= htonl(hw_id
);
623 hdr
->hw_rev
= htonl(hw_rev
);
625 if (boot_info
.file_size
== 0)
626 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
628 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
630 hdr
->kernel_la
= htonl(kernel_la
);
631 hdr
->kernel_ep
= htonl(kernel_ep
);
632 hdr
->fw_length
= htonl(layout
->fw_max_len
);
633 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
634 hdr
->kernel_len
= htonl(kernel_len
);
636 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
637 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
640 hdr
->ver_hi
= htons(fw_ver_hi
);
641 hdr
->ver_mid
= htons(fw_ver_mid
);
642 hdr
->ver_lo
= htons(fw_ver_lo
);
644 get_md5(buf
, len
, hdr
->md5sum1
);
647 static int pad_jffs2(char *buf
, int currlen
)
653 pad_mask
= (64 * 1024);
654 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
658 for (i
= 10; i
< 32; i
++) {
664 len
= ALIGN(len
, mask
);
666 for (i
= 10; i
< 32; i
++) {
668 if ((len
& (mask
- 1)) == 0)
672 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
673 buf
[len
+ i
] = jffs2_eof_mark
[i
];
675 len
+= sizeof(jffs2_eof_mark
);
681 static int write_fw(char *data
, int len
)
684 int ret
= EXIT_FAILURE
;
686 f
= fopen(ofname
, "w");
688 ERRS("could not open \"%s\" for writing", ofname
);
693 fwrite(data
, len
, 1, f
);
695 ERRS("unable to write output file");
699 DBG("firmware file \"%s\" completed", ofname
);
706 if (ret
!= EXIT_SUCCESS
) {
713 static int build_fw(void)
718 int ret
= EXIT_FAILURE
;
721 buflen
= layout
->fw_max_len
;
723 buf
= malloc(buflen
);
725 ERR("no memory for buffer\n");
729 memset(buf
, 0xff, buflen
);
730 p
= buf
+ sizeof(struct fw_header
);
731 ret
= read_to_buf(&kernel_info
, p
);
735 writelen
= sizeof(struct fw_header
) + kernel_len
;
741 p
= buf
+ rootfs_ofs
;
743 ret
= read_to_buf(&rootfs_info
, p
);
748 writelen
+= rootfs_info
.file_size
;
750 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
753 writelen
= pad_jffs2(buf
, writelen
);
759 fill_header(buf
, writelen
);
760 ret
= write_fw(buf
, writelen
);
772 /* Helper functions to inspect_fw() representing different output formats */
773 static inline void inspect_fw_pstr(char *label
, char *str
)
775 printf("%-23s: %s\n", label
, str
);
778 static inline void inspect_fw_phex(char *label
, uint32_t val
)
780 printf("%-23s: 0x%08x\n", label
, val
);
783 static inline void inspect_fw_phexpost(char *label
,
784 uint32_t val
, char *post
)
786 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
789 static inline void inspect_fw_phexdef(char *label
,
790 uint32_t val
, uint32_t defval
)
792 printf("%-23s: 0x%08x ", label
, val
);
795 printf("(== OpenWrt default)\n");
797 printf("(OpenWrt default: 0x%08x)\n", defval
);
800 static inline void inspect_fw_phexexp(char *label
,
801 uint32_t val
, uint32_t expval
)
803 printf("%-23s: 0x%08x ", label
, val
);
808 printf("(expected: 0x%08x)\n", expval
);
811 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
813 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
816 static inline void inspect_fw_phexdecdef(char *label
,
817 uint32_t val
, uint32_t defval
)
819 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
822 printf("(== OpenWrt default)\n");
824 printf("(OpenWrt default: 0x%08x)\n", defval
);
827 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
831 printf("%-23s:", label
);
832 for (i
=0; i
<MD5SUM_LEN
; i
++)
833 printf(" %02x", val
[i
]);
834 printf(" %s\n", text
);
837 static int inspect_fw(void)
840 struct fw_header
*hdr
;
841 uint8_t md5sum
[MD5SUM_LEN
];
842 struct board_info
*board
;
843 int ret
= EXIT_FAILURE
;
845 buf
= malloc(inspect_info
.file_size
);
847 ERR("no memory for buffer!\n");
851 ret
= read_to_buf(&inspect_info
, buf
);
854 hdr
= (struct fw_header
*)buf
;
856 inspect_fw_pstr("File name", inspect_info
.file_name
);
857 inspect_fw_phexdec("File size", inspect_info
.file_size
);
859 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
860 ERR("file does not seem to have V1 header!\n");
864 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
866 if (ntohl(hdr
->unk1
) != 0)
867 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
869 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
870 if (ntohl(hdr
->boot_len
) == 0)
871 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
873 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
874 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
876 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
877 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
878 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
880 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
882 if (ntohl(hdr
->unk2
) != 0)
883 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
884 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
885 "(purpose yet unknown, unchecked here)");
886 if (ntohl(hdr
->unk3
) != 0)
887 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
891 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
892 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
893 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
895 layout
= find_layout(board
->layout_id
);
896 inspect_fw_phexpost("Hardware ID",
897 ntohl(hdr
->hw_id
), board
->id
);
898 inspect_fw_phexexp("Hardware Revision",
899 ntohl(hdr
->hw_rev
), board
->hw_rev
);
901 inspect_fw_phexpost("Hardware ID",
902 ntohl(hdr
->hw_id
), "unknown");
903 inspect_fw_phex("Hardware Revision",
909 inspect_fw_phexdec("Kernel data offset",
910 ntohl(hdr
->kernel_ofs
));
911 inspect_fw_phexdec("Kernel data length",
912 ntohl(hdr
->kernel_len
));
914 inspect_fw_phexdef("Kernel load address",
915 ntohl(hdr
->kernel_la
),
916 layout
? layout
->kernel_la
: 0xffffffff);
917 inspect_fw_phexdef("Kernel entry point",
918 ntohl(hdr
->kernel_ep
),
919 layout
? layout
->kernel_ep
: 0xffffffff);
920 inspect_fw_phexdecdef("Rootfs data offset",
921 ntohl(hdr
->rootfs_ofs
),
922 layout
? layout
->rootfs_ofs
: 0xffffffff);
924 inspect_fw_phex("Kernel load address",
925 ntohl(hdr
->kernel_la
));
926 inspect_fw_phex("Kernel entry point",
927 ntohl(hdr
->kernel_ep
));
928 inspect_fw_phexdec("Rootfs data offset",
929 ntohl(hdr
->rootfs_ofs
));
931 inspect_fw_phexdec("Rootfs data length",
932 ntohl(hdr
->rootfs_len
));
933 inspect_fw_phexdec("Boot loader data offset",
934 ntohl(hdr
->boot_ofs
));
935 inspect_fw_phexdec("Boot loader data length",
936 ntohl(hdr
->boot_len
));
937 inspect_fw_phexdec("Total firmware length",
938 ntohl(hdr
->fw_length
));
946 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
947 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
948 printf("Extracting kernel to \"%s\"...\n", filename
);
949 fp
= fopen(filename
, "w");
951 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
952 ntohl(hdr
->kernel_len
), 1, fp
)) {
953 ERR("error in fwrite(): %s", strerror(errno
));
957 ERR("error in fopen(): %s", strerror(errno
));
961 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
962 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
963 printf("Extracting rootfs to \"%s\"...\n", filename
);
964 fp
= fopen(filename
, "w");
966 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
967 ntohl(hdr
->rootfs_len
), 1, fp
)) {
968 ERR("error in fwrite(): %s", strerror(errno
));
972 ERR("error in fopen(): %s", strerror(errno
));
983 int main(int argc
, char *argv
[])
985 int ret
= EXIT_FAILURE
;
990 progname
= basename(argv
[0]);
995 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
1001 sscanf(optarg
, "0x%x", &rootfs_align
);
1010 sscanf(optarg
, "0x%x", &kernel_ep
);
1016 opt_hw_rev
= optarg
;
1019 sscanf(optarg
, "0x%x", &kernel_la
);
1034 kernel_info
.file_name
= optarg
;
1037 rootfs_info
.file_name
= optarg
;
1040 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1049 inspect_info
.file_name
= optarg
;
1058 usage(EXIT_SUCCESS
);
1061 usage(EXIT_FAILURE
);
1066 ret
= check_options();
1070 if (!inspect_info
.file_name
)