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)); })
31 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
33 #define HEADER_VERSION_V1 0x01000000
34 #define HEADER_VERSION_V2 0x02000000
39 char *file_name
; /* name of the file */
40 uint32_t file_size
; /* length of the file */
44 uint32_t version
; /* header version */
47 uint32_t hw_id
; /* hardware id */
48 uint32_t hw_rev
; /* hardware revision */
49 uint32_t region
; /* region code */
50 uint8_t md5sum1
[MD5SUM_LEN
];
52 uint8_t md5sum2
[MD5SUM_LEN
];
54 uint32_t kernel_la
; /* kernel load address */
55 uint32_t kernel_ep
; /* kernel entry point */
56 uint32_t fw_length
; /* total length of the firmware */
57 uint32_t kernel_ofs
; /* kernel data offset */
58 uint32_t kernel_len
; /* kernel data length */
59 uint32_t rootfs_ofs
; /* rootfs data offset */
60 uint32_t rootfs_len
; /* rootfs data length */
61 uint32_t boot_ofs
; /* bootloader data offset */
62 uint32_t boot_len
; /* bootloader data length */
67 } __attribute__ ((packed
));
81 static char *progname
;
82 static char *vendor
= "TP-LINK Technologies";
83 static char *version
= "ver. 1.0";
84 static char *fw_ver
= "0.0.0";
85 static uint32_t hdr_ver
= HEADER_VERSION_V1
;
87 static char *layout_id
;
88 static struct flash_layout
*layout
;
89 static char *opt_hw_id
;
90 static uint32_t hw_id
;
91 static char *opt_hw_rev
;
92 static uint32_t hw_rev
;
93 static uint32_t opt_hdr_ver
= 1;
95 static uint32_t region
;
97 static int fw_ver_mid
;
99 static struct file_info kernel_info
;
100 static uint32_t kernel_la
= 0;
101 static uint32_t kernel_ep
= 0;
102 static uint32_t kernel_len
= 0;
103 static struct file_info rootfs_info
;
104 static uint32_t rootfs_ofs
= 0;
105 static uint32_t rootfs_align
;
106 static struct file_info boot_info
;
108 static int strip_padding
;
109 static int ignore_size
;
110 static int add_jffs2_eof
;
111 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
112 static uint32_t fw_max_len
;
113 static uint32_t reserved_space
;
115 static struct file_info inspect_info
;
116 static int extract
= 0;
118 static const char md5salt_normal
[MD5SUM_LEN
] = {
119 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
120 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
123 static const char md5salt_boot
[MD5SUM_LEN
] = {
124 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
125 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
128 static struct flash_layout layouts
[] = {
131 .fw_max_len
= 0x3c0000,
132 .kernel_la
= 0x80060000,
133 .kernel_ep
= 0x80060000,
134 .rootfs_ofs
= 0x140000,
137 .fw_max_len
= 0x3c0000,
138 .kernel_la
= 0x80060000,
139 .kernel_ep
= 0x80060000,
140 .rootfs_ofs
= 0x100000,
143 .fw_max_len
= 0x7c0000,
144 .kernel_la
= 0x80060000,
145 .kernel_ep
= 0x80060000,
146 .rootfs_ofs
= 0x140000,
149 .fw_max_len
= 0x7c0000,
150 .kernel_la
= 0x80060000,
151 .kernel_ep
= 0x80060000,
152 .rootfs_ofs
= 0x100000,
155 .fw_max_len
= 0xf80000,
156 .kernel_la
= 0x80060000,
157 .kernel_ep
= 0x80060000,
158 .rootfs_ofs
= 0x140000,
161 .fw_max_len
= 0xf80000,
162 .kernel_la
= 0x80060000,
163 .kernel_ep
= 0x80060000,
164 .rootfs_ofs
= 0x100000,
167 .fw_max_len
= 0xf80000,
168 .kernel_la
= 0x00000000 ,
169 .kernel_ep
= 0xc0000000,
170 .rootfs_ofs
= 0x2a0000,
172 /* terminating entry */
176 static const char *const regions
[] = {
177 "UN", /* universal */
184 #define ERR(fmt, ...) do { \
186 fprintf(stderr, "[%s] *** error: " fmt "\n", \
187 progname, ## __VA_ARGS__ ); \
190 #define ERRS(fmt, ...) do { \
193 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
194 progname, ## __VA_ARGS__, strerror(save)); \
197 #define DBG(fmt, ...) do { \
198 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
201 static struct flash_layout
*find_layout(const char *id
)
203 struct flash_layout
*ret
;
204 struct flash_layout
*l
;
207 for (l
= layouts
; l
->id
!= NULL
; l
++){
208 if (strcasecmp(id
, l
->id
) == 0) {
217 static uint32_t find_region(const char *country
) {
220 for (i
= 0; i
< ARRAY_SIZE(regions
); i
++) {
221 if (strcasecmp(regions
[i
], country
) == 0)
228 static const char * get_region_country(uint32_t region
) {
229 if (region
< ARRAY_SIZE(regions
))
230 return regions
[region
];
235 static void usage(int status
)
237 fprintf(stderr
, "Usage: %s [OPTIONS...]\n", progname
);
241 " -c use combined kernel image\n"
242 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
243 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
244 " -H <hwid> use hardware id specified with <hwid>\n"
245 " -W <hwrev> use hardware revision specified with <hwrev>\n"
246 " -C <country> set region code to <country>\n"
247 " -F <id> use flash layout specified with <id>\n"
248 " -k <file> read kernel image from the file <file>\n"
249 " -r <file> read rootfs image from the file <file>\n"
250 " -a <align> align the rootfs start on an <align> bytes boundary\n"
251 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
252 " -o <file> write output to the file <file>\n"
253 " -s strip padding from the end of the image\n"
254 " -S ignore firmware size limit (only for combined images)\n"
255 " -j add jffs2 end-of-filesystem markers\n"
256 " -N <vendor> set image vendor to <vendor>\n"
257 " -V <version> set image version to <version>\n"
258 " -v <version> set firmware version to <version>\n"
259 " -m <version> set header version to <version>\n"
260 " -i <file> inspect given firmware file <file>\n"
261 " -x extract kernel and rootfs while inspecting (requires -i)\n"
262 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
263 " -h show this screen\n"
269 static void get_md5(const char *data
, int size
, uint8_t *md5
)
274 MD5_Update(&ctx
, data
, size
);
275 MD5_Final(md5
, &ctx
);
278 static int get_file_stat(struct file_info
*fdata
)
283 if (fdata
->file_name
== NULL
)
286 res
= stat(fdata
->file_name
, &st
);
288 ERRS("stat failed on %s", fdata
->file_name
);
292 fdata
->file_size
= st
.st_size
;
296 static int read_to_buf(const struct file_info
*fdata
, char *buf
)
299 int ret
= EXIT_FAILURE
;
301 f
= fopen(fdata
->file_name
, "r");
303 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
308 fread(buf
, fdata
->file_size
, 1, f
);
310 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
322 static int check_options(void)
327 if (inspect_info
.file_name
) {
328 ret
= get_file_stat(&inspect_info
);
333 } else if (extract
) {
334 ERR("no firmware for inspection specified");
338 if (opt_hw_id
== NULL
) {
339 ERR("hardware id not specified");
342 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
344 if (layout_id
== NULL
) {
345 ERR("flash layout is not specified");
350 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
355 region
= find_region(country
);
356 if (region
== (uint32_t)-1) {
358 region
= strtoul(country
, &end
, 0);
360 ERR("unknown region code \"%s\"", country
);
366 layout
= find_layout(layout_id
);
367 if (layout
== NULL
) {
368 ERR("unknown flash layout \"%s\"", layout_id
);
373 kernel_la
= layout
->kernel_la
;
375 kernel_ep
= layout
->kernel_ep
;
377 rootfs_ofs
= layout
->rootfs_ofs
;
379 if (reserved_space
> layout
->fw_max_len
) {
380 ERR("reserved space is not valid");
384 fw_max_len
= layout
->fw_max_len
- reserved_space
;
386 if (kernel_info
.file_name
== NULL
) {
387 ERR("no kernel image specified");
391 ret
= get_file_stat(&kernel_info
);
395 kernel_len
= kernel_info
.file_size
;
398 exceed_bytes
= kernel_info
.file_size
- (fw_max_len
- sizeof(struct fw_header
));
399 if (exceed_bytes
> 0) {
401 ERR("kernel image is too big by %i bytes", exceed_bytes
);
404 layout
->fw_max_len
= sizeof(struct fw_header
) +
405 kernel_info
.file_size
+
409 if (rootfs_info
.file_name
== NULL
) {
410 ERR("no rootfs image specified");
414 ret
= get_file_stat(&rootfs_info
);
419 kernel_len
+= sizeof(struct fw_header
);
420 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
421 kernel_len
-= sizeof(struct fw_header
);
423 DBG("kernel length aligned to %u", kernel_len
);
425 exceed_bytes
= kernel_len
+ rootfs_info
.file_size
- (fw_max_len
- sizeof(struct fw_header
));
426 if (exceed_bytes
> 0) {
427 ERR("images are too big by %i bytes", exceed_bytes
);
431 exceed_bytes
= kernel_info
.file_size
- (rootfs_ofs
- sizeof(struct fw_header
));
432 if (exceed_bytes
> 0) {
433 ERR("kernel image is too big by %i bytes", exceed_bytes
);
437 exceed_bytes
= rootfs_info
.file_size
- (fw_max_len
- rootfs_ofs
);
438 if (exceed_bytes
> 0) {
439 ERR("rootfs image is too big by %i bytes", exceed_bytes
);
445 if (ofname
== NULL
) {
446 ERR("no output file specified");
450 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
452 ERR("invalid firmware version '%s'", fw_ver
);
456 if (opt_hdr_ver
== 1) {
457 hdr_ver
= HEADER_VERSION_V1
;
458 } else if (opt_hdr_ver
== 2) {
459 hdr_ver
= HEADER_VERSION_V2
;
461 ERR("invalid header version '%u'", opt_hdr_ver
);
468 static void fill_header(char *buf
, int len
)
470 struct fw_header
*hdr
= (struct fw_header
*)buf
;
472 memset(hdr
, 0, sizeof(struct fw_header
));
474 hdr
->version
= htonl(hdr_ver
);
475 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
476 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
477 hdr
->hw_id
= htonl(hw_id
);
478 hdr
->hw_rev
= htonl(hw_rev
);
479 hdr
->region
= htonl(region
);
481 if (boot_info
.file_size
== 0)
482 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
484 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
486 hdr
->kernel_la
= htonl(kernel_la
);
487 hdr
->kernel_ep
= htonl(kernel_ep
);
488 hdr
->fw_length
= htonl(layout
->fw_max_len
);
489 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
490 hdr
->kernel_len
= htonl(kernel_len
);
492 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
493 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
496 hdr
->ver_hi
= htons(fw_ver_hi
);
497 hdr
->ver_mid
= htons(fw_ver_mid
);
498 hdr
->ver_lo
= htons(fw_ver_lo
);
500 get_md5(buf
, len
, hdr
->md5sum1
);
503 static int pad_jffs2(char *buf
, int currlen
)
509 pad_mask
= (64 * 1024);
510 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
514 for (i
= 10; i
< 32; i
++) {
520 len
= ALIGN(len
, mask
);
522 for (i
= 10; i
< 32; i
++) {
524 if ((len
& (mask
- 1)) == 0)
528 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
529 buf
[len
+ i
] = jffs2_eof_mark
[i
];
531 len
+= sizeof(jffs2_eof_mark
);
537 static int write_fw(const char *data
, int len
)
540 int ret
= EXIT_FAILURE
;
542 f
= fopen(ofname
, "w");
544 ERRS("could not open \"%s\" for writing", ofname
);
549 fwrite(data
, len
, 1, f
);
551 ERRS("unable to write output file");
555 DBG("firmware file \"%s\" completed", ofname
);
562 if (ret
!= EXIT_SUCCESS
) {
569 static int build_fw(void)
574 int ret
= EXIT_FAILURE
;
577 buflen
= layout
->fw_max_len
;
579 buf
= malloc(buflen
);
581 ERR("no memory for buffer\n");
585 memset(buf
, 0xff, buflen
);
586 p
= buf
+ sizeof(struct fw_header
);
587 ret
= read_to_buf(&kernel_info
, p
);
591 writelen
= sizeof(struct fw_header
) + kernel_len
;
597 p
= buf
+ rootfs_ofs
;
599 ret
= read_to_buf(&rootfs_info
, p
);
604 writelen
+= rootfs_info
.file_size
;
606 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
609 writelen
= pad_jffs2(buf
, writelen
);
615 fill_header(buf
, writelen
);
616 ret
= write_fw(buf
, writelen
);
628 /* Helper functions to inspect_fw() representing different output formats */
629 static inline void inspect_fw_pstr(const char *label
, const char *str
)
631 printf("%-23s: %s\n", label
, str
);
634 static inline void inspect_fw_phex(const char *label
, uint32_t val
)
636 printf("%-23s: 0x%08x\n", label
, val
);
639 static inline void inspect_fw_phexpost(const char *label
, uint32_t val
, const char *post
)
641 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
644 static inline void inspect_fw_phexdec(const char *label
, uint32_t val
)
646 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
649 static inline void inspect_fw_pmd5sum(const char *label
, const uint8_t *val
, const char *text
)
653 printf("%-23s:", label
);
654 for (i
=0; i
<MD5SUM_LEN
; i
++)
655 printf(" %02x", val
[i
]);
656 printf(" %s\n", text
);
659 static int inspect_fw(void)
662 struct fw_header
*hdr
;
663 uint8_t md5sum
[MD5SUM_LEN
];
664 int ret
= EXIT_FAILURE
;
666 buf
= malloc(inspect_info
.file_size
);
668 ERR("no memory for buffer!\n");
672 ret
= read_to_buf(&inspect_info
, buf
);
675 hdr
= (struct fw_header
*)buf
;
677 inspect_fw_pstr("File name", inspect_info
.file_name
);
678 inspect_fw_phexdec("File size", inspect_info
.file_size
);
680 if ((ntohl(hdr
->version
) != HEADER_VERSION_V1
) &&
681 (ntohl(hdr
->version
) != HEADER_VERSION_V2
)) {
682 ERR("file does not seem to have V1/V2 header!\n");
686 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
688 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
689 if (ntohl(hdr
->boot_len
) == 0)
690 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
692 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
693 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
695 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
696 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
697 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
699 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
701 if (ntohl(hdr
->unk2
) != 0)
702 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
703 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
704 "(purpose yet unknown, unchecked here)");
705 if (ntohl(hdr
->unk3
) != 0)
706 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
710 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
711 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
712 inspect_fw_phex("Hardware ID", ntohl(hdr
->hw_id
));
713 inspect_fw_phex("Hardware Revision", ntohl(hdr
->hw_rev
));
714 inspect_fw_phexpost("Region code", ntohl(hdr
->region
), get_region_country(ntohl(hdr
->region
)));
718 inspect_fw_phexdec("Kernel data offset",
719 ntohl(hdr
->kernel_ofs
));
720 inspect_fw_phexdec("Kernel data length",
721 ntohl(hdr
->kernel_len
));
722 inspect_fw_phex("Kernel load address",
723 ntohl(hdr
->kernel_la
));
724 inspect_fw_phex("Kernel entry point",
725 ntohl(hdr
->kernel_ep
));
726 inspect_fw_phexdec("Rootfs data offset",
727 ntohl(hdr
->rootfs_ofs
));
728 inspect_fw_phexdec("Rootfs data length",
729 ntohl(hdr
->rootfs_len
));
730 inspect_fw_phexdec("Boot loader data offset",
731 ntohl(hdr
->boot_ofs
));
732 inspect_fw_phexdec("Boot loader data length",
733 ntohl(hdr
->boot_len
));
734 inspect_fw_phexdec("Total firmware length",
735 ntohl(hdr
->fw_length
));
743 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
744 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
745 printf("Extracting kernel to \"%s\"...\n", filename
);
746 fp
= fopen(filename
, "w");
748 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
749 ntohl(hdr
->kernel_len
), 1, fp
)) {
750 ERR("error in fwrite(): %s", strerror(errno
));
754 ERR("error in fopen(): %s", strerror(errno
));
758 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
759 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
760 printf("Extracting rootfs to \"%s\"...\n", filename
);
761 fp
= fopen(filename
, "w");
763 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
764 ntohl(hdr
->rootfs_len
), 1, fp
)) {
765 ERR("error in fwrite(): %s", strerror(errno
));
769 ERR("error in fopen(): %s", strerror(errno
));
780 int main(int argc
, char *argv
[])
782 int ret
= EXIT_FAILURE
;
784 progname
= basename(argv
[0]);
789 c
= getopt(argc
, argv
, "a:H:E:F:L:m:V:N:W:C:ci:k:r:R:o:xX:hsSjv:");
795 sscanf(optarg
, "0x%x", &rootfs_align
);
801 sscanf(optarg
, "0x%x", &kernel_ep
);
813 sscanf(optarg
, "0x%x", &kernel_la
);
816 sscanf(optarg
, "%u", &opt_hdr_ver
);
831 kernel_info
.file_name
= optarg
;
834 rootfs_info
.file_name
= optarg
;
837 sscanf(optarg
, "0x%x", &rootfs_ofs
);
849 inspect_info
.file_name
= optarg
;
861 sscanf(optarg
, "0x%x", &reserved_space
);
869 ret
= check_options();
873 if (!inspect_info
.file_name
)