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