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_WR841N_V1_5 0x08410002
38 #define HWID_TL_WR841ND_V3 0x08410003
39 #define HWID_TL_WR841ND_V5 0x08410005
40 #define HWID_TL_WR941ND_V2 0x09410002
41 #define HWID_TL_WR941ND_V4 0x09410004
42 #define HWID_TL_WR1043ND_V1 0x10430001
47 char *file_name; /* name of the file */
48 uint32_t file_size; /* length of the file */
52 uint32_t version; /* header version */
55 uint32_t hw_id; /* hardware id */
56 uint32_t hw_rev; /* hardware revision */
58 uint8_t md5sum1[MD5SUM_LEN];
60 uint8_t md5sum2[MD5SUM_LEN];
62 uint32_t kernel_la; /* kernel load address */
63 uint32_t kernel_ep; /* kernel entry point */
64 uint32_t fw_length; /* total length of the firmware */
65 uint32_t kernel_ofs; /* kernel data offset */
66 uint32_t kernel_len; /* kernel data length */
67 uint32_t rootfs_ofs; /* rootfs data offset */
68 uint32_t rootfs_len; /* rootfs data length */
69 uint32_t boot_ofs; /* bootloader data offset */
70 uint32_t boot_len; /* bootloader data length */
72 } __attribute__ ((packed));
88 static char *progname;
89 static char *vendor = "TP-LINK Technologies";
90 static char *version = "ver. 1.0";
92 static char *board_id;
93 static struct board_info *board;
94 static struct file_info kernel_info;
95 static struct file_info rootfs_info;
96 static struct file_info boot_info;
98 static int strip_padding;
100 char md5salt_normal[MD5SUM_LEN] = {
101 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
102 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
105 char md5salt_boot[MD5SUM_LEN] = {
106 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
107 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
110 static struct board_info boards[] = {
112 .id = "TL-WR741NDv1",
113 .hw_id = HWID_TL_WR741ND_V1,
115 .fw_max_len = 0x3c0000,
116 .kernel_la = 0x80060000,
117 .kernel_ep = 0x80060000,
118 .rootfs_ofs = 0x140000,
120 .id = "TL-WR841Nv1.5",
121 .hw_id = HWID_TL_WR841N_V1_5,
123 .fw_max_len = 0x3c0000,
124 .kernel_la = 0x80060000,
125 .kernel_ep = 0x80060000,
126 .rootfs_ofs = 0x140000,
128 .id = "TL-WR841NDv3",
129 .hw_id = HWID_TL_WR841ND_V3,
131 .fw_max_len = 0x3c0000,
132 .kernel_la = 0x80060000,
133 .kernel_ep = 0x80060000,
134 .rootfs_ofs = 0x140000,
136 .id = "TL-WR841NDv5",
137 .hw_id = HWID_TL_WR841ND_V5,
139 .fw_max_len = 0x3c0000,
140 .kernel_la = 0x80060000,
141 .kernel_ep = 0x80060000,
142 .rootfs_ofs = 0x140000,
144 .id = "TL-WR941NDv2",
145 .hw_id = HWID_TL_WR941ND_V2,
147 .fw_max_len = 0x3c0000,
148 .kernel_la = 0x80060000,
149 .kernel_ep = 0x80060000,
150 .rootfs_ofs = 0x140000,
152 .id = "TL-WR941NDv4",
153 .hw_id = HWID_TL_WR941ND_V4,
155 .fw_max_len = 0x3c0000,
156 .kernel_la = 0x80060000,
157 .kernel_ep = 0x80060000,
158 .rootfs_ofs = 0x140000,
160 .id = "TL-WR1043NDv1",
161 .hw_id = HWID_TL_WR1043ND_V1,
163 .fw_max_len = 0x7c0000,
164 .kernel_la = 0x80060000,
165 .kernel_ep = 0x80060000,
166 .rootfs_ofs = 0x140000,
168 /* terminating entry */
175 #define ERR(fmt, ...) do { \
177 fprintf(stderr, "[%s] *** error: " fmt "\n", \
178 progname, ## __VA_ARGS__ ); \
181 #define ERRS(fmt, ...) do { \
184 fprintf(stderr, "[%s] *** error: " fmt "\n", \
185 progname, ## __VA_ARGS__, strerror(save)); \
188 #define DBG(fmt, ...) do { \
189 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
192 static struct board_info *find_board(char *id)
194 struct board_info *ret;
195 struct board_info *board;
198 for (board = boards; board->id != NULL; board++){
199 if (strcasecmp(id, board->id) == 0) {
208 static void usage(int status)
210 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
211 struct board_info *board;
213 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
217 " -B <board> create image for the board specified with <board>\n"
218 " -c use combined kernel image\n"
219 " -k <file> read kernel image from the file <file>\n"
220 " -r <file> read rootfs image from the file <file>\n"
221 " -o <file> write output to the file <file>\n"
222 " -s strip padding from the end of the image\n"
223 " -N <vendor> set image vendor to <vendor>\n"
224 " -V <version> set image version to <version>\n"
225 " -h show this screen\n"
231 static int get_md5(char *data, int size, char *md5)
236 MD5_Update(&ctx, data, size);
237 MD5_Final(md5, &ctx);
240 static int get_file_stat(struct file_info *fdata)
245 if (fdata->file_name == NULL)
248 res = stat(fdata->file_name, &st);
250 ERRS("stat failed on %s", fdata->file_name);
254 fdata->file_size = st.st_size;
258 static int read_to_buf(struct file_info *fdata, char *buf)
261 int ret = EXIT_FAILURE;
263 f = fopen(fdata->file_name, "r");
265 ERRS("could not open \"%s\" for reading", fdata->file_name);
270 fread(buf, fdata->file_size, 1, f);
272 ERRS("unable to read from file \"%s\"", fdata->file_name);
284 static int check_options(void)
288 if (board_id == NULL) {
289 ERR("no board specified");
293 board = find_board(board_id);
295 ERR("unknown/unsupported board id \"%s\"", board_id);
299 if (kernel_info.file_name == NULL) {
300 ERR("no kernel image specified");
304 ret = get_file_stat(&kernel_info);
309 if (kernel_info.file_size >
310 board->fw_max_len - sizeof(struct fw_header)) {
311 ERR("kernel image is too big");
315 if (kernel_info.file_size >
316 board->rootfs_ofs - sizeof(struct fw_header)) {
317 ERR("kernel image is too big");
320 if (rootfs_info.file_name == NULL) {
321 ERR("no rootfs image specified");
325 ret = get_file_stat(&rootfs_info);
329 if (rootfs_info.file_size >
330 (board->fw_max_len - board->rootfs_ofs)) {
331 ERR("rootfs image is too big");
336 if (ofname == NULL) {
337 ERR("no output file specified");
344 static void fill_header(char *buf, int len)
346 struct fw_header *hdr = (struct fw_header *)buf;
348 memset(hdr, 0, sizeof(struct fw_header));
350 hdr->version = HOST_TO_BE32(HEADER_VERSION_V1);
351 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
352 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
353 hdr->hw_id = HOST_TO_BE32(board->hw_id);
354 hdr->hw_rev = HOST_TO_BE32(board->hw_rev);
356 if (boot_info.file_size == 0)
357 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
359 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
361 hdr->kernel_la = HOST_TO_BE32(board->kernel_la);
362 hdr->kernel_ep = HOST_TO_BE32(board->kernel_ep);
363 hdr->fw_length = HOST_TO_BE32(board->fw_max_len);
364 hdr->kernel_ofs = HOST_TO_BE32(sizeof(struct fw_header));
365 hdr->kernel_len = HOST_TO_BE32(kernel_info.file_size);
367 hdr->rootfs_ofs = HOST_TO_BE32(board->rootfs_ofs);
368 hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
371 get_md5(buf, len, hdr->md5sum1);
374 static int write_fw(char *data, int len)
377 int ret = EXIT_FAILURE;
379 f = fopen(ofname, "w");
381 ERRS("could not open \"%s\" for writing", ofname);
386 fwrite(data, len, 1, f);
388 ERRS("unable to write output file");
392 DBG("firmware file \"%s\" completed", ofname);
399 if (ret != EXIT_SUCCESS) {
406 static int build_fw(void)
411 int ret = EXIT_FAILURE;
414 buflen = board->fw_max_len;
416 buf = malloc(buflen);
418 ERR("no memory for buffer\n");
422 memset(buf, 0xff, buflen);
423 p = buf + sizeof(struct fw_header);
424 ret = read_to_buf(&kernel_info, p);
428 writelen = kernel_info.file_size;
431 p = buf + board->rootfs_ofs;
432 ret = read_to_buf(&rootfs_info, p);
436 writelen = board->rootfs_ofs + rootfs_info.file_size;
442 fill_header(buf, writelen);
443 ret = write_fw(buf, writelen);
455 int main(int argc, char *argv[])
457 int ret = EXIT_FAILURE;
462 progname = basename(argv[0]);
467 c = getopt(argc, argv, "B:V:N:ck:r:o:hs");
485 kernel_info.file_name = optarg;
488 rootfs_info.file_name = optarg;
505 ret = check_options();