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