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