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)); })
33 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
35 #define HEADER_VERSION_V1 0x01000000
36 #define HEADER_VERSION_V2 0x02000000
41 char *file_name
; /* name of the file */
42 uint32_t file_size
; /* length of the file */
46 uint32_t version
; /* header version */
49 uint32_t hw_id
; /* hardware id */
50 uint32_t hw_rev
; /* hardware revision */
51 uint32_t region_code
; /* region code */
52 uint8_t md5sum1
[MD5SUM_LEN
];
54 uint8_t md5sum2
[MD5SUM_LEN
];
56 uint32_t kernel_la
; /* kernel load address */
57 uint32_t kernel_ep
; /* kernel entry point */
58 uint32_t fw_length
; /* total length of the firmware */
59 uint32_t kernel_ofs
; /* kernel data offset */
60 uint32_t kernel_len
; /* kernel data length */
61 uint32_t rootfs_ofs
; /* rootfs data offset */
62 uint32_t rootfs_len
; /* rootfs data length */
63 uint32_t boot_ofs
; /* bootloader data offset */
64 uint32_t boot_len
; /* bootloader data length */
72 } __attribute__ ((packed
));
92 static char *progname
;
93 static char *vendor
= "TP-LINK Technologies";
94 static char *version
= "ver. 1.0";
95 static char *fw_ver
= "0.0.0";
96 static uint32_t hdr_ver
= HEADER_VERSION_V1
;
98 static char *layout_id
;
99 static struct flash_layout
*layout
;
100 static char *opt_hw_id
;
101 static uint32_t hw_id
;
102 static char *opt_hw_rev
;
103 static uint32_t hw_rev
;
104 static uint32_t opt_hdr_ver
= 1;
105 static char *country
;
106 static const struct fw_region
*region
;
107 static int fw_ver_lo
;
108 static int fw_ver_mid
;
109 static int fw_ver_hi
;
110 static struct file_info kernel_info
;
111 static uint32_t kernel_la
= 0;
112 static uint32_t kernel_ep
= 0;
113 static uint32_t kernel_len
= 0;
114 static struct file_info rootfs_info
;
115 static uint32_t rootfs_ofs
= 0;
116 static uint32_t rootfs_align
;
117 static struct file_info boot_info
;
119 static int strip_padding
;
120 static int add_jffs2_eof
;
121 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
122 static uint32_t fw_max_len
;
123 static uint32_t reserved_space
;
125 static struct file_info inspect_info
;
126 static int extract
= 0;
127 static bool endian_swap
= false;
129 static const char md5salt_normal
[MD5SUM_LEN
] = {
130 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
131 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
134 static const char md5salt_boot
[MD5SUM_LEN
] = {
135 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
136 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
139 static struct flash_layout layouts
[] = {
142 .fw_max_len
= 0x3c0000,
143 .kernel_la
= 0x80060000,
144 .kernel_ep
= 0x80060000,
145 .rootfs_ofs
= 0x140000,
148 .fw_max_len
= 0x3c0000,
149 .kernel_la
= 0x80060000,
150 .kernel_ep
= 0x80060000,
151 .rootfs_ofs
= 0x100000,
154 .fw_max_len
= 0x7c0000,
155 .kernel_la
= 0x80060000,
156 .kernel_ep
= 0x80060000,
157 .rootfs_ofs
= 0x140000,
160 .fw_max_len
= 0x7c0000,
161 .kernel_la
= 0x80060000,
162 .kernel_ep
= 0x80060000,
163 .rootfs_ofs
= 0x100000,
166 .fw_max_len
= 0xf80000,
167 .kernel_la
= 0x80060000,
168 .kernel_ep
= 0x80060000,
169 .rootfs_ofs
= 0x140000,
172 .fw_max_len
= 0xf80000,
173 .kernel_la
= 0x80060000,
174 .kernel_ep
= 0x80060000,
175 .rootfs_ofs
= 0x100000,
178 .fw_max_len
= 0xf80000,
179 .kernel_la
= 0x00000000 ,
180 .kernel_ep
= 0xc0000000,
181 .rootfs_ofs
= 0x2a0000,
183 /* terminating entry */
187 static const struct fw_region regions
[] = {
188 /* Default region (universal) uses code 0 as well */
196 #define ERR(fmt, ...) do { \
198 fprintf(stderr, "[%s] *** error: " fmt "\n", \
199 progname, ## __VA_ARGS__ ); \
202 #define ERRS(fmt, ...) do { \
205 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
206 progname, ## __VA_ARGS__, strerror(save)); \
209 #define DBG(fmt, ...) do { \
210 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
213 static struct flash_layout
*find_layout(const char *id
)
215 struct flash_layout
*ret
;
216 struct flash_layout
*l
;
219 for (l
= layouts
; l
->id
!= NULL
; l
++){
220 if (strcasecmp(id
, l
->id
) == 0) {
229 static const struct fw_region
* find_region(const char *country
) {
232 for (i
= 0; i
< ARRAY_SIZE(regions
); i
++) {
233 if (strcasecmp(regions
[i
].name
, country
) == 0)
240 static void usage(int status
)
242 fprintf(stderr
, "Usage: %s [OPTIONS...]\n", progname
);
246 " -c use combined kernel image\n"
247 " -e swap endianness in kernel load address and entry point\n"
248 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
249 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
250 " -H <hwid> use hardware id specified with <hwid>\n"
251 " -W <hwrev> use hardware revision specified with <hwrev>\n"
252 " -C <country> set region code to <country>\n"
253 " -F <id> use flash layout specified with <id>\n"
254 " -k <file> read kernel image from the file <file>\n"
255 " -r <file> read rootfs image from the file <file>\n"
256 " -a <align> align the rootfs start on an <align> bytes boundary\n"
257 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
258 " -o <file> write output to the file <file>\n"
259 " -s strip padding from the end of the image\n"
260 " -j add jffs2 end-of-filesystem markers\n"
261 " -N <vendor> set image vendor to <vendor>\n"
262 " -V <version> set image version to <version>\n"
263 " -v <version> set firmware version to <version>\n"
264 " -m <version> set header version to <version>\n"
265 " -i <file> inspect given firmware file <file>\n"
266 " -x extract kernel and rootfs while inspecting (requires -i)\n"
267 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
268 " -h show this screen\n"
274 static void get_md5(const char *data
, int size
, uint8_t *md5
)
279 MD5_Update(&ctx
, data
, size
);
280 MD5_Final(md5
, &ctx
);
283 static int get_file_stat(struct file_info
*fdata
)
288 if (fdata
->file_name
== NULL
)
291 res
= stat(fdata
->file_name
, &st
);
293 ERRS("stat failed on %s", fdata
->file_name
);
297 fdata
->file_size
= st
.st_size
;
301 static int read_to_buf(const struct file_info
*fdata
, char *buf
)
304 int ret
= EXIT_FAILURE
;
306 f
= fopen(fdata
->file_name
, "r");
308 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
313 fread(buf
, fdata
->file_size
, 1, f
);
315 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
327 static int check_options(void)
332 if (inspect_info
.file_name
) {
333 ret
= get_file_stat(&inspect_info
);
338 } else if (extract
) {
339 ERR("no firmware for inspection specified");
343 if (opt_hw_id
== NULL
) {
344 ERR("hardware id not specified");
347 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
349 if (!combined
&& layout_id
== NULL
) {
350 ERR("flash layout is not specified");
355 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
360 region
= find_region(country
);
362 ERR("unknown region code \"%s\"", country
);
368 if (!kernel_la
|| !kernel_ep
) {
369 ERR("kernel loading address and entry point must be specified for combined image");
373 layout
= find_layout(layout_id
);
374 if (layout
== NULL
) {
375 ERR("unknown flash layout \"%s\"", layout_id
);
380 kernel_la
= layout
->kernel_la
;
382 kernel_ep
= layout
->kernel_ep
;
384 rootfs_ofs
= layout
->rootfs_ofs
;
386 if (reserved_space
> layout
->fw_max_len
) {
387 ERR("reserved space is not valid");
392 if (kernel_info
.file_name
== NULL
) {
393 ERR("no kernel image specified");
397 ret
= get_file_stat(&kernel_info
);
401 kernel_len
= kernel_info
.file_size
;
404 fw_max_len
= layout
->fw_max_len
- reserved_space
;
406 if (rootfs_info
.file_name
== NULL
) {
407 ERR("no rootfs image specified");
411 ret
= get_file_stat(&rootfs_info
);
416 kernel_len
+= sizeof(struct fw_header
);
417 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
418 kernel_len
-= sizeof(struct fw_header
);
420 DBG("kernel length aligned to %u", kernel_len
);
422 exceed_bytes
= kernel_len
+ rootfs_info
.file_size
- (fw_max_len
- sizeof(struct fw_header
));
423 if (exceed_bytes
> 0) {
424 ERR("images are too big by %i bytes", exceed_bytes
);
428 exceed_bytes
= kernel_info
.file_size
- (rootfs_ofs
- sizeof(struct fw_header
));
429 if (exceed_bytes
> 0) {
430 ERR("kernel image is too big by %i bytes", exceed_bytes
);
434 exceed_bytes
= rootfs_info
.file_size
- (fw_max_len
- rootfs_ofs
);
435 if (exceed_bytes
> 0) {
436 ERR("rootfs image is too big by %i bytes", exceed_bytes
);
442 if (ofname
== NULL
) {
443 ERR("no output file specified");
447 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
449 ERR("invalid firmware version '%s'", fw_ver
);
453 if (opt_hdr_ver
== 1) {
454 hdr_ver
= HEADER_VERSION_V1
;
455 } else if (opt_hdr_ver
== 2) {
456 hdr_ver
= HEADER_VERSION_V2
;
458 ERR("invalid header version '%u'", opt_hdr_ver
);
465 static void fill_header(char *buf
, int len
)
467 struct fw_header
*hdr
= (struct fw_header
*)buf
;
469 memset(hdr
, 0, sizeof(struct fw_header
));
471 hdr
->version
= htonl(hdr_ver
);
472 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
473 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
474 hdr
->hw_id
= htonl(hw_id
);
475 hdr
->hw_rev
= htonl(hw_rev
);
477 hdr
->kernel_la
= htonl(kernel_la
);
478 hdr
->kernel_ep
= htonl(kernel_ep
);
479 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
480 hdr
->kernel_len
= htonl(kernel_len
);
483 if (boot_info
.file_size
== 0)
484 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
486 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
488 hdr
->fw_length
= htonl(layout
->fw_max_len
);
489 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
490 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
493 hdr
->ver_hi
= htons(fw_ver_hi
);
494 hdr
->ver_mid
= htons(fw_ver_mid
);
495 hdr
->ver_lo
= htons(fw_ver_lo
);
498 hdr
->region_code
= htonl(region
->code
);
500 hdr
->region_str1
, sizeof(hdr
->region_str1
), "00000000;%02X%02X%02X%02X;",
501 region
->name
[0], region
->name
[1], region
->name
[2], region
->name
[3]
504 hdr
->region_str2
, sizeof(hdr
->region_str2
), "%02X%02X%02X%02X",
505 region
->name
[0], region
->name
[1], region
->name
[2], region
->name
[3]
510 hdr
->kernel_la
= bswap_32(hdr
->kernel_la
);
511 hdr
->kernel_ep
= bswap_32(hdr
->kernel_ep
);
515 get_md5(buf
, len
, hdr
->md5sum1
);
518 static int pad_jffs2(char *buf
, int currlen
)
524 pad_mask
= (64 * 1024);
525 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
529 for (i
= 10; i
< 32; i
++) {
535 len
= ALIGN(len
, mask
);
537 for (i
= 10; i
< 32; i
++) {
539 if ((len
& (mask
- 1)) == 0)
543 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
544 buf
[len
+ i
] = jffs2_eof_mark
[i
];
546 len
+= sizeof(jffs2_eof_mark
);
552 static int write_fw(const char *data
, int len
)
555 int ret
= EXIT_FAILURE
;
557 f
= fopen(ofname
, "w");
559 ERRS("could not open \"%s\" for writing", ofname
);
564 fwrite(data
, len
, 1, f
);
566 ERRS("unable to write output file");
570 DBG("firmware file \"%s\" completed", ofname
);
577 if (ret
!= EXIT_SUCCESS
) {
584 static int build_fw(void)
589 int ret
= EXIT_FAILURE
;
592 writelen
= sizeof(struct fw_header
) + kernel_len
;
597 buflen
= layout
->fw_max_len
;
599 buf
= malloc(buflen
);
601 ERR("no memory for buffer\n");
605 memset(buf
, 0xff, buflen
);
606 p
= buf
+ sizeof(struct fw_header
);
607 ret
= read_to_buf(&kernel_info
, p
);
616 p
= buf
+ rootfs_ofs
;
618 ret
= read_to_buf(&rootfs_info
, p
);
623 writelen
+= rootfs_info
.file_size
;
625 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
628 writelen
= pad_jffs2(buf
, writelen
);
634 fill_header(buf
, writelen
);
635 ret
= write_fw(buf
, writelen
);
647 /* Helper functions to inspect_fw() representing different output formats */
648 static inline void inspect_fw_pstr(const char *label
, const char *str
)
650 printf("%-23s: %s\n", label
, str
);
653 static inline void inspect_fw_phex(const char *label
, uint32_t val
)
655 printf("%-23s: 0x%08x\n", label
, val
);
658 static inline void inspect_fw_phexdec(const char *label
, uint32_t val
)
660 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
663 static inline void inspect_fw_pmd5sum(const char *label
, const uint8_t *val
, const char *text
)
667 printf("%-23s:", label
);
668 for (i
=0; i
<MD5SUM_LEN
; i
++)
669 printf(" %02x", val
[i
]);
670 printf(" %s\n", text
);
673 static int inspect_fw(void)
676 struct fw_header
*hdr
;
677 uint8_t md5sum
[MD5SUM_LEN
];
678 int ret
= EXIT_FAILURE
;
680 buf
= malloc(inspect_info
.file_size
);
682 ERR("no memory for buffer!\n");
686 ret
= read_to_buf(&inspect_info
, buf
);
689 hdr
= (struct fw_header
*)buf
;
691 inspect_fw_pstr("File name", inspect_info
.file_name
);
692 inspect_fw_phexdec("File size", inspect_info
.file_size
);
694 if ((ntohl(hdr
->version
) != HEADER_VERSION_V1
) &&
695 (ntohl(hdr
->version
) != HEADER_VERSION_V2
)) {
696 ERR("file does not seem to have V1/V2 header!\n");
700 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
702 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
703 if (ntohl(hdr
->boot_len
) == 0)
704 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
706 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
707 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
709 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
710 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
711 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
713 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
715 if (ntohl(hdr
->unk2
) != 0)
716 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
717 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
718 "(purpose yet unknown, unchecked here)");
719 if (ntohl(hdr
->unk3
) != 0)
720 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
724 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
725 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
726 inspect_fw_phex("Hardware ID", ntohl(hdr
->hw_id
));
727 inspect_fw_phex("Hardware Revision", ntohl(hdr
->hw_rev
));
728 inspect_fw_phex("Region code", ntohl(hdr
->region_code
));
732 inspect_fw_phexdec("Kernel data offset",
733 ntohl(hdr
->kernel_ofs
));
734 inspect_fw_phexdec("Kernel data length",
735 ntohl(hdr
->kernel_len
));
736 inspect_fw_phex("Kernel load address",
737 ntohl(hdr
->kernel_la
));
738 inspect_fw_phex("Kernel entry point",
739 ntohl(hdr
->kernel_ep
));
740 inspect_fw_phexdec("Rootfs data offset",
741 ntohl(hdr
->rootfs_ofs
));
742 inspect_fw_phexdec("Rootfs data length",
743 ntohl(hdr
->rootfs_len
));
744 inspect_fw_phexdec("Boot loader data offset",
745 ntohl(hdr
->boot_ofs
));
746 inspect_fw_phexdec("Boot loader data length",
747 ntohl(hdr
->boot_len
));
748 inspect_fw_phexdec("Total firmware length",
749 ntohl(hdr
->fw_length
));
757 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
758 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
759 printf("Extracting kernel to \"%s\"...\n", filename
);
760 fp
= fopen(filename
, "w");
762 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
763 ntohl(hdr
->kernel_len
), 1, fp
)) {
764 ERR("error in fwrite(): %s", strerror(errno
));
768 ERR("error in fopen(): %s", strerror(errno
));
772 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
773 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
774 printf("Extracting rootfs to \"%s\"...\n", filename
);
775 fp
= fopen(filename
, "w");
777 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
778 ntohl(hdr
->rootfs_len
), 1, fp
)) {
779 ERR("error in fwrite(): %s", strerror(errno
));
783 ERR("error in fopen(): %s", strerror(errno
));
794 int main(int argc
, char *argv
[])
796 int ret
= EXIT_FAILURE
;
798 progname
= basename(argv
[0]);
803 c
= getopt(argc
, argv
, "a:H:E:F:L:m:V:N:W:C:ci:k:r:R:o:xX:ehsjv:");
809 sscanf(optarg
, "0x%x", &rootfs_align
);
815 sscanf(optarg
, "0x%x", &kernel_ep
);
827 sscanf(optarg
, "0x%x", &kernel_la
);
830 sscanf(optarg
, "%u", &opt_hdr_ver
);
845 kernel_info
.file_name
= optarg
;
848 rootfs_info
.file_name
= optarg
;
851 sscanf(optarg
, "0x%x", &rootfs_ofs
);
860 inspect_info
.file_name
= optarg
;
875 sscanf(optarg
, "0x%x", &reserved_space
);
883 ret
= check_options();
887 if (!inspect_info
.file_name
)