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_WA901ND_V1 0x09010001
46 #define HWID_TL_WA901ND_V2 0x09010002
47 #define HWID_TL_WDR4900_V1 0x49000001
48 #define HWID_TL_WR703N_V1 0x07030101
49 #define HWID_TL_WR720N_V3 0x07200103
50 #define HWID_TL_WR741ND_V1 0x07410001
51 #define HWID_TL_WR741ND_V4 0x07410004
52 #define HWID_TL_WR740N_V1 0x07400001
53 #define HWID_TL_WR740N_V3 0x07400003
54 #define HWID_TL_WR743ND_V1 0x07430001
55 #define HWID_TL_WR743ND_V2 0x07430002
56 #define HWID_TL_WR841N_V1_5 0x08410002
57 #define HWID_TL_WR841ND_V3 0x08410003
58 #define HWID_TL_WR841ND_V5 0x08410005
59 #define HWID_TL_WR841ND_V7 0x08410007
60 #define HWID_TL_WR941ND_V2 0x09410002
61 #define HWID_TL_WR941ND_V4 0x09410004
62 #define HWID_TL_WR1043ND_V1 0x10430001
63 #define HWID_TL_WR1043ND_V2 0x10430002
64 #define HWID_TL_WR1041N_V2 0x10410002
65 #define HWID_TL_WR2543N_V1 0x25430001
70 char *file_name
; /* name of the file */
71 uint32_t file_size
; /* length of the file */
75 uint32_t version
; /* header version */
78 uint32_t hw_id
; /* hardware id */
79 uint32_t hw_rev
; /* hardware revision */
81 uint8_t md5sum1
[MD5SUM_LEN
];
83 uint8_t md5sum2
[MD5SUM_LEN
];
85 uint32_t kernel_la
; /* kernel load address */
86 uint32_t kernel_ep
; /* kernel entry point */
87 uint32_t fw_length
; /* total length of the firmware */
88 uint32_t kernel_ofs
; /* kernel data offset */
89 uint32_t kernel_len
; /* kernel data length */
90 uint32_t rootfs_ofs
; /* rootfs data offset */
91 uint32_t rootfs_len
; /* rootfs data length */
92 uint32_t boot_ofs
; /* bootloader data offset */
93 uint32_t boot_len
; /* bootloader data length */
98 } __attribute__ ((packed
));
100 struct flash_layout
{
119 static char *progname
;
120 static char *vendor
= "TP-LINK Technologies";
121 static char *version
= "ver. 1.0";
122 static char *fw_ver
= "0.0.0";
124 static char *board_id
;
125 static struct board_info
*board
;
126 static char *layout_id
;
127 static struct flash_layout
*layout
;
128 static char *opt_hw_id
;
129 static uint32_t hw_id
;
130 static char *opt_hw_rev
;
131 static uint32_t hw_rev
;
132 static int fw_ver_lo
;
133 static int fw_ver_mid
;
134 static int fw_ver_hi
;
135 static struct file_info kernel_info
;
136 static uint32_t kernel_la
= 0;
137 static uint32_t kernel_ep
= 0;
138 static uint32_t kernel_len
= 0;
139 static struct file_info rootfs_info
;
140 static uint32_t rootfs_ofs
= 0;
141 static uint32_t rootfs_align
;
142 static struct file_info boot_info
;
144 static int strip_padding
;
145 static int add_jffs2_eof
;
146 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
148 static struct file_info inspect_info
;
149 static int extract
= 0;
151 char md5salt_normal
[MD5SUM_LEN
] = {
152 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
153 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
156 char md5salt_boot
[MD5SUM_LEN
] = {
157 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
158 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
161 static struct flash_layout layouts
[] = {
164 .fw_max_len
= 0x3c0000,
165 .kernel_la
= 0x80060000,
166 .kernel_ep
= 0x80060000,
167 .rootfs_ofs
= 0x140000,
170 .fw_max_len
= 0x3c0000,
171 .kernel_la
= 0x80060000,
172 .kernel_ep
= 0x80060000,
173 .rootfs_ofs
= 0x100000,
176 .fw_max_len
= 0x7c0000,
177 .kernel_la
= 0x80060000,
178 .kernel_ep
= 0x80060000,
179 .rootfs_ofs
= 0x140000,
182 .fw_max_len
= 0x7c0000,
183 .kernel_la
= 0x80060000,
184 .kernel_ep
= 0x80060000,
185 .rootfs_ofs
= 0x100000,
188 .fw_max_len
= 0xf80000,
189 .kernel_la
= 0x00000000,
190 .kernel_ep
= 0xc0000000,
191 .rootfs_ofs
= 0x2a0000,
193 /* terminating entry */
197 static struct board_info boards
[] = {
200 .hw_id
= HWID_TL_MR10U_V1
,
202 .layout_id
= "4Mlzma",
205 .hw_id
= HWID_TL_MR13U_V1
,
207 .layout_id
= "4Mlzma",
210 .hw_id
= HWID_TL_MR3020_V1
,
212 .layout_id
= "4Mlzma",
215 .hw_id
= HWID_TL_MR3220_V1
,
220 .hw_id
= HWID_TL_MR3220_V2
,
222 .layout_id
= "4Mlzma",
225 .hw_id
= HWID_TL_MR3420_V1
,
230 .hw_id
= HWID_TL_MR3420_V2
,
232 .layout_id
= "4Mlzma",
235 .hw_id
= HWID_TL_WA701N_V1
,
240 .hw_id
= HWID_TL_WA7510N_V1
,
244 .id
= "TL-WA801NDv1",
245 .hw_id
= HWID_TL_WA801ND_V1
,
249 .id
= "TL-WA830REv1",
250 .hw_id
= HWID_TL_WA830RE_V1
,
254 .id
= "TL-WA830REv2",
255 .hw_id
= HWID_TL_WA830RE_V2
,
259 .id
= "TL-WA901NDv1",
260 .hw_id
= HWID_TL_WA901ND_V1
,
264 .id
= "TL-WA901NDv2",
265 .hw_id
= HWID_TL_WA901ND_V2
,
269 .id
= "TL-WDR4900v1",
270 .hw_id
= HWID_TL_WDR4900_V1
,
272 .layout_id
= "16Mppc",
274 .id
= "TL-WR741NDv1",
275 .hw_id
= HWID_TL_WR741ND_V1
,
279 .id
= "TL-WR741NDv4",
280 .hw_id
= HWID_TL_WR741ND_V4
,
282 .layout_id
= "4Mlzma",
285 .hw_id
= HWID_TL_WR740N_V1
,
290 .hw_id
= HWID_TL_WR740N_V3
,
294 .id
= "TL-WR743NDv1",
295 .hw_id
= HWID_TL_WR743ND_V1
,
299 .id
= "TL-WR743NDv2",
300 .hw_id
= HWID_TL_WR743ND_V2
,
302 .layout_id
= "4Mlzma",
304 .id
= "TL-WR841Nv1.5",
305 .hw_id
= HWID_TL_WR841N_V1_5
,
309 .id
= "TL-WR841NDv3",
310 .hw_id
= HWID_TL_WR841ND_V3
,
314 .id
= "TL-WR841NDv5",
315 .hw_id
= HWID_TL_WR841ND_V5
,
319 .id
= "TL-WR841NDv7",
320 .hw_id
= HWID_TL_WR841ND_V7
,
324 .id
= "TL-WR941NDv2",
325 .hw_id
= HWID_TL_WR941ND_V2
,
329 .id
= "TL-WR941NDv4",
330 .hw_id
= HWID_TL_WR941ND_V4
,
334 .id
= "TL-WR1041Nv2",
335 .hw_id
= HWID_TL_WR1041N_V2
,
337 .layout_id
= "4Mlzma",
339 .id
= "TL-WR1043NDv1",
340 .hw_id
= HWID_TL_WR1043ND_V1
,
344 .id
= "TL-WR1043NDv2",
345 .hw_id
= HWID_TL_WR1043ND_V2
,
347 .layout_id
= "8Mlzma",
349 .id
= "TL-WR2543Nv1",
350 .hw_id
= HWID_TL_WR2543N_V1
,
352 .layout_id
= "8Mlzma",
355 .hw_id
= HWID_TL_WR703N_V1
,
357 .layout_id
= "4Mlzma",
360 .hw_id
= HWID_TL_WR720N_V3
,
362 .layout_id
= "4Mlzma",
364 /* terminating entry */
371 #define ERR(fmt, ...) do { \
373 fprintf(stderr, "[%s] *** error: " fmt "\n", \
374 progname, ## __VA_ARGS__ ); \
377 #define ERRS(fmt, ...) do { \
380 fprintf(stderr, "[%s] *** error: " fmt "\n", \
381 progname, ## __VA_ARGS__, strerror(save)); \
384 #define DBG(fmt, ...) do { \
385 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
388 static struct board_info
*find_board(char *id
)
390 struct board_info
*ret
;
391 struct board_info
*board
;
394 for (board
= boards
; board
->id
!= NULL
; board
++){
395 if (strcasecmp(id
, board
->id
) == 0) {
404 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
406 struct board_info
*board
;
408 for (board
= boards
; board
->id
!= NULL
; board
++) {
409 if (hw_id
== board
->hw_id
)
416 static struct flash_layout
*find_layout(char *id
)
418 struct flash_layout
*ret
;
419 struct flash_layout
*l
;
422 for (l
= layouts
; l
->id
!= NULL
; l
++){
423 if (strcasecmp(id
, l
->id
) == 0) {
432 static void usage(int status
)
434 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
435 struct board_info
*board
;
437 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
441 " -B <board> create image for the board specified with <board>\n"
442 " -c use combined kernel image\n"
443 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
444 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
445 " -H <hwid> use hardware id specified with <hwid>\n"
446 " -W <hwrev> use hardware revision specified with <hwrev>\n"
447 " -F <id> use flash layout specified with <id>\n"
448 " -k <file> read kernel image from the file <file>\n"
449 " -r <file> read rootfs image from the file <file>\n"
450 " -a <align> align the rootfs start on an <align> bytes boundary\n"
451 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
452 " -o <file> write output to the file <file>\n"
453 " -s strip padding from the end of the image\n"
454 " -j add jffs2 end-of-filesystem markers\n"
455 " -N <vendor> set image vendor to <vendor>\n"
456 " -V <version> set image version to <version>\n"
457 " -v <version> set firmware version to <version>\n"
458 " -i <file> inspect given firmware file <file>\n"
459 " -x extract kernel and rootfs while inspecting (requires -i)\n"
460 " -h show this screen\n"
466 static int get_md5(char *data
, int size
, char *md5
)
471 MD5_Update(&ctx
, data
, size
);
472 MD5_Final(md5
, &ctx
);
475 static int get_file_stat(struct file_info
*fdata
)
480 if (fdata
->file_name
== NULL
)
483 res
= stat(fdata
->file_name
, &st
);
485 ERRS("stat failed on %s", fdata
->file_name
);
489 fdata
->file_size
= st
.st_size
;
493 static int read_to_buf(struct file_info
*fdata
, char *buf
)
496 int ret
= EXIT_FAILURE
;
498 f
= fopen(fdata
->file_name
, "r");
500 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
505 fread(buf
, fdata
->file_size
, 1, f
);
507 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
519 static int check_options(void)
523 if (inspect_info
.file_name
) {
524 ret
= get_file_stat(&inspect_info
);
529 } else if (extract
) {
530 ERR("no firmware for inspection specified");
534 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
535 ERR("either board or hardware id must be specified");
540 board
= find_board(board_id
);
542 ERR("unknown/unsupported board id \"%s\"", board_id
);
545 if (layout_id
== NULL
)
546 layout_id
= board
->layout_id
;
548 hw_id
= board
->hw_id
;
549 hw_rev
= board
->hw_rev
;
551 if (layout_id
== NULL
) {
552 ERR("flash layout is not specified");
555 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
558 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
563 layout
= find_layout(layout_id
);
564 if (layout
== NULL
) {
565 ERR("unknown flash layout \"%s\"", layout_id
);
570 kernel_la
= layout
->kernel_la
;
572 kernel_ep
= layout
->kernel_ep
;
574 rootfs_ofs
= layout
->rootfs_ofs
;
576 if (kernel_info
.file_name
== NULL
) {
577 ERR("no kernel image specified");
581 ret
= get_file_stat(&kernel_info
);
585 kernel_len
= kernel_info
.file_size
;
588 if (kernel_info
.file_size
>
589 layout
->fw_max_len
- sizeof(struct fw_header
)) {
590 ERR("kernel image is too big");
594 if (rootfs_info
.file_name
== NULL
) {
595 ERR("no rootfs image specified");
599 ret
= get_file_stat(&rootfs_info
);
604 kernel_len
+= sizeof(struct fw_header
);
605 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
606 kernel_len
-= sizeof(struct fw_header
);
608 DBG("kernel length aligned to %u", kernel_len
);
610 if (kernel_len
+ rootfs_info
.file_size
>
611 layout
->fw_max_len
- sizeof(struct fw_header
)) {
612 ERR("images are too big");
616 if (kernel_info
.file_size
>
617 rootfs_ofs
- sizeof(struct fw_header
)) {
618 ERR("kernel image is too big");
622 if (rootfs_info
.file_size
>
623 (layout
->fw_max_len
- rootfs_ofs
)) {
624 ERR("rootfs image is too big");
630 if (ofname
== NULL
) {
631 ERR("no output file specified");
635 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
637 ERR("invalid firmware version '%s'", fw_ver
);
644 static void fill_header(char *buf
, int len
)
646 struct fw_header
*hdr
= (struct fw_header
*)buf
;
648 memset(hdr
, 0, sizeof(struct fw_header
));
650 hdr
->version
= htonl(HEADER_VERSION_V1
);
651 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
652 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
653 hdr
->hw_id
= htonl(hw_id
);
654 hdr
->hw_rev
= htonl(hw_rev
);
656 if (boot_info
.file_size
== 0)
657 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
659 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
661 hdr
->kernel_la
= htonl(kernel_la
);
662 hdr
->kernel_ep
= htonl(kernel_ep
);
663 hdr
->fw_length
= htonl(layout
->fw_max_len
);
664 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
665 hdr
->kernel_len
= htonl(kernel_len
);
667 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
668 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
671 hdr
->ver_hi
= htons(fw_ver_hi
);
672 hdr
->ver_mid
= htons(fw_ver_mid
);
673 hdr
->ver_lo
= htons(fw_ver_lo
);
675 get_md5(buf
, len
, hdr
->md5sum1
);
678 static int pad_jffs2(char *buf
, int currlen
)
684 pad_mask
= (64 * 1024);
685 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
689 for (i
= 10; i
< 32; i
++) {
695 len
= ALIGN(len
, mask
);
697 for (i
= 10; i
< 32; i
++) {
699 if ((len
& (mask
- 1)) == 0)
703 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
704 buf
[len
+ i
] = jffs2_eof_mark
[i
];
706 len
+= sizeof(jffs2_eof_mark
);
712 static int write_fw(char *data
, int len
)
715 int ret
= EXIT_FAILURE
;
717 f
= fopen(ofname
, "w");
719 ERRS("could not open \"%s\" for writing", ofname
);
724 fwrite(data
, len
, 1, f
);
726 ERRS("unable to write output file");
730 DBG("firmware file \"%s\" completed", ofname
);
737 if (ret
!= EXIT_SUCCESS
) {
744 static int build_fw(void)
749 int ret
= EXIT_FAILURE
;
752 buflen
= layout
->fw_max_len
;
754 buf
= malloc(buflen
);
756 ERR("no memory for buffer\n");
760 memset(buf
, 0xff, buflen
);
761 p
= buf
+ sizeof(struct fw_header
);
762 ret
= read_to_buf(&kernel_info
, p
);
766 writelen
= sizeof(struct fw_header
) + kernel_len
;
772 p
= buf
+ rootfs_ofs
;
774 ret
= read_to_buf(&rootfs_info
, p
);
779 writelen
+= rootfs_info
.file_size
;
781 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
784 writelen
= pad_jffs2(buf
, writelen
);
790 fill_header(buf
, writelen
);
791 ret
= write_fw(buf
, writelen
);
803 /* Helper functions to inspect_fw() representing different output formats */
804 static inline void inspect_fw_pstr(char *label
, char *str
)
806 printf("%-23s: %s\n", label
, str
);
809 static inline void inspect_fw_phex(char *label
, uint32_t val
)
811 printf("%-23s: 0x%08x\n", label
, val
);
814 static inline void inspect_fw_phexpost(char *label
,
815 uint32_t val
, char *post
)
817 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
820 static inline void inspect_fw_phexdef(char *label
,
821 uint32_t val
, uint32_t defval
)
823 printf("%-23s: 0x%08x ", label
, val
);
826 printf("(== OpenWrt default)\n");
828 printf("(OpenWrt default: 0x%08x)\n", defval
);
831 static inline void inspect_fw_phexexp(char *label
,
832 uint32_t val
, uint32_t expval
)
834 printf("%-23s: 0x%08x ", label
, val
);
839 printf("(expected: 0x%08x)\n", expval
);
842 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
844 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
847 static inline void inspect_fw_phexdecdef(char *label
,
848 uint32_t val
, uint32_t defval
)
850 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
853 printf("(== OpenWrt default)\n");
855 printf("(OpenWrt default: 0x%08x)\n", defval
);
858 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
862 printf("%-23s:", label
);
863 for (i
=0; i
<MD5SUM_LEN
; i
++)
864 printf(" %02x", val
[i
]);
865 printf(" %s\n", text
);
868 static int inspect_fw(void)
871 struct fw_header
*hdr
;
872 uint8_t md5sum
[MD5SUM_LEN
];
873 struct board_info
*board
;
874 int ret
= EXIT_FAILURE
;
876 buf
= malloc(inspect_info
.file_size
);
878 ERR("no memory for buffer!\n");
882 ret
= read_to_buf(&inspect_info
, buf
);
885 hdr
= (struct fw_header
*)buf
;
887 inspect_fw_pstr("File name", inspect_info
.file_name
);
888 inspect_fw_phexdec("File size", inspect_info
.file_size
);
890 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
891 ERR("file does not seem to have V1 header!\n");
895 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
897 if (ntohl(hdr
->unk1
) != 0)
898 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
900 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
901 if (ntohl(hdr
->boot_len
) == 0)
902 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
904 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
905 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
907 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
908 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
909 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
911 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
913 if (ntohl(hdr
->unk2
) != 0)
914 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
915 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
916 "(purpose yet unknown, unchecked here)");
917 if (ntohl(hdr
->unk3
) != 0)
918 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
922 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
923 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
924 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
926 layout
= find_layout(board
->layout_id
);
927 inspect_fw_phexpost("Hardware ID",
928 ntohl(hdr
->hw_id
), board
->id
);
929 inspect_fw_phexexp("Hardware Revision",
930 ntohl(hdr
->hw_rev
), board
->hw_rev
);
932 inspect_fw_phexpost("Hardware ID",
933 ntohl(hdr
->hw_id
), "unknown");
934 inspect_fw_phex("Hardware Revision",
940 inspect_fw_phexdec("Kernel data offset",
941 ntohl(hdr
->kernel_ofs
));
942 inspect_fw_phexdec("Kernel data length",
943 ntohl(hdr
->kernel_len
));
945 inspect_fw_phexdef("Kernel load address",
946 ntohl(hdr
->kernel_la
),
947 layout
? layout
->kernel_la
: 0xffffffff);
948 inspect_fw_phexdef("Kernel entry point",
949 ntohl(hdr
->kernel_ep
),
950 layout
? layout
->kernel_ep
: 0xffffffff);
951 inspect_fw_phexdecdef("Rootfs data offset",
952 ntohl(hdr
->rootfs_ofs
),
953 layout
? layout
->rootfs_ofs
: 0xffffffff);
955 inspect_fw_phex("Kernel load address",
956 ntohl(hdr
->kernel_la
));
957 inspect_fw_phex("Kernel entry point",
958 ntohl(hdr
->kernel_ep
));
959 inspect_fw_phexdec("Rootfs data offset",
960 ntohl(hdr
->rootfs_ofs
));
962 inspect_fw_phexdec("Rootfs data length",
963 ntohl(hdr
->rootfs_len
));
964 inspect_fw_phexdec("Boot loader data offset",
965 ntohl(hdr
->boot_ofs
));
966 inspect_fw_phexdec("Boot loader data length",
967 ntohl(hdr
->boot_len
));
968 inspect_fw_phexdec("Total firmware length",
969 ntohl(hdr
->fw_length
));
977 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
978 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
979 printf("Extracting kernel to \"%s\"...\n", filename
);
980 fp
= fopen(filename
, "w");
982 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
983 ntohl(hdr
->kernel_len
), 1, fp
)) {
984 ERR("error in fwrite(): %s", strerror(errno
));
988 ERR("error in fopen(): %s", strerror(errno
));
992 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
993 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
994 printf("Extracting rootfs to \"%s\"...\n", filename
);
995 fp
= fopen(filename
, "w");
997 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
998 ntohl(hdr
->rootfs_len
), 1, fp
)) {
999 ERR("error in fwrite(): %s", strerror(errno
));
1003 ERR("error in fopen(): %s", strerror(errno
));
1014 int main(int argc
, char *argv
[])
1016 int ret
= EXIT_FAILURE
;
1021 progname
= basename(argv
[0]);
1026 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
1032 sscanf(optarg
, "0x%x", &rootfs_align
);
1041 sscanf(optarg
, "0x%x", &kernel_ep
);
1047 opt_hw_rev
= optarg
;
1050 sscanf(optarg
, "0x%x", &kernel_la
);
1065 kernel_info
.file_name
= optarg
;
1068 rootfs_info
.file_name
= optarg
;
1071 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1080 inspect_info
.file_name
= optarg
;
1089 usage(EXIT_SUCCESS
);
1092 usage(EXIT_FAILURE
);
1097 ret
= check_options();
1101 if (!inspect_info
.file_name
)