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 #if (__BYTE_ORDER == __BIG_ENDIAN)
28 # define HOST_TO_BE32(x) (x)
29 # define BE32_TO_HOST(x) (x)
31 # define HOST_TO_BE32(x) bswap_32(x)
32 # define BE32_TO_HOST(x) bswap_32(x)
35 #define HEADER_VERSION_V1 0x01000000
36 #define HWID_TL_WR741ND_V1 0x07410001
37 #define HWID_TL_WR841ND_V3 0x08410003
38 #define HWID_TL_WR841ND_V5 0x08410005
39 #define HWID_TL_WR941ND_V2 0x09410002
40 #define HWID_TL_WR1043ND_V1 0x10430001
45 char *file_name
; /* name of the file */
46 uint32_t file_size
; /* length of the file */
50 uint32_t version
; /* header version */
53 uint32_t hw_id
; /* hardware id */
54 uint32_t hw_rev
; /* hardware revision */
56 uint8_t md5sum1
[MD5SUM_LEN
];
58 uint8_t md5sum2
[MD5SUM_LEN
];
60 uint32_t kernel_la
; /* kernel load address */
61 uint32_t kernel_ep
; /* kernel entry point */
62 uint32_t fw_length
; /* total length of the firmware */
63 uint32_t kernel_ofs
; /* kernel data offset */
64 uint32_t kernel_len
; /* kernel data length */
65 uint32_t rootfs_ofs
; /* rootfs data offset */
66 uint32_t rootfs_len
; /* rootfs data length */
67 uint32_t boot_ofs
; /* bootloader data offset */
68 uint32_t boot_len
; /* bootloader data length */
70 } __attribute__ ((packed
));
86 static char *progname
;
87 static char *vendor
= "TP-LINK Technologies";
88 static char *version
= "ver. 1.0";
90 static char *board_id
;
91 static struct board_info
*board
;
92 static struct file_info kernel_info
;
93 static struct file_info rootfs_info
;
94 static struct file_info boot_info
;
97 char md5salt_normal
[MD5SUM_LEN
] = {
98 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
99 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
102 char md5salt_boot
[MD5SUM_LEN
] = {
103 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
104 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
107 static struct board_info boards
[] = {
109 .id
= "TL-WR741NDv1",
110 .hw_id
= HWID_TL_WR741ND_V1
,
112 .fw_max_len
= 0x3c0000,
113 .kernel_la
= 0x80060000,
114 .kernel_ep
= 0x80060000,
115 .rootfs_ofs
= 0x140000,
117 .id
= "TL-WR841NDv3",
118 .hw_id
= HWID_TL_WR841ND_V3
,
120 .fw_max_len
= 0x3c0000,
121 .kernel_la
= 0x80060000,
122 .kernel_ep
= 0x80060000,
123 .rootfs_ofs
= 0x140000,
125 .id
= "TL-WR841NDv5",
126 .hw_id
= HWID_TL_WR841ND_V5
,
128 .fw_max_len
= 0x3c0000,
129 .kernel_la
= 0x80060000,
130 .kernel_ep
= 0x80060000,
131 .rootfs_ofs
= 0x140000,
133 .id
= "TL-WR941NDv2",
134 .hw_id
= HWID_TL_WR941ND_V2
,
136 .fw_max_len
= 0x3c0000,
137 .kernel_la
= 0x80060000,
138 .kernel_ep
= 0x80060000,
139 .rootfs_ofs
= 0x140000,
141 .id
= "TL-WR1043NDv1",
142 .hw_id
= HWID_TL_WR1043ND_V1
,
144 .fw_max_len
= 0x7c0000,
145 .kernel_la
= 0x80060000,
146 .kernel_ep
= 0x80060000,
147 .rootfs_ofs
= 0x140000,
149 /* terminating entry */
156 #define ERR(fmt, ...) do { \
158 fprintf(stderr, "[%s] *** error: " fmt "\n", \
159 progname, ## __VA_ARGS__ ); \
162 #define ERRS(fmt, ...) do { \
165 fprintf(stderr, "[%s] *** error: " fmt "\n", \
166 progname, ## __VA_ARGS__, strerror(save)); \
169 #define DBG(fmt, ...) do { \
170 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
173 static struct board_info
*find_board(char *id
)
175 struct board_info
*ret
;
176 struct board_info
*board
;
179 for (board
= boards
; board
->id
!= NULL
; board
++){
180 if (strcasecmp(id
, board
->id
) == 0) {
189 static void usage(int status
)
191 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
192 struct board_info
*board
;
194 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
198 " -B <board> create image for the board specified with <board>\n"
199 " -c use combined kernel image\n"
200 " -k <file> read kernel image from the file <file>\n"
201 " -r <file> read rootfs image from the file <file>\n"
202 " -o <file> write output to the file <file>\n"
203 " -v <version> set image version to <version>\n"
204 " -h show this screen\n"
210 static int get_md5(char *data
, int size
, char *md5
)
215 MD5_Update(&ctx
, data
, size
);
216 MD5_Final(md5
, &ctx
);
219 static int get_file_stat(struct file_info
*fdata
)
224 if (fdata
->file_name
== NULL
)
227 res
= stat(fdata
->file_name
, &st
);
229 ERRS("stat failed on %s", fdata
->file_name
);
233 fdata
->file_size
= st
.st_size
;
237 static int read_to_buf(struct file_info
*fdata
, char *buf
)
240 int ret
= EXIT_FAILURE
;
242 f
= fopen(fdata
->file_name
, "r");
244 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
249 fread(buf
, fdata
->file_size
, 1, f
);
251 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
263 static int check_options(void)
267 if (board_id
== NULL
) {
268 ERR("no board specified");
272 board
= find_board(board_id
);
274 ERR("unknown/unsupported board id \"%s\"", board_id
);
278 if (kernel_info
.file_name
== NULL
) {
279 ERR("no kernel image specified");
283 ret
= get_file_stat(&kernel_info
);
288 if (kernel_info
.file_size
>
289 board
->fw_max_len
- sizeof(struct fw_header
)) {
290 ERR("kernel image is too big");
294 if (kernel_info
.file_size
>
295 board
->rootfs_ofs
- sizeof(struct fw_header
)) {
296 ERR("kernel image is too big");
299 if (rootfs_info
.file_name
== NULL
) {
300 ERR("no rootfs image specified");
304 ret
= get_file_stat(&rootfs_info
);
308 if (rootfs_info
.file_size
>
309 (board
->fw_max_len
- board
->rootfs_ofs
)) {
310 ERR("rootfs image is too big");
315 if (ofname
== NULL
) {
316 ERR("no output file specified");
323 static void fill_header(char *buf
, int len
)
325 struct fw_header
*hdr
= (struct fw_header
*)buf
;
327 memset(hdr
, 0, sizeof(struct fw_header
));
329 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
330 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
331 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
332 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
333 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
335 if (boot_info
.file_size
== 0)
336 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
338 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
340 hdr
->kernel_la
= HOST_TO_BE32(board
->kernel_la
);
341 hdr
->kernel_ep
= HOST_TO_BE32(board
->kernel_ep
);
342 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
343 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
344 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
346 hdr
->rootfs_ofs
= HOST_TO_BE32(board
->rootfs_ofs
);
347 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
350 get_md5(buf
, len
, hdr
->md5sum1
);
353 static int write_fw(char *data
, int len
)
356 int ret
= EXIT_FAILURE
;
358 f
= fopen(ofname
, "w");
360 ERRS("could not open \"%s\" for writing", ofname
);
365 fwrite(data
, len
, 1, f
);
367 ERRS("unable to write output file");
371 DBG("firmware file \"%s\" completed", ofname
);
378 if (ret
!= EXIT_SUCCESS
) {
385 static int build_fw(void)
390 int ret
= EXIT_FAILURE
;
392 buflen
= board
->fw_max_len
;
394 buf
= malloc(buflen
);
396 ERR("no memory for buffer\n");
400 memset(buf
, 0xff, buflen
);
401 p
= buf
+ sizeof(struct fw_header
);
402 ret
= read_to_buf(&kernel_info
, p
);
407 p
= buf
+ board
->rootfs_ofs
;
408 ret
= read_to_buf(&rootfs_info
, p
);
413 fill_header(buf
, buflen
);
415 ret
= write_fw(buf
, buflen
);
427 int main(int argc
, char *argv
[])
429 int ret
= EXIT_FAILURE
;
434 progname
= basename(argv
[0]);
439 c
= getopt(argc
, argv
, "B:V:N:ck:r:o:v:h:");
457 kernel_info
.file_name
= optarg
;
460 rootfs_info
.file_name
= optarg
;
477 ret
= check_options();
This page took 0.061838 seconds and 5 git commands to generate.