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_WR741ND_V1 0x07410001
48 #define HWID_TL_WR741ND_V4 0x07410004
49 #define HWID_TL_WR740N_V1 0x07400001
50 #define HWID_TL_WR740N_V3 0x07400003
51 #define HWID_TL_WR743ND_V1 0x07430001
52 #define HWID_TL_WR743ND_V2 0x07430002
53 #define HWID_TL_WR841N_V1_5 0x08410002
54 #define HWID_TL_WR841ND_V3 0x08410003
55 #define HWID_TL_WR841ND_V5 0x08410005
56 #define HWID_TL_WR841ND_V7 0x08410007
57 #define HWID_TL_WR941ND_V2 0x09410002
58 #define HWID_TL_WR941ND_V4 0x09410004
59 #define HWID_TL_WR1043ND_V1 0x10430001
60 #define HWID_TL_WR1041N_V2 0x10410002
61 #define HWID_TL_WR2543N_V1 0x25430001
66 char *file_name
; /* name of the file */
67 uint32_t file_size
; /* length of the file */
71 uint32_t version
; /* header version */
74 uint32_t hw_id
; /* hardware id */
75 uint32_t hw_rev
; /* hardware revision */
77 uint8_t md5sum1
[MD5SUM_LEN
];
79 uint8_t md5sum2
[MD5SUM_LEN
];
81 uint32_t kernel_la
; /* kernel load address */
82 uint32_t kernel_ep
; /* kernel entry point */
83 uint32_t fw_length
; /* total length of the firmware */
84 uint32_t kernel_ofs
; /* kernel data offset */
85 uint32_t kernel_len
; /* kernel data length */
86 uint32_t rootfs_ofs
; /* rootfs data offset */
87 uint32_t rootfs_len
; /* rootfs data length */
88 uint32_t boot_ofs
; /* bootloader data offset */
89 uint32_t boot_len
; /* bootloader data length */
94 } __attribute__ ((packed
));
115 static char *progname
;
116 static char *vendor
= "TP-LINK Technologies";
117 static char *version
= "ver. 1.0";
118 static char *fw_ver
= "0.0.0";
120 static char *board_id
;
121 static struct board_info
*board
;
122 static char *layout_id
;
123 static struct flash_layout
*layout
;
124 static char *opt_hw_id
;
125 static uint32_t hw_id
;
126 static char *opt_hw_rev
;
127 static uint32_t hw_rev
;
128 static int fw_ver_lo
;
129 static int fw_ver_mid
;
130 static int fw_ver_hi
;
131 static struct file_info kernel_info
;
132 static uint32_t kernel_la
= 0;
133 static uint32_t kernel_ep
= 0;
134 static uint32_t kernel_len
= 0;
135 static struct file_info rootfs_info
;
136 static uint32_t rootfs_ofs
= 0;
137 static uint32_t rootfs_align
;
138 static struct file_info boot_info
;
140 static int strip_padding
;
141 static int add_jffs2_eof
;
142 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
144 static struct file_info inspect_info
;
145 static int extract
= 0;
147 char md5salt_normal
[MD5SUM_LEN
] = {
148 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
149 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
152 char md5salt_boot
[MD5SUM_LEN
] = {
153 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
154 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
157 static struct flash_layout layouts
[] = {
160 .fw_max_len
= 0x3c0000,
161 .kernel_la
= 0x80060000,
162 .kernel_ep
= 0x80060000,
163 .rootfs_ofs
= 0x140000,
166 .fw_max_len
= 0x3c0000,
167 .kernel_la
= 0x80060000,
168 .kernel_ep
= 0x80060000,
169 .rootfs_ofs
= 0x100000,
172 .fw_max_len
= 0x7c0000,
173 .kernel_la
= 0x80060000,
174 .kernel_ep
= 0x80060000,
175 .rootfs_ofs
= 0x140000,
178 .fw_max_len
= 0x7c0000,
179 .kernel_la
= 0x80060000,
180 .kernel_ep
= 0x80060000,
181 .rootfs_ofs
= 0x100000,
184 .fw_max_len
= 0xf80000,
185 .kernel_la
= 0x00000000,
186 .kernel_ep
= 0xc0000000,
187 .rootfs_ofs
= 0x2a0000,
189 /* terminating entry */
193 static struct board_info boards
[] = {
196 .hw_id
= HWID_TL_MR3020_V1
,
198 .layout_id
= "4Mlzma",
201 .hw_id
= HWID_TL_MR3220_V1
,
206 .hw_id
= HWID_TL_MR3220_V2
,
208 .layout_id
= "4Mlzma",
211 .hw_id
= HWID_TL_MR3420_V1
,
216 .hw_id
= HWID_TL_MR3420_V2
,
218 .layout_id
= "4Mlzma",
221 .hw_id
= HWID_TL_WA701N_V1
,
226 .hw_id
= HWID_TL_WA7510N_V1
,
230 .id
= "TL-WA801NDv1",
231 .hw_id
= HWID_TL_WA801ND_V1
,
235 .id
= "TL-WA830REv1",
236 .hw_id
= HWID_TL_WA830RE_V1
,
240 .id
= "TL-WA830REv2",
241 .hw_id
= HWID_TL_WA830RE_V2
,
245 .id
= "TL-WA901NDv1",
246 .hw_id
= HWID_TL_WA901ND_V1
,
250 .id
= "TL-WA901NDv2",
251 .hw_id
= HWID_TL_WA901ND_V2
,
255 .id
= "TL-WDR4900v1",
256 .hw_id
= HWID_TL_WDR4900_V1
,
258 .layout_id
= "16Mppc",
260 .id
= "TL-WR741NDv1",
261 .hw_id
= HWID_TL_WR741ND_V1
,
265 .id
= "TL-WR741NDv4",
266 .hw_id
= HWID_TL_WR741ND_V4
,
268 .layout_id
= "4Mlzma",
271 .hw_id
= HWID_TL_WR740N_V1
,
276 .hw_id
= HWID_TL_WR740N_V3
,
280 .id
= "TL-WR743NDv1",
281 .hw_id
= HWID_TL_WR743ND_V1
,
285 .id
= "TL-WR743NDv2",
286 .hw_id
= HWID_TL_WR743ND_V2
,
288 .layout_id
= "4Mlzma",
290 .id
= "TL-WR841Nv1.5",
291 .hw_id
= HWID_TL_WR841N_V1_5
,
295 .id
= "TL-WR841NDv3",
296 .hw_id
= HWID_TL_WR841ND_V3
,
300 .id
= "TL-WR841NDv5",
301 .hw_id
= HWID_TL_WR841ND_V5
,
305 .id
= "TL-WR841NDv7",
306 .hw_id
= HWID_TL_WR841ND_V7
,
310 .id
= "TL-WR941NDv2",
311 .hw_id
= HWID_TL_WR941ND_V2
,
315 .id
= "TL-WR941NDv4",
316 .hw_id
= HWID_TL_WR941ND_V4
,
320 .id
= "TL-WR1041Nv2",
321 .hw_id
= HWID_TL_WR1041N_V2
,
323 .layout_id
= "4Mlzma",
325 .id
= "TL-WR1043NDv1",
326 .hw_id
= HWID_TL_WR1043ND_V1
,
330 .id
= "TL-WR2543Nv1",
331 .hw_id
= HWID_TL_WR2543N_V1
,
333 .layout_id
= "8Mlzma",
336 .hw_id
= HWID_TL_WR703N_V1
,
338 .layout_id
= "4Mlzma",
340 /* terminating entry */
347 #define ERR(fmt, ...) do { \
349 fprintf(stderr, "[%s] *** error: " fmt "\n", \
350 progname, ## __VA_ARGS__ ); \
353 #define ERRS(fmt, ...) do { \
356 fprintf(stderr, "[%s] *** error: " fmt "\n", \
357 progname, ## __VA_ARGS__, strerror(save)); \
360 #define DBG(fmt, ...) do { \
361 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
364 static struct board_info
*find_board(char *id
)
366 struct board_info
*ret
;
367 struct board_info
*board
;
370 for (board
= boards
; board
->id
!= NULL
; board
++){
371 if (strcasecmp(id
, board
->id
) == 0) {
380 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
382 struct board_info
*board
;
384 for (board
= boards
; board
->id
!= NULL
; board
++) {
385 if (hw_id
== board
->hw_id
)
392 static struct flash_layout
*find_layout(char *id
)
394 struct flash_layout
*ret
;
395 struct flash_layout
*l
;
398 for (l
= layouts
; l
->id
!= NULL
; l
++){
399 if (strcasecmp(id
, l
->id
) == 0) {
408 static void usage(int status
)
410 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
411 struct board_info
*board
;
413 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
417 " -B <board> create image for the board specified with <board>\n"
418 " -c use combined kernel image\n"
419 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
420 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
421 " -H <hwid> use hardware id specified with <hwid>\n"
422 " -F <id> use flash layout specified with <id>\n"
423 " -k <file> read kernel image from the file <file>\n"
424 " -r <file> read rootfs image from the file <file>\n"
425 " -a <align> align the rootfs start on an <align> bytes boundary\n"
426 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
427 " -o <file> write output to the file <file>\n"
428 " -s strip padding from the end of the image\n"
429 " -j add jffs2 end-of-filesystem markers\n"
430 " -N <vendor> set image vendor to <vendor>\n"
431 " -V <version> set image version to <version>\n"
432 " -v <version> set firmware version to <version>\n"
433 " -i <file> inspect given firmware file <file>\n"
434 " -x extract kernel and rootfs while inspecting (requires -i)\n"
435 " -h show this screen\n"
441 static int get_md5(char *data
, int size
, char *md5
)
446 MD5_Update(&ctx
, data
, size
);
447 MD5_Final(md5
, &ctx
);
450 static int get_file_stat(struct file_info
*fdata
)
455 if (fdata
->file_name
== NULL
)
458 res
= stat(fdata
->file_name
, &st
);
460 ERRS("stat failed on %s", fdata
->file_name
);
464 fdata
->file_size
= st
.st_size
;
468 static int read_to_buf(struct file_info
*fdata
, char *buf
)
471 int ret
= EXIT_FAILURE
;
473 f
= fopen(fdata
->file_name
, "r");
475 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
480 fread(buf
, fdata
->file_size
, 1, f
);
482 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
494 static int check_options(void)
498 if (inspect_info
.file_name
) {
499 ret
= get_file_stat(&inspect_info
);
504 } else if (extract
) {
505 ERR("no firmware for inspection specified");
509 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
510 ERR("either board or hardware id must be specified");
515 board
= find_board(board_id
);
517 ERR("unknown/unsupported board id \"%s\"", board_id
);
520 if (layout_id
== NULL
)
521 layout_id
= board
->layout_id
;
523 hw_id
= board
->hw_id
;
524 hw_rev
= board
->hw_rev
;
526 if (layout_id
== NULL
) {
527 ERR("flash layout is not specified");
530 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
533 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
538 layout
= find_layout(layout_id
);
539 if (layout
== NULL
) {
540 ERR("unknown flash layout \"%s\"", layout_id
);
545 kernel_la
= layout
->kernel_la
;
547 kernel_ep
= layout
->kernel_ep
;
549 rootfs_ofs
= layout
->rootfs_ofs
;
551 if (kernel_info
.file_name
== NULL
) {
552 ERR("no kernel image specified");
556 ret
= get_file_stat(&kernel_info
);
560 kernel_len
= kernel_info
.file_size
;
563 if (kernel_info
.file_size
>
564 layout
->fw_max_len
- sizeof(struct fw_header
)) {
565 ERR("kernel image is too big");
569 if (rootfs_info
.file_name
== NULL
) {
570 ERR("no rootfs image specified");
574 ret
= get_file_stat(&rootfs_info
);
579 kernel_len
+= sizeof(struct fw_header
);
580 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
581 kernel_len
-= sizeof(struct fw_header
);
583 DBG("kernel length aligned to %u", kernel_len
);
585 if (kernel_len
+ rootfs_info
.file_size
>
586 layout
->fw_max_len
- sizeof(struct fw_header
)) {
587 ERR("images are too big");
591 if (kernel_info
.file_size
>
592 rootfs_ofs
- sizeof(struct fw_header
)) {
593 ERR("kernel image is too big");
597 if (rootfs_info
.file_size
>
598 (layout
->fw_max_len
- rootfs_ofs
)) {
599 ERR("rootfs image is too big");
605 if (ofname
== NULL
) {
606 ERR("no output file specified");
610 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
612 ERR("invalid firmware version '%s'", fw_ver
);
619 static void fill_header(char *buf
, int len
)
621 struct fw_header
*hdr
= (struct fw_header
*)buf
;
623 memset(hdr
, 0, sizeof(struct fw_header
));
625 hdr
->version
= htonl(HEADER_VERSION_V1
);
626 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
627 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
628 hdr
->hw_id
= htonl(hw_id
);
629 hdr
->hw_rev
= htonl(hw_rev
);
631 if (boot_info
.file_size
== 0)
632 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
634 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
636 hdr
->kernel_la
= htonl(kernel_la
);
637 hdr
->kernel_ep
= htonl(kernel_ep
);
638 hdr
->fw_length
= htonl(layout
->fw_max_len
);
639 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
640 hdr
->kernel_len
= htonl(kernel_len
);
642 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
643 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
646 hdr
->ver_hi
= htons(fw_ver_hi
);
647 hdr
->ver_mid
= htons(fw_ver_mid
);
648 hdr
->ver_lo
= htons(fw_ver_lo
);
650 get_md5(buf
, len
, hdr
->md5sum1
);
653 static int pad_jffs2(char *buf
, int currlen
)
659 pad_mask
= (64 * 1024);
660 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
664 for (i
= 10; i
< 32; i
++) {
670 len
= ALIGN(len
, mask
);
672 for (i
= 10; i
< 32; i
++) {
674 if ((len
& (mask
- 1)) == 0)
678 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
679 buf
[len
+ i
] = jffs2_eof_mark
[i
];
681 len
+= sizeof(jffs2_eof_mark
);
687 static int write_fw(char *data
, int len
)
690 int ret
= EXIT_FAILURE
;
692 f
= fopen(ofname
, "w");
694 ERRS("could not open \"%s\" for writing", ofname
);
699 fwrite(data
, len
, 1, f
);
701 ERRS("unable to write output file");
705 DBG("firmware file \"%s\" completed", ofname
);
712 if (ret
!= EXIT_SUCCESS
) {
719 static int build_fw(void)
724 int ret
= EXIT_FAILURE
;
727 buflen
= layout
->fw_max_len
;
729 buf
= malloc(buflen
);
731 ERR("no memory for buffer\n");
735 memset(buf
, 0xff, buflen
);
736 p
= buf
+ sizeof(struct fw_header
);
737 ret
= read_to_buf(&kernel_info
, p
);
741 writelen
= sizeof(struct fw_header
) + kernel_len
;
747 p
= buf
+ rootfs_ofs
;
749 ret
= read_to_buf(&rootfs_info
, p
);
754 writelen
+= rootfs_info
.file_size
;
756 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
759 writelen
= pad_jffs2(buf
, writelen
);
765 fill_header(buf
, writelen
);
766 ret
= write_fw(buf
, writelen
);
778 /* Helper functions to inspect_fw() representing different output formats */
779 static inline void inspect_fw_pstr(char *label
, char *str
)
781 printf("%-23s: %s\n", label
, str
);
784 static inline void inspect_fw_phex(char *label
, uint32_t val
)
786 printf("%-23s: 0x%08x\n", label
, val
);
789 static inline void inspect_fw_phexpost(char *label
,
790 uint32_t val
, char *post
)
792 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
795 static inline void inspect_fw_phexdef(char *label
,
796 uint32_t val
, uint32_t defval
)
798 printf("%-23s: 0x%08x ", label
, val
);
801 printf("(== OpenWrt default)\n");
803 printf("(OpenWrt default: 0x%08x)\n", defval
);
806 static inline void inspect_fw_phexexp(char *label
,
807 uint32_t val
, uint32_t expval
)
809 printf("%-23s: 0x%08x ", label
, val
);
814 printf("(expected: 0x%08x)\n", expval
);
817 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
819 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
822 static inline void inspect_fw_phexdecdef(char *label
,
823 uint32_t val
, uint32_t defval
)
825 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
828 printf("(== OpenWrt default)\n");
830 printf("(OpenWrt default: 0x%08x)\n", defval
);
833 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
837 printf("%-23s:", label
);
838 for (i
=0; i
<MD5SUM_LEN
; i
++)
839 printf(" %02x", val
[i
]);
840 printf(" %s\n", text
);
843 static int inspect_fw(void)
846 struct fw_header
*hdr
;
847 uint8_t md5sum
[MD5SUM_LEN
];
848 struct board_info
*board
;
849 int ret
= EXIT_FAILURE
;
851 buf
= malloc(inspect_info
.file_size
);
853 ERR("no memory for buffer!\n");
857 ret
= read_to_buf(&inspect_info
, buf
);
860 hdr
= (struct fw_header
*)buf
;
862 inspect_fw_pstr("File name", inspect_info
.file_name
);
863 inspect_fw_phexdec("File size", inspect_info
.file_size
);
865 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
866 ERR("file does not seem to have V1 header!\n");
870 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
872 if (ntohl(hdr
->unk1
) != 0)
873 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
875 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
876 if (ntohl(hdr
->boot_len
) == 0)
877 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
879 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
880 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
882 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
883 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
884 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
886 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
888 if (ntohl(hdr
->unk2
) != 0)
889 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
890 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
891 "(purpose yet unknown, unchecked here)");
892 if (ntohl(hdr
->unk3
) != 0)
893 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
897 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
898 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
899 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
901 layout
= find_layout(board
->layout_id
);
902 inspect_fw_phexpost("Hardware ID",
903 ntohl(hdr
->hw_id
), board
->id
);
904 inspect_fw_phexexp("Hardware Revision",
905 ntohl(hdr
->hw_rev
), board
->hw_rev
);
907 inspect_fw_phexpost("Hardware ID",
908 ntohl(hdr
->hw_id
), "unknown");
909 inspect_fw_phex("Hardware Revision",
915 inspect_fw_phexdec("Kernel data offset",
916 ntohl(hdr
->kernel_ofs
));
917 inspect_fw_phexdec("Kernel data length",
918 ntohl(hdr
->kernel_len
));
920 inspect_fw_phexdef("Kernel load address",
921 ntohl(hdr
->kernel_la
),
922 layout
? layout
->kernel_la
: 0xffffffff);
923 inspect_fw_phexdef("Kernel entry point",
924 ntohl(hdr
->kernel_ep
),
925 layout
? layout
->kernel_ep
: 0xffffffff);
926 inspect_fw_phexdecdef("Rootfs data offset",
927 ntohl(hdr
->rootfs_ofs
),
928 layout
? layout
->rootfs_ofs
: 0xffffffff);
930 inspect_fw_phex("Kernel load address",
931 ntohl(hdr
->kernel_la
));
932 inspect_fw_phex("Kernel entry point",
933 ntohl(hdr
->kernel_ep
));
934 inspect_fw_phexdec("Rootfs data offset",
935 ntohl(hdr
->rootfs_ofs
));
937 inspect_fw_phexdec("Rootfs data length",
938 ntohl(hdr
->rootfs_len
));
939 inspect_fw_phexdec("Boot loader data offset",
940 ntohl(hdr
->boot_ofs
));
941 inspect_fw_phexdec("Boot loader data length",
942 ntohl(hdr
->boot_len
));
943 inspect_fw_phexdec("Total firmware length",
944 ntohl(hdr
->fw_length
));
952 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
953 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
954 printf("Extracting kernel to \"%s\"...\n", filename
);
955 fp
= fopen(filename
, "w");
957 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
958 ntohl(hdr
->kernel_len
), 1, fp
)) {
959 ERR("error in fwrite(): %s", strerror(errno
));
963 ERR("error in fopen(): %s", strerror(errno
));
967 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
968 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
969 printf("Extracting rootfs to \"%s\"...\n", filename
);
970 fp
= fopen(filename
, "w");
972 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
973 ntohl(hdr
->rootfs_len
), 1, fp
)) {
974 ERR("error in fwrite(): %s", strerror(errno
));
978 ERR("error in fopen(): %s", strerror(errno
));
989 int main(int argc
, char *argv
[])
991 int ret
= EXIT_FAILURE
;
996 progname
= basename(argv
[0]);
1001 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
1007 sscanf(optarg
, "0x%x", &rootfs_align
);
1016 sscanf(optarg
, "0x%x", &kernel_ep
);
1022 opt_hw_rev
= optarg
;
1025 sscanf(optarg
, "0x%x", &kernel_la
);
1040 kernel_info
.file_name
= optarg
;
1043 rootfs_info
.file_name
= optarg
;
1046 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1055 inspect_info
.file_name
= optarg
;
1064 usage(EXIT_SUCCESS
);
1067 usage(EXIT_FAILURE
);
1072 ret
= check_options();
1076 if (!inspect_info
.file_name
)