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