ar71xx: preliminary support for the TL-WR1043ND
[openwrt/openwrt.git] / tools / firmware-utils / src / mktplinkfw.c
1 /*
2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
3 *
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>
7 *
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.
11 *
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <unistd.h> /* for unlink() */
19 #include <libgen.h>
20 #include <getopt.h> /* for getopt() */
21 #include <stdarg.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24
25 #include "md5.h"
26
27 #if (__BYTE_ORDER == __BIG_ENDIAN)
28 # define HOST_TO_BE32(x) (x)
29 # define BE32_TO_HOST(x) (x)
30 #else
31 # define HOST_TO_BE32(x) bswap_32(x)
32 # define BE32_TO_HOST(x) bswap_32(x)
33 #endif
34
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
41
42 #define MD5SUM_LEN 16
43
44 struct file_info {
45 char *file_name; /* name of the file */
46 uint32_t file_size; /* length of the file */
47 };
48
49 struct fw_header {
50 uint32_t version; /* header version */
51 char vendor_name[24];
52 char fw_version[36];
53 uint32_t hw_id; /* hardware id */
54 uint32_t hw_rev; /* hardware revision */
55 uint32_t unk1;
56 uint8_t md5sum1[MD5SUM_LEN];
57 uint32_t unk2;
58 uint8_t md5sum2[MD5SUM_LEN];
59 uint32_t unk3;
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 */
69 uint8_t pad[360];
70 } __attribute__ ((packed));
71
72 struct board_info {
73 char *id;
74 uint32_t hw_id;
75 uint32_t hw_rev;
76 uint32_t fw_max_len;
77 uint32_t kernel_la;
78 uint32_t kernel_ep;
79 uint32_t rootfs_ofs;
80 };
81
82 /*
83 * Globals
84 */
85 static char *ofname;
86 static char *progname;
87 static char *vendor = "TP-LINK Technologies";
88 static char *version = "ver. 1.0";
89
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;
95 static int combined;
96
97 char md5salt_normal[MD5SUM_LEN] = {
98 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
99 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
100 };
101
102 char md5salt_boot[MD5SUM_LEN] = {
103 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
104 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
105 };
106
107 static struct board_info boards[] = {
108 {
109 .id = "TL-WR741NDv1",
110 .hw_id = HWID_TL_WR741ND_V1,
111 .hw_rev = 1,
112 .fw_max_len = 0x3c0000,
113 .kernel_la = 0x80060000,
114 .kernel_ep = 0x80060000,
115 .rootfs_ofs = 0x140000,
116 }, {
117 .id = "TL-WR841NDv3",
118 .hw_id = HWID_TL_WR841ND_V3,
119 .hw_rev = 3,
120 .fw_max_len = 0x3c0000,
121 .kernel_la = 0x80060000,
122 .kernel_ep = 0x80060000,
123 .rootfs_ofs = 0x140000,
124 }, {
125 .id = "TL-WR841NDv5",
126 .hw_id = HWID_TL_WR841ND_V5,
127 .hw_rev = 1,
128 .fw_max_len = 0x3c0000,
129 .kernel_la = 0x80060000,
130 .kernel_ep = 0x80060000,
131 .rootfs_ofs = 0x140000,
132 }, {
133 .id = "TL-WR941NDv2",
134 .hw_id = HWID_TL_WR941ND_V2,
135 .hw_rev = 2,
136 .fw_max_len = 0x3c0000,
137 .kernel_la = 0x80060000,
138 .kernel_ep = 0x80060000,
139 .rootfs_ofs = 0x140000,
140 }, {
141 .id = "TL-WR1043NDv1",
142 .hw_id = HWID_TL_WR1043ND_V1,
143 .hw_rev = 1,
144 .fw_max_len = 0x7c0000,
145 .kernel_la = 0x80060000,
146 .kernel_ep = 0x80060000,
147 .rootfs_ofs = 0x140000,
148 }, {
149 /* terminating entry */
150 }
151 };
152
153 /*
154 * Message macros
155 */
156 #define ERR(fmt, ...) do { \
157 fflush(0); \
158 fprintf(stderr, "[%s] *** error: " fmt "\n", \
159 progname, ## __VA_ARGS__ ); \
160 } while (0)
161
162 #define ERRS(fmt, ...) do { \
163 int save = errno; \
164 fflush(0); \
165 fprintf(stderr, "[%s] *** error: " fmt "\n", \
166 progname, ## __VA_ARGS__, strerror(save)); \
167 } while (0)
168
169 #define DBG(fmt, ...) do { \
170 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
171 } while (0)
172
173 static struct board_info *find_board(char *id)
174 {
175 struct board_info *ret;
176 struct board_info *board;
177
178 ret = NULL;
179 for (board = boards; board->id != NULL; board++){
180 if (strcasecmp(id, board->id) == 0) {
181 ret = board;
182 break;
183 }
184 };
185
186 return ret;
187 }
188
189 static void usage(int status)
190 {
191 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
192 struct board_info *board;
193
194 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
195 fprintf(stream,
196 "\n"
197 "Options:\n"
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"
205 );
206
207 exit(status);
208 }
209
210 static int get_md5(char *data, int size, char *md5)
211 {
212 MD5_CTX ctx;
213
214 MD5_Init(&ctx);
215 MD5_Update(&ctx, data, size);
216 MD5_Final(md5, &ctx);
217 }
218
219 static int get_file_stat(struct file_info *fdata)
220 {
221 struct stat st;
222 int res;
223
224 if (fdata->file_name == NULL)
225 return 0;
226
227 res = stat(fdata->file_name, &st);
228 if (res){
229 ERRS("stat failed on %s", fdata->file_name);
230 return res;
231 }
232
233 fdata->file_size = st.st_size;
234 return 0;
235 }
236
237 static int read_to_buf(struct file_info *fdata, char *buf)
238 {
239 FILE *f;
240 int ret = EXIT_FAILURE;
241
242 f = fopen(fdata->file_name, "r");
243 if (f == NULL) {
244 ERRS("could not open \"%s\" for reading", fdata->file_name);
245 goto out;
246 }
247
248 errno = 0;
249 fread(buf, fdata->file_size, 1, f);
250 if (errno != 0) {
251 ERRS("unable to read from file \"%s\"", fdata->file_name);
252 goto out_close;
253 }
254
255 ret = EXIT_SUCCESS;
256
257 out_close:
258 fclose(f);
259 out:
260 return ret;
261 }
262
263 static int check_options(void)
264 {
265 int ret;
266
267 if (board_id == NULL) {
268 ERR("no board specified");
269 return -1;
270 }
271
272 board = find_board(board_id);
273 if (board == NULL) {
274 ERR("unknown/unsupported board id \"%s\"", board_id);
275 return -1;
276 }
277
278 if (kernel_info.file_name == NULL) {
279 ERR("no kernel image specified");
280 return -1;
281 }
282
283 ret = get_file_stat(&kernel_info);
284 if (ret)
285 return ret;
286
287 if (combined) {
288 if (kernel_info.file_size >
289 board->fw_max_len - sizeof(struct fw_header)) {
290 ERR("kernel image is too big");
291 return -1;
292 }
293 } else {
294 if (kernel_info.file_size >
295 board->rootfs_ofs - sizeof(struct fw_header)) {
296 ERR("kernel image is too big");
297 return -1;
298 }
299 if (rootfs_info.file_name == NULL) {
300 ERR("no rootfs image specified");
301 return -1;
302 }
303
304 ret = get_file_stat(&rootfs_info);
305 if (ret)
306 return ret;
307
308 if (rootfs_info.file_size >
309 (board->fw_max_len - board->rootfs_ofs)) {
310 ERR("rootfs image is too big");
311 return -1;
312 }
313 }
314
315 if (ofname == NULL) {
316 ERR("no output file specified");
317 return -1;
318 }
319
320 return 0;
321 }
322
323 static void fill_header(char *buf, int len)
324 {
325 struct fw_header *hdr = (struct fw_header *)buf;
326
327 memset(hdr, 0, sizeof(struct fw_header));
328
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);
334
335 if (boot_info.file_size == 0)
336 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
337 else
338 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
339
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);
345 if (!combined) {
346 hdr->rootfs_ofs = HOST_TO_BE32(board->rootfs_ofs);
347 hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
348 }
349
350 get_md5(buf, len, hdr->md5sum1);
351 }
352
353 static int write_fw(char *data, int len)
354 {
355 FILE *f;
356 int ret = EXIT_FAILURE;
357
358 f = fopen(ofname, "w");
359 if (f == NULL) {
360 ERRS("could not open \"%s\" for writing", ofname);
361 goto out;
362 }
363
364 errno = 0;
365 fwrite(data, len, 1, f);
366 if (errno) {
367 ERRS("unable to write output file");
368 goto out_flush;
369 }
370
371 DBG("firmware file \"%s\" completed", ofname);
372
373 ret = EXIT_SUCCESS;
374
375 out_flush:
376 fflush(f);
377 fclose(f);
378 if (ret != EXIT_SUCCESS) {
379 unlink(ofname);
380 }
381 out:
382 return ret;
383 }
384
385 static int build_fw(void)
386 {
387 int buflen;
388 char *buf;
389 char *p;
390 int ret = EXIT_FAILURE;
391
392 buflen = board->fw_max_len;
393
394 buf = malloc(buflen);
395 if (!buf) {
396 ERR("no memory for buffer\n");
397 goto out;
398 }
399
400 memset(buf, 0xff, buflen);
401 p = buf + sizeof(struct fw_header);
402 ret = read_to_buf(&kernel_info, p);
403 if (ret)
404 goto out_free_buf;
405
406 if (!combined) {
407 p = buf + board->rootfs_ofs;
408 ret = read_to_buf(&rootfs_info, p);
409 if (ret)
410 goto out_free_buf;
411 }
412
413 fill_header(buf, buflen);
414
415 ret = write_fw(buf, buflen);
416 if (ret)
417 goto out_free_buf;
418
419 ret = EXIT_SUCCESS;
420
421 out_free_buf:
422 free(buf);
423 out:
424 return ret;
425 }
426
427 int main(int argc, char *argv[])
428 {
429 int ret = EXIT_FAILURE;
430 int err;
431
432 FILE *outfile;
433
434 progname = basename(argv[0]);
435
436 while ( 1 ) {
437 int c;
438
439 c = getopt(argc, argv, "B:V:N:ck:r:o:v:h:");
440 if (c == -1)
441 break;
442
443 switch (c) {
444 case 'B':
445 board_id = optarg;
446 break;
447 case 'V':
448 version = optarg;
449 break;
450 case 'N':
451 vendor = optarg;
452 break;
453 case 'c':
454 combined++;
455 break;
456 case 'k':
457 kernel_info.file_name = optarg;
458 break;
459 case 'r':
460 rootfs_info.file_name = optarg;
461 break;
462 case 'o':
463 ofname = optarg;
464 break;
465 case 'v':
466 version = optarg;
467 break;
468 case 'h':
469 usage(EXIT_SUCCESS);
470 break;
471 default:
472 usage(EXIT_FAILURE);
473 break;
474 }
475 }
476
477 ret = check_options();
478 if (ret)
479 goto out;
480
481 ret = build_fw();
482
483 out:
484 return ret;
485 }
486