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() */
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
32 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
37 char *file_name
; /* name of the file */
38 uint32_t file_size
; /* length of the file */
42 uint32_t version
; /* 0x00: header version */
43 char fw_version
[48]; /* 0x04: fw version string */
44 uint32_t hw_id
; /* 0x34: hardware id */
45 uint32_t hw_rev
; /* 0x38: FIXME: hardware revision? */
46 uint32_t hw_ver_add
; /* 0x3c: additional hardware version */
47 uint8_t md5sum1
[MD5SUM_LEN
]; /* 0x40 */
48 uint32_t unk2
; /* 0x50: 0x00000000 */
49 uint8_t md5sum2
[MD5SUM_LEN
]; /* 0x54 */
50 uint32_t unk3
; /* 0x64: 0xffffffff */
52 uint32_t kernel_la
; /* 0x68: kernel load address */
53 uint32_t kernel_ep
; /* 0x6c: kernel entry point */
54 uint32_t fw_length
; /* 0x70: total length of the image */
55 uint32_t kernel_ofs
; /* 0x74: kernel data offset */
56 uint32_t kernel_len
; /* 0x78: kernel data length */
57 uint32_t rootfs_ofs
; /* 0x7c: rootfs data offset */
58 uint32_t rootfs_len
; /* 0x80: rootfs data length */
59 uint32_t boot_ofs
; /* 0x84: bootloader offset */
60 uint32_t boot_len
; /* 0x88: bootloader length */
61 uint16_t unk4
; /* 0x8c: 0x55aa */
62 uint8_t sver_hi
; /* 0x8e */
63 uint8_t sver_lo
; /* 0x8f */
64 uint8_t unk5
; /* 0x90: magic: 0xa5 */
65 uint8_t ver_hi
; /* 0x91 */
66 uint8_t ver_mid
; /* 0x92 */
67 uint8_t ver_lo
; /* 0x93 */
69 } __attribute__ ((packed
));
79 #define FLAG_LE_KERNEL_LA_EP 0x00000001 /* Little-endian used for kernel load address & entry point */
95 static char *progname
;
96 static char *vendor
= "TP-LINK Technologies";
97 static char *version
= "ver. 1.0";
98 static char *fw_ver
= "0.0.0";
99 static char *sver
= "1.0";
100 static uint32_t hdr_ver
= 2;
102 static struct board_info custom_board
;
104 static char *board_id
;
105 static struct board_info
*board
;
106 static char *layout_id
;
107 static struct flash_layout
*layout
;
108 static char *opt_hw_id
;
109 static char *opt_hw_rev
;
110 static char *opt_hw_ver_add
;
111 static int fw_ver_lo
;
112 static int fw_ver_mid
;
113 static int fw_ver_hi
;
116 static struct file_info kernel_info
;
117 static uint32_t kernel_la
= 0;
118 static uint32_t kernel_ep
= 0;
119 static uint32_t kernel_len
= 0;
120 static struct file_info rootfs_info
;
121 static uint32_t rootfs_ofs
= 0;
122 static uint32_t rootfs_align
;
123 static struct file_info boot_info
;
125 static int strip_padding
;
126 static int add_jffs2_eof
;
127 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
129 static struct file_info inspect_info
;
130 static int extract
= 0;
132 char md5salt_normal
[MD5SUM_LEN
] = {
133 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
134 0xdc, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x37,
137 char md5salt_boot
[MD5SUM_LEN
] = {
138 0x8c, 0xef, 0x33, 0x5f, 0xd5, 0xc5, 0xce, 0xfa,
139 0xac, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
142 static struct flash_layout layouts
[] = {
145 .fw_max_len
= 0x7a0000,
146 .kernel_la
= 0x80002000,
147 .kernel_ep
= 0x80002000,
148 .rootfs_ofs
= 0x140000,
151 .fw_max_len
= 0xf90000,
152 .kernel_la
= 0x80002000,
153 .kernel_ep
= 0x800061b0,
154 .rootfs_ofs
= 0x140000,
157 .fw_max_len
= 0x7a0000,
158 .kernel_la
= 0x80000000,
159 .kernel_ep
= 0x80000000,
160 .rootfs_ofs
= 0x140000,
163 .fw_max_len
= 0x7b0000,
164 .kernel_la
= 0x80000000,
165 .kernel_ep
= 0x80000000,
166 .rootfs_ofs
= 0x140000,
168 /* terminating entry */
172 static struct board_info boards
[] = {
177 .layout_id
= "8Mltq",
182 .layout_id
= "8Mltq",
188 .layout_id
= "8Mmtk",
190 .flags
= FLAG_LE_KERNEL_LA_EP
195 .layout_id
= "8Mmtk",
197 .flags
= FLAG_LE_KERNEL_LA_EP
,
199 .id
= "ArcherVR200V",
202 .layout_id
= "16Mltq",
208 .layout_id
= "8Mmtk",
210 .flags
= FLAG_LE_KERNEL_LA_EP
,
215 .layout_id
= "8MLmtk",
217 .flags
= FLAG_LE_KERNEL_LA_EP
,
223 .layout_id
= "8Mmtk",
225 .flags
= FLAG_LE_KERNEL_LA_EP
,
227 .id
= "TL-WR841NV13",
231 .layout_id
= "8Mmtk",
233 .flags
= FLAG_LE_KERNEL_LA_EP
,
235 /* terminating entry */
242 #define ERR(fmt, ...) do { \
244 fprintf(stderr, "[%s] *** error: " fmt "\n", \
245 progname, ## __VA_ARGS__ ); \
248 #define ERRS(fmt, ...) do { \
251 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
252 progname, ## __VA_ARGS__, strerror(save)); \
255 #define DBG(fmt, ...) do { \
256 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
259 static struct board_info
*find_board(char *id
)
261 struct board_info
*ret
;
262 struct board_info
*board
;
265 for (board
= boards
; board
->id
!= NULL
; board
++){
266 if (strcasecmp(id
, board
->id
) == 0) {
275 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
277 struct board_info
*board
;
279 for (board
= boards
; board
->id
!= NULL
; board
++) {
280 if (hw_id
== board
->hw_id
)
287 static struct flash_layout
*find_layout(char *id
)
289 struct flash_layout
*ret
;
290 struct flash_layout
*l
;
293 for (l
= layouts
; l
->id
!= NULL
; l
++){
294 if (strcasecmp(id
, l
->id
) == 0) {
303 static void usage(int status
)
305 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
306 struct board_info
*board
;
308 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
312 " -B <board> create image for the board specified with <board>\n"
313 " -c use combined kernel image\n"
314 " -e swap endianness in kernel load address and entry point\n"
315 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
316 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
317 " -H <hwid> use hardware id specified with <hwid>\n"
318 " -W <hwrev> use hardware revision specified with <hwrev>\n"
319 " -w <hwveradd> use additional hardware version specified with <hwveradd>\n"
320 " -F <id> use flash layout specified with <id>\n"
321 " -k <file> read kernel image from the file <file>\n"
322 " -r <file> read rootfs image from the file <file>\n"
323 " -a <align> align the rootfs start on an <align> bytes boundary\n"
324 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
325 " -o <file> write output to the file <file>\n"
326 " -s strip padding from the end of the image\n"
327 " -j add jffs2 end-of-filesystem markers\n"
328 " -N <vendor> set image vendor to <vendor>\n"
329 " -T <version> set header version to <version>\n"
330 " -V <version> set image version to <version>\n"
331 " -v <version> set firmware version to <version>\n"
332 " -y <version> set secondary version to <version>\n"
333 " -i <file> inspect given firmware file <file>\n"
334 " -x extract kernel and rootfs while inspecting (requires -i)\n"
335 " -h show this screen\n"
341 static int get_md5(char *data
, int size
, char *md5
)
346 MD5_Update(&ctx
, data
, size
);
347 MD5_Final(md5
, &ctx
);
350 static int get_file_stat(struct file_info
*fdata
)
355 if (fdata
->file_name
== NULL
)
358 res
= stat(fdata
->file_name
, &st
);
360 ERRS("stat failed on %s", fdata
->file_name
);
364 fdata
->file_size
= st
.st_size
;
368 static int read_to_buf(struct file_info
*fdata
, char *buf
)
371 int ret
= EXIT_FAILURE
;
373 f
= fopen(fdata
->file_name
, "r");
375 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
380 fread(buf
, fdata
->file_size
, 1, f
);
382 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
394 static int check_options(void)
398 if (inspect_info
.file_name
) {
399 ret
= get_file_stat(&inspect_info
);
404 } else if (extract
) {
405 ERR("no firmware for inspection specified");
409 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
410 ERR("either board or hardware id must be specified");
415 board
= find_board(board_id
);
417 ERR("unknown/unsupported board id \"%s\"", board_id
);
420 if (layout_id
== NULL
)
421 layout_id
= board
->layout_id
;
424 hdr_ver
= board
->hdr_ver
;
426 board
= &custom_board
;
428 if (layout_id
== NULL
) {
429 ERR("flash layout is not specified");
433 board
->hw_id
= strtoul(opt_hw_id
, NULL
, 0);
436 board
->hw_ver_add
= 0;
440 board
->hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
442 board
->hw_ver_add
= strtoul(opt_hw_ver_add
, NULL
, 0);
444 layout
= find_layout(layout_id
);
445 if (layout
== NULL
) {
446 ERR("unknown flash layout \"%s\"", layout_id
);
451 kernel_la
= layout
->kernel_la
;
453 kernel_ep
= layout
->kernel_ep
;
455 rootfs_ofs
= layout
->rootfs_ofs
;
457 if (kernel_info
.file_name
== NULL
) {
458 ERR("no kernel image specified");
462 ret
= get_file_stat(&kernel_info
);
466 kernel_len
= kernel_info
.file_size
;
469 if (kernel_info
.file_size
>
470 layout
->fw_max_len
- sizeof(struct fw_header
)) {
471 ERR("kernel image is too big");
475 if (rootfs_info
.file_name
== NULL
) {
476 ERR("no rootfs image specified");
480 ret
= get_file_stat(&rootfs_info
);
485 kernel_len
+= sizeof(struct fw_header
);
486 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
487 kernel_len
-= sizeof(struct fw_header
);
489 DBG("kernel length aligned to %u", kernel_len
);
491 if (kernel_len
+ rootfs_info
.file_size
>
492 layout
->fw_max_len
- sizeof(struct fw_header
)) {
493 ERR("images are too big");
497 if (kernel_info
.file_size
>
498 rootfs_ofs
- sizeof(struct fw_header
)) {
499 ERR("kernel image is too big");
503 if (rootfs_info
.file_size
>
504 (layout
->fw_max_len
- rootfs_ofs
)) {
505 ERR("rootfs image is too big");
511 if (ofname
== NULL
) {
512 ERR("no output file specified");
516 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
518 ERR("invalid firmware version '%s'", fw_ver
);
522 ret
= sscanf(sver
, "%d.%d", &sver_hi
, &sver_lo
);
524 ERR("invalid secondary version '%s'", sver
);
531 static void fill_header(char *buf
, int len
)
533 struct fw_header
*hdr
= (struct fw_header
*)buf
;
536 memset(hdr
, '\xff', sizeof(struct fw_header
));
538 hdr
->version
= htonl(bswap_32(hdr_ver
));
539 ver_len
= strlen(version
);
540 if (ver_len
> (sizeof(hdr
->fw_version
) - 1))
541 ver_len
= sizeof(hdr
->fw_version
) - 1;
543 memcpy(hdr
->fw_version
, version
, ver_len
);
544 hdr
->fw_version
[ver_len
] = 0;
546 hdr
->hw_id
= htonl(board
->hw_id
);
547 hdr
->hw_rev
= htonl(board
->hw_rev
);
548 hdr
->hw_ver_add
= htonl(board
->hw_ver_add
);
550 if (boot_info
.file_size
== 0) {
551 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
552 hdr
->boot_ofs
= htonl(0);
553 hdr
->boot_len
= htonl(0);
555 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
556 hdr
->boot_ofs
= htonl(rootfs_ofs
+ rootfs_info
.file_size
);
557 hdr
->boot_len
= htonl(rootfs_info
.file_size
);
560 hdr
->kernel_la
= htonl(kernel_la
);
561 hdr
->kernel_ep
= htonl(kernel_ep
);
562 hdr
->fw_length
= htonl(layout
->fw_max_len
);
563 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
564 hdr
->kernel_len
= htonl(kernel_len
);
566 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
567 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
570 hdr
->boot_ofs
= htonl(0);
571 hdr
->boot_len
= htonl(boot_info
.file_size
);
573 hdr
->unk2
= htonl(0);
574 hdr
->unk3
= htonl(0xffffffff);
575 hdr
->unk4
= htons(0x55aa);
578 hdr
->sver_hi
= sver_hi
;
579 hdr
->sver_lo
= sver_lo
;
581 hdr
->ver_hi
= fw_ver_hi
;
582 hdr
->ver_mid
= fw_ver_mid
;
583 hdr
->ver_lo
= fw_ver_lo
;
585 if (board
->flags
& FLAG_LE_KERNEL_LA_EP
) {
586 hdr
->kernel_la
= bswap_32(hdr
->kernel_la
);
587 hdr
->kernel_ep
= bswap_32(hdr
->kernel_ep
);
590 get_md5(buf
, len
, hdr
->md5sum1
);
593 static int pad_jffs2(char *buf
, int currlen
)
599 pad_mask
= (64 * 1024);
600 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
604 for (i
= 10; i
< 32; i
++) {
610 len
= ALIGN(len
, mask
);
612 for (i
= 10; i
< 32; i
++) {
614 if ((len
& (mask
- 1)) == 0)
618 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
619 buf
[len
+ i
] = jffs2_eof_mark
[i
];
621 len
+= sizeof(jffs2_eof_mark
);
627 static int write_fw(char *data
, int len
)
630 int ret
= EXIT_FAILURE
;
632 f
= fopen(ofname
, "w");
634 ERRS("could not open \"%s\" for writing", ofname
);
639 fwrite(data
, len
, 1, f
);
641 ERRS("unable to write output file");
645 DBG("firmware file \"%s\" completed", ofname
);
652 if (ret
!= EXIT_SUCCESS
) {
659 static int build_fw(void)
664 int ret
= EXIT_FAILURE
;
667 buflen
= layout
->fw_max_len
;
669 buf
= malloc(buflen
);
671 ERR("no memory for buffer\n");
675 memset(buf
, 0xff, buflen
);
676 p
= buf
+ sizeof(struct fw_header
);
677 ret
= read_to_buf(&kernel_info
, p
);
681 writelen
= sizeof(struct fw_header
) + kernel_len
;
687 p
= buf
+ rootfs_ofs
;
689 ret
= read_to_buf(&rootfs_info
, p
);
694 writelen
+= rootfs_info
.file_size
;
696 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
699 writelen
= pad_jffs2(buf
, writelen
);
705 fill_header(buf
, writelen
);
706 ret
= write_fw(buf
, writelen
);
718 /* Helper functions to inspect_fw() representing different output formats */
719 static inline void inspect_fw_pstr(char *label
, char *str
)
721 printf("%-23s: %s\n", label
, str
);
724 static inline void inspect_fw_phex(char *label
, uint32_t val
)
726 printf("%-23s: 0x%08x\n", label
, val
);
729 static inline void inspect_fw_phexpost(char *label
,
730 uint32_t val
, char *post
)
732 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
735 static inline void inspect_fw_phexdef(char *label
,
736 uint32_t val
, uint32_t defval
)
738 printf("%-23s: 0x%08x ", label
, val
);
741 printf("(== OpenWrt default)\n");
743 printf("(OpenWrt default: 0x%08x)\n", defval
);
746 static inline void inspect_fw_phexexp(char *label
,
747 uint32_t val
, uint32_t expval
)
749 printf("%-23s: 0x%08x ", label
, val
);
754 printf("(expected: 0x%08x)\n", expval
);
757 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
759 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
762 static inline void inspect_fw_phexdecdef(char *label
,
763 uint32_t val
, uint32_t defval
)
765 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
768 printf("(== OpenWrt default)\n");
770 printf("(OpenWrt default: 0x%08x)\n", defval
);
773 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
777 printf("%-23s:", label
);
778 for (i
=0; i
<MD5SUM_LEN
; i
++)
779 printf(" %02x", val
[i
]);
780 printf(" %s\n", text
);
783 static int inspect_fw(void)
786 struct fw_header
*hdr
;
787 uint8_t md5sum
[MD5SUM_LEN
];
788 struct board_info
*board
;
789 int ret
= EXIT_FAILURE
;
791 buf
= malloc(inspect_info
.file_size
);
793 ERR("no memory for buffer!\n");
797 ret
= read_to_buf(&inspect_info
, buf
);
800 hdr
= (struct fw_header
*)buf
;
802 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
804 board
= &custom_board
;
806 if (board
->flags
& FLAG_LE_KERNEL_LA_EP
) {
807 hdr
->kernel_la
= bswap_32(hdr
->kernel_la
);
808 hdr
->kernel_ep
= bswap_32(hdr
->kernel_ep
);
811 inspect_fw_pstr("File name", inspect_info
.file_name
);
812 inspect_fw_phexdec("File size", inspect_info
.file_size
);
814 switch(bswap_32(ntohl(hdr
->version
))) {
819 ERR("file does not seem to have V2/V3 header!\n");
823 inspect_fw_phexdec("Version 2 Header size", sizeof(struct fw_header
));
825 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
826 if (ntohl(hdr
->boot_len
) == 0)
827 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
829 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
830 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
832 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
833 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
834 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
836 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
838 if (ntohl(hdr
->unk2
) != 0)
839 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
840 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
841 "(purpose yet unknown, unchecked here)");
843 if (ntohl(hdr
->unk3
) != 0xffffffff)
844 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
846 if (ntohs(hdr
->unk4
) != 0x55aa)
847 inspect_fw_phexdec("Unknown value 4", hdr
->unk4
);
849 if (hdr
->unk5
!= 0xa5)
850 inspect_fw_phexdec("Unknown value 5", hdr
->unk5
);
854 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
856 if (board
!= &custom_board
) {
857 layout
= find_layout(board
->layout_id
);
858 inspect_fw_phexpost("Hardware ID",
859 ntohl(hdr
->hw_id
), board
->id
);
860 inspect_fw_phexexp("Hardware Revision",
861 ntohl(hdr
->hw_rev
), board
->hw_rev
);
862 inspect_fw_phexexp("Additional HW Version",
863 ntohl(hdr
->hw_ver_add
), board
->hw_ver_add
);
865 inspect_fw_phexpost("Hardware ID",
866 ntohl(hdr
->hw_id
), "unknown");
867 inspect_fw_phex("Hardware Revision",
869 inspect_fw_phex("Additional HW Version",
870 ntohl(hdr
->hw_ver_add
));
873 printf("%-23s: %d.%d.%d-%d.%d\n", "Software version",
874 hdr
->ver_hi
, hdr
->ver_mid
, hdr
->ver_lo
,
875 hdr
->sver_hi
, hdr
->sver_lo
);
879 inspect_fw_phexdec("Kernel data offset",
880 ntohl(hdr
->kernel_ofs
));
881 inspect_fw_phexdec("Kernel data length",
882 ntohl(hdr
->kernel_len
));
883 if (board
!= &custom_board
) {
884 inspect_fw_phexdef("Kernel load address",
885 ntohl(hdr
->kernel_la
),
886 layout
? layout
->kernel_la
: 0xffffffff);
887 inspect_fw_phexdef("Kernel entry point",
888 ntohl(hdr
->kernel_ep
),
889 layout
? layout
->kernel_ep
: 0xffffffff);
890 inspect_fw_phexdecdef("Rootfs data offset",
891 ntohl(hdr
->rootfs_ofs
),
892 layout
? layout
->rootfs_ofs
: 0xffffffff);
894 inspect_fw_phex("Kernel load address",
895 ntohl(hdr
->kernel_la
));
896 inspect_fw_phex("Kernel entry point",
897 ntohl(hdr
->kernel_ep
));
898 inspect_fw_phexdec("Rootfs data offset",
899 ntohl(hdr
->rootfs_ofs
));
901 inspect_fw_phexdec("Rootfs data length",
902 ntohl(hdr
->rootfs_len
));
903 inspect_fw_phexdec("Boot loader data offset",
904 ntohl(hdr
->boot_ofs
));
905 inspect_fw_phexdec("Boot loader data length",
906 ntohl(hdr
->boot_len
));
907 inspect_fw_phexdec("Total firmware length",
908 ntohl(hdr
->fw_length
));
916 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
917 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
918 printf("Extracting kernel to \"%s\"...\n", filename
);
919 fp
= fopen(filename
, "w");
921 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
922 ntohl(hdr
->kernel_len
), 1, fp
)) {
923 ERR("error in fwrite(): %s", strerror(errno
));
927 ERR("error in fopen(): %s", strerror(errno
));
931 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
932 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
933 printf("Extracting rootfs to \"%s\"...\n", filename
);
934 fp
= fopen(filename
, "w");
936 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
937 ntohl(hdr
->rootfs_len
), 1, fp
)) {
938 ERR("error in fwrite(): %s", strerror(errno
));
942 ERR("error in fopen(): %s", strerror(errno
));
953 int main(int argc
, char *argv
[])
955 int ret
= EXIT_FAILURE
;
960 progname
= basename(argv
[0]);
965 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:w:ci:k:r:R:o:xhsjv:y:T:e");
971 sscanf(optarg
, "0x%x", &rootfs_align
);
980 sscanf(optarg
, "0x%x", &kernel_ep
);
989 opt_hw_ver_add
= optarg
;
992 sscanf(optarg
, "0x%x", &kernel_la
);
1010 kernel_info
.file_name
= optarg
;
1013 rootfs_info
.file_name
= optarg
;
1016 sscanf(optarg
, "0x%x", &rootfs_ofs
);
1025 inspect_info
.file_name
= optarg
;
1034 hdr_ver
= atoi(optarg
);
1037 custom_board
.flags
= FLAG_LE_KERNEL_LA_EP
;
1040 usage(EXIT_SUCCESS
);
1043 usage(EXIT_FAILURE
);
1048 ret
= check_options();
1052 if (!inspect_info
.file_name
)