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_WA901ND_V1 0x09010001
37 #define HWID_TL_WR741ND_V1 0x07410001
38 #define HWID_TL_WR841N_V1_5 0x08410002
39 #define HWID_TL_WR841ND_V3 0x08410003
40 #define HWID_TL_WR841ND_V5 0x08410005
41 #define HWID_TL_WR941ND_V2 0x09410002
42 #define HWID_TL_WR941ND_V4 0x09410004
43 #define HWID_TL_WR1043ND_V1 0x10430001
48 char *file_name
; /* name of the file */
49 uint32_t file_size
; /* length of the file */
53 uint32_t version
; /* header version */
56 uint32_t hw_id
; /* hardware id */
57 uint32_t hw_rev
; /* hardware revision */
59 uint8_t md5sum1
[MD5SUM_LEN
];
61 uint8_t md5sum2
[MD5SUM_LEN
];
63 uint32_t kernel_la
; /* kernel load address */
64 uint32_t kernel_ep
; /* kernel entry point */
65 uint32_t fw_length
; /* total length of the firmware */
66 uint32_t kernel_ofs
; /* kernel data offset */
67 uint32_t kernel_len
; /* kernel data length */
68 uint32_t rootfs_ofs
; /* rootfs data offset */
69 uint32_t rootfs_len
; /* rootfs data length */
70 uint32_t boot_ofs
; /* bootloader data offset */
71 uint32_t boot_len
; /* bootloader data length */
73 } __attribute__ ((packed
));
89 static char *progname
;
90 static char *vendor
= "TP-LINK Technologies";
91 static char *version
= "ver. 1.0";
93 static char *board_id
;
94 static struct board_info
*board
;
95 static struct file_info kernel_info
;
96 static struct file_info rootfs_info
;
97 static struct file_info boot_info
;
99 static int strip_padding
;
101 char md5salt_normal
[MD5SUM_LEN
] = {
102 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
103 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
106 char md5salt_boot
[MD5SUM_LEN
] = {
107 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
108 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
111 static struct board_info boards
[] = {
113 .id
= "TL-WA901NDv1",
114 .hw_id
= HWID_TL_WA901ND_V1
,
116 .fw_max_len
= 0x3c0000,
117 .kernel_la
= 0x80060000,
118 .kernel_ep
= 0x80060000,
119 .rootfs_ofs
= 0x140000,
121 .id
= "TL-WR741NDv1",
122 .hw_id
= HWID_TL_WR741ND_V1
,
124 .fw_max_len
= 0x3c0000,
125 .kernel_la
= 0x80060000,
126 .kernel_ep
= 0x80060000,
127 .rootfs_ofs
= 0x140000,
129 .id
= "TL-WR841Nv1.5",
130 .hw_id
= HWID_TL_WR841N_V1_5
,
132 .fw_max_len
= 0x3c0000,
133 .kernel_la
= 0x80060000,
134 .kernel_ep
= 0x80060000,
135 .rootfs_ofs
= 0x140000,
137 .id
= "TL-WR841NDv3",
138 .hw_id
= HWID_TL_WR841ND_V3
,
140 .fw_max_len
= 0x3c0000,
141 .kernel_la
= 0x80060000,
142 .kernel_ep
= 0x80060000,
143 .rootfs_ofs
= 0x140000,
145 .id
= "TL-WR841NDv5",
146 .hw_id
= HWID_TL_WR841ND_V5
,
148 .fw_max_len
= 0x3c0000,
149 .kernel_la
= 0x80060000,
150 .kernel_ep
= 0x80060000,
151 .rootfs_ofs
= 0x140000,
153 .id
= "TL-WR941NDv2",
154 .hw_id
= HWID_TL_WR941ND_V2
,
156 .fw_max_len
= 0x3c0000,
157 .kernel_la
= 0x80060000,
158 .kernel_ep
= 0x80060000,
159 .rootfs_ofs
= 0x140000,
161 .id
= "TL-WR941NDv4",
162 .hw_id
= HWID_TL_WR941ND_V4
,
164 .fw_max_len
= 0x3c0000,
165 .kernel_la
= 0x80060000,
166 .kernel_ep
= 0x80060000,
167 .rootfs_ofs
= 0x140000,
169 .id
= "TL-WR1043NDv1",
170 .hw_id
= HWID_TL_WR1043ND_V1
,
172 .fw_max_len
= 0x7c0000,
173 .kernel_la
= 0x80060000,
174 .kernel_ep
= 0x80060000,
175 .rootfs_ofs
= 0x140000,
177 /* terminating entry */
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 "\n", \
194 progname, ## __VA_ARGS__, strerror(save)); \
197 #define DBG(fmt, ...) do { \
198 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
201 static struct board_info
*find_board(char *id
)
203 struct board_info
*ret
;
204 struct board_info
*board
;
207 for (board
= boards
; board
->id
!= NULL
; board
++){
208 if (strcasecmp(id
, board
->id
) == 0) {
217 static void usage(int status
)
219 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
220 struct board_info
*board
;
222 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
226 " -B <board> create image for the board specified with <board>\n"
227 " -c use combined kernel image\n"
228 " -k <file> read kernel image from the file <file>\n"
229 " -r <file> read rootfs image from the file <file>\n"
230 " -o <file> write output to the file <file>\n"
231 " -s strip padding from the end of the image\n"
232 " -N <vendor> set image vendor to <vendor>\n"
233 " -V <version> set image version to <version>\n"
234 " -h show this screen\n"
240 static int get_md5(char *data
, int size
, char *md5
)
245 MD5_Update(&ctx
, data
, size
);
246 MD5_Final(md5
, &ctx
);
249 static int get_file_stat(struct file_info
*fdata
)
254 if (fdata
->file_name
== NULL
)
257 res
= stat(fdata
->file_name
, &st
);
259 ERRS("stat failed on %s", fdata
->file_name
);
263 fdata
->file_size
= st
.st_size
;
267 static int read_to_buf(struct file_info
*fdata
, char *buf
)
270 int ret
= EXIT_FAILURE
;
272 f
= fopen(fdata
->file_name
, "r");
274 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
279 fread(buf
, fdata
->file_size
, 1, f
);
281 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
293 static int check_options(void)
297 if (board_id
== NULL
) {
298 ERR("no board specified");
302 board
= find_board(board_id
);
304 ERR("unknown/unsupported board id \"%s\"", board_id
);
308 if (kernel_info
.file_name
== NULL
) {
309 ERR("no kernel image specified");
313 ret
= get_file_stat(&kernel_info
);
318 if (kernel_info
.file_size
>
319 board
->fw_max_len
- sizeof(struct fw_header
)) {
320 ERR("kernel image is too big");
324 if (kernel_info
.file_size
>
325 board
->rootfs_ofs
- sizeof(struct fw_header
)) {
326 ERR("kernel image is too big");
329 if (rootfs_info
.file_name
== NULL
) {
330 ERR("no rootfs image specified");
334 ret
= get_file_stat(&rootfs_info
);
338 if (rootfs_info
.file_size
>
339 (board
->fw_max_len
- board
->rootfs_ofs
)) {
340 ERR("rootfs image is too big");
345 if (ofname
== NULL
) {
346 ERR("no output file specified");
353 static void fill_header(char *buf
, int len
)
355 struct fw_header
*hdr
= (struct fw_header
*)buf
;
357 memset(hdr
, 0, sizeof(struct fw_header
));
359 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
360 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
361 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
362 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
363 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
365 if (boot_info
.file_size
== 0)
366 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
368 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
370 hdr
->kernel_la
= HOST_TO_BE32(board
->kernel_la
);
371 hdr
->kernel_ep
= HOST_TO_BE32(board
->kernel_ep
);
372 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
373 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
374 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
376 hdr
->rootfs_ofs
= HOST_TO_BE32(board
->rootfs_ofs
);
377 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
380 get_md5(buf
, len
, hdr
->md5sum1
);
383 static int write_fw(char *data
, int len
)
386 int ret
= EXIT_FAILURE
;
388 f
= fopen(ofname
, "w");
390 ERRS("could not open \"%s\" for writing", ofname
);
395 fwrite(data
, len
, 1, f
);
397 ERRS("unable to write output file");
401 DBG("firmware file \"%s\" completed", ofname
);
408 if (ret
!= EXIT_SUCCESS
) {
415 static int build_fw(void)
420 int ret
= EXIT_FAILURE
;
423 buflen
= board
->fw_max_len
;
425 buf
= malloc(buflen
);
427 ERR("no memory for buffer\n");
431 memset(buf
, 0xff, buflen
);
432 p
= buf
+ sizeof(struct fw_header
);
433 ret
= read_to_buf(&kernel_info
, p
);
437 writelen
= kernel_info
.file_size
;
440 p
= buf
+ board
->rootfs_ofs
;
441 ret
= read_to_buf(&rootfs_info
, p
);
445 writelen
= board
->rootfs_ofs
+ rootfs_info
.file_size
;
451 fill_header(buf
, writelen
);
452 ret
= write_fw(buf
, writelen
);
464 int main(int argc
, char *argv
[])
466 int ret
= EXIT_FAILURE
;
471 progname
= basename(argv
[0]);
476 c
= getopt(argc
, argv
, "B:V:N:ck:r:o:hs");
494 kernel_info
.file_name
= optarg
;
497 rootfs_info
.file_name
= optarg
;
514 ret
= check_options();
This page took 0.073378 seconds and 5 git commands to generate.