35db848bbab9f8315526dfa24ed351ad58878dde
[openwrt/openwrt.git] / tools / firmware-utils / src / mktplinkfw2.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 <stdbool.h>
24 #include <endian.h>
25 #include <sys/stat.h>
26
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29
30 #include "md5.h"
31 #include "mktplinkfw-lib.h"
32
33 struct fw_header {
34 uint32_t version; /* 0x00: header version */
35 char fw_version[48]; /* 0x04: fw version string */
36 uint32_t hw_id; /* 0x34: hardware id */
37 uint32_t hw_rev; /* 0x38: FIXME: hardware revision? */
38 uint32_t hw_ver_add; /* 0x3c: additional hardware version */
39 uint8_t md5sum1[MD5SUM_LEN]; /* 0x40 */
40 uint32_t unk2; /* 0x50: 0x00000000 */
41 uint8_t md5sum2[MD5SUM_LEN]; /* 0x54 */
42 uint32_t unk3; /* 0x64: 0xffffffff */
43
44 uint32_t kernel_la; /* 0x68: kernel load address */
45 uint32_t kernel_ep; /* 0x6c: kernel entry point */
46 uint32_t fw_length; /* 0x70: total length of the image */
47 uint32_t kernel_ofs; /* 0x74: kernel data offset */
48 uint32_t kernel_len; /* 0x78: kernel data length */
49 uint32_t rootfs_ofs; /* 0x7c: rootfs data offset */
50 uint32_t rootfs_len; /* 0x80: rootfs data length */
51 uint32_t boot_ofs; /* 0x84: bootloader offset */
52 uint32_t boot_len; /* 0x88: bootloader length */
53 uint16_t unk4; /* 0x8c: 0x55aa */
54 uint8_t sver_hi; /* 0x8e */
55 uint8_t sver_lo; /* 0x8f */
56 uint8_t unk5; /* 0x90: magic: 0xa5 */
57 uint8_t ver_hi; /* 0x91 */
58 uint8_t ver_mid; /* 0x92 */
59 uint8_t ver_lo; /* 0x93 */
60 uint8_t pad[364];
61 } __attribute__ ((packed));
62
63 #define FLAG_LE_KERNEL_LA_EP 0x00000001 /* Little-endian used for kernel load address & entry point */
64
65 struct board_info {
66 char *id;
67 uint32_t hw_id;
68 uint32_t hw_rev;
69 uint32_t hw_ver_add;
70 char *layout_id;
71 uint32_t hdr_ver;
72 uint32_t flags;
73 };
74
75 /*
76 * Globals
77 */
78 char *ofname;
79 char *progname;
80 static char *vendor = "TP-LINK Technologies";
81 static char *version = "ver. 1.0";
82 static char *fw_ver = "0.0.0";
83 static char *sver = "1.0";
84 static uint32_t hdr_ver = 2;
85
86 static struct board_info custom_board;
87
88 static struct board_info *board;
89 static char *layout_id;
90 struct flash_layout *layout;
91 static char *opt_hw_id;
92 static char *opt_hw_rev;
93 static char *opt_hw_ver_add;
94 static int fw_ver_lo;
95 static int fw_ver_mid;
96 static int fw_ver_hi;
97 static int sver_lo;
98 static int sver_hi;
99 struct file_info kernel_info;
100 static uint32_t kernel_la = 0;
101 static uint32_t kernel_ep = 0;
102 uint32_t kernel_len = 0;
103 struct file_info rootfs_info;
104 uint32_t rootfs_ofs = 0;
105 uint32_t rootfs_align;
106 static struct file_info boot_info;
107 int combined;
108 int strip_padding;
109 int add_jffs2_eof;
110
111 static struct file_info inspect_info;
112 static int extract = 0;
113
114 char md5salt_normal[MD5SUM_LEN] = {
115 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
116 0xdc, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x37,
117 };
118
119 char md5salt_boot[MD5SUM_LEN] = {
120 0x8c, 0xef, 0x33, 0x5f, 0xd5, 0xc5, 0xce, 0xfa,
121 0xac, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
122 };
123
124 static struct flash_layout layouts[] = {
125 {
126 .id = "4Mmtk",
127 .fw_max_len = 0x3d0000,
128 .kernel_la = 0x80000000,
129 .kernel_ep = 0x80000000,
130 .rootfs_ofs = 0x140000,
131 }, {
132 .id = "8Mltq",
133 .fw_max_len = 0x7a0000,
134 .kernel_la = 0x80002000,
135 .kernel_ep = 0x80002000,
136 .rootfs_ofs = 0x140000,
137 }, {
138 .id = "16Mltq",
139 .fw_max_len = 0xf90000,
140 .kernel_la = 0x80002000,
141 .kernel_ep = 0x800061b0,
142 .rootfs_ofs = 0x140000,
143 }, {
144 .id = "8Mmtk",
145 .fw_max_len = 0x7a0000,
146 .kernel_la = 0x80000000,
147 .kernel_ep = 0x80000000,
148 .rootfs_ofs = 0x140000,
149 }, {
150 .id = "8MSUmtk", /* Split U-Boot OS */
151 .fw_max_len = 0x770000,
152 .kernel_la = 0x80000000,
153 .kernel_ep = 0x80000000,
154 .rootfs_ofs = 0x140000,
155 }, {
156 .id = "8MLmtk",
157 .fw_max_len = 0x7b0000,
158 .kernel_la = 0x80000000,
159 .kernel_ep = 0x80000000,
160 .rootfs_ofs = 0x140000,
161 }, {
162 /* terminating entry */
163 }
164 };
165
166 static void usage(int status)
167 {
168 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
169 struct board_info *board;
170
171 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
172 fprintf(stream,
173 "\n"
174 "Options:\n"
175 " -c use combined kernel image\n"
176 " -e swap endianness in kernel load address and entry point\n"
177 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
178 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
179 " -H <hwid> use hardware id specified with <hwid>\n"
180 " -W <hwrev> use hardware revision specified with <hwrev>\n"
181 " -w <hwveradd> use additional hardware version specified with <hwveradd>\n"
182 " -F <id> use flash layout specified with <id>\n"
183 " -k <file> read kernel image from the file <file>\n"
184 " -r <file> read rootfs image from the file <file>\n"
185 " -a <align> align the rootfs start on an <align> bytes boundary\n"
186 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
187 " -o <file> write output to the file <file>\n"
188 " -s strip padding from the end of the image\n"
189 " -j add jffs2 end-of-filesystem markers\n"
190 " -N <vendor> set image vendor to <vendor>\n"
191 " -T <version> set header version to <version>\n"
192 " -V <version> set image version to <version>\n"
193 " -v <version> set firmware version to <version>\n"
194 " -y <version> set secondary version to <version>\n"
195 " -i <file> inspect given firmware file <file>\n"
196 " -x extract kernel and rootfs while inspecting (requires -i)\n"
197 " -h show this screen\n"
198 );
199
200 exit(status);
201 }
202
203 static int check_options(void)
204 {
205 int ret;
206
207 if (inspect_info.file_name) {
208 ret = get_file_stat(&inspect_info);
209 if (ret)
210 return ret;
211
212 return 0;
213 } else if (extract) {
214 ERR("no firmware for inspection specified");
215 return -1;
216 }
217
218 if (opt_hw_id == NULL) {
219 ERR("hardware id must be specified");
220 return -1;
221 }
222
223 board = &custom_board;
224
225 if (layout_id == NULL) {
226 ERR("flash layout is not specified");
227 return -1;
228 }
229
230 board->hw_id = strtoul(opt_hw_id, NULL, 0);
231
232 board->hw_rev = 1;
233 board->hw_ver_add = 0;
234
235 if (opt_hw_rev)
236 board->hw_rev = strtoul(opt_hw_rev, NULL, 0);
237 if (opt_hw_ver_add)
238 board->hw_ver_add = strtoul(opt_hw_ver_add, NULL, 0);
239
240 layout = find_layout(layouts, layout_id);
241 if (layout == NULL) {
242 ERR("unknown flash layout \"%s\"", layout_id);
243 return -1;
244 }
245
246 if (!kernel_la)
247 kernel_la = layout->kernel_la;
248 if (!kernel_ep)
249 kernel_ep = layout->kernel_ep;
250 if (!rootfs_ofs)
251 rootfs_ofs = layout->rootfs_ofs;
252
253 if (kernel_info.file_name == NULL) {
254 ERR("no kernel image specified");
255 return -1;
256 }
257
258 ret = get_file_stat(&kernel_info);
259 if (ret)
260 return ret;
261
262 kernel_len = kernel_info.file_size;
263
264 if (combined) {
265 if (kernel_info.file_size >
266 layout->fw_max_len - sizeof(struct fw_header)) {
267 ERR("kernel image is too big");
268 return -1;
269 }
270 } else {
271 if (rootfs_info.file_name == NULL) {
272 ERR("no rootfs image specified");
273 return -1;
274 }
275
276 ret = get_file_stat(&rootfs_info);
277 if (ret)
278 return ret;
279
280 if (rootfs_align) {
281 kernel_len += sizeof(struct fw_header);
282 rootfs_ofs = ALIGN(kernel_len, rootfs_align);
283 kernel_len -= sizeof(struct fw_header);
284
285 DBG("rootfs offset aligned to 0x%u", rootfs_ofs);
286
287 if (kernel_len + rootfs_info.file_size >
288 layout->fw_max_len - sizeof(struct fw_header)) {
289 ERR("images are too big");
290 return -1;
291 }
292 } else {
293 if (kernel_info.file_size >
294 rootfs_ofs - sizeof(struct fw_header)) {
295 ERR("kernel image is too big");
296 return -1;
297 }
298
299 if (rootfs_info.file_size >
300 (layout->fw_max_len - rootfs_ofs)) {
301 ERR("rootfs image is too big");
302 return -1;
303 }
304 }
305 }
306
307 if (ofname == NULL) {
308 ERR("no output file specified");
309 return -1;
310 }
311
312 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
313 if (ret != 3) {
314 ERR("invalid firmware version '%s'", fw_ver);
315 return -1;
316 }
317
318 ret = sscanf(sver, "%d.%d", &sver_hi, &sver_lo);
319 if (ret != 2) {
320 ERR("invalid secondary version '%s'", sver);
321 return -1;
322 }
323
324 return 0;
325 }
326
327 void fill_header(char *buf, int len)
328 {
329 struct fw_header *hdr = (struct fw_header *)buf;
330 unsigned ver_len;
331
332 memset(hdr, '\xff', sizeof(struct fw_header));
333
334 hdr->version = htonl(bswap_32(hdr_ver));
335 ver_len = strlen(version);
336 if (ver_len > (sizeof(hdr->fw_version) - 1))
337 ver_len = sizeof(hdr->fw_version) - 1;
338
339 memcpy(hdr->fw_version, version, ver_len);
340 hdr->fw_version[ver_len] = 0;
341
342 hdr->hw_id = htonl(board->hw_id);
343 hdr->hw_rev = htonl(board->hw_rev);
344 hdr->hw_ver_add = htonl(board->hw_ver_add);
345
346 if (boot_info.file_size == 0) {
347 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
348 hdr->boot_ofs = htonl(0);
349 hdr->boot_len = htonl(0);
350 } else {
351 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
352 hdr->boot_ofs = htonl(rootfs_ofs + rootfs_info.file_size);
353 hdr->boot_len = htonl(rootfs_info.file_size);
354 }
355
356 hdr->kernel_la = htonl(kernel_la);
357 hdr->kernel_ep = htonl(kernel_ep);
358 hdr->fw_length = htonl(layout->fw_max_len);
359 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
360 hdr->kernel_len = htonl(kernel_len);
361 if (!combined) {
362 hdr->rootfs_ofs = htonl(rootfs_ofs);
363 hdr->rootfs_len = htonl(rootfs_info.file_size);
364 }
365
366 hdr->boot_ofs = htonl(0);
367 hdr->boot_len = htonl(boot_info.file_size);
368
369 hdr->unk2 = htonl(0);
370 hdr->unk3 = htonl(0xffffffff);
371 hdr->unk4 = htons(0x55aa);
372 hdr->unk5 = 0xa5;
373
374 hdr->sver_hi = sver_hi;
375 hdr->sver_lo = sver_lo;
376
377 hdr->ver_hi = fw_ver_hi;
378 hdr->ver_mid = fw_ver_mid;
379 hdr->ver_lo = fw_ver_lo;
380
381 if (board->flags & FLAG_LE_KERNEL_LA_EP) {
382 hdr->kernel_la = bswap_32(hdr->kernel_la);
383 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
384 }
385
386 get_md5(buf, len, hdr->md5sum1);
387 }
388
389 static int inspect_fw(void)
390 {
391 char *buf;
392 struct fw_header *hdr;
393 uint8_t md5sum[MD5SUM_LEN];
394 struct board_info *board;
395 int ret = EXIT_FAILURE;
396
397 buf = malloc(inspect_info.file_size);
398 if (!buf) {
399 ERR("no memory for buffer!\n");
400 goto out;
401 }
402
403 ret = read_to_buf(&inspect_info, buf);
404 if (ret)
405 goto out_free_buf;
406 hdr = (struct fw_header *)buf;
407
408 board = &custom_board;
409
410 if (board->flags & FLAG_LE_KERNEL_LA_EP) {
411 hdr->kernel_la = bswap_32(hdr->kernel_la);
412 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
413 }
414
415 inspect_fw_pstr("File name", inspect_info.file_name);
416 inspect_fw_phexdec("File size", inspect_info.file_size);
417
418 switch(bswap_32(ntohl(hdr->version))) {
419 case 2:
420 case 3:
421 break;
422 default:
423 ERR("file does not seem to have V2/V3 header!\n");
424 goto out_free_buf;
425 }
426
427 inspect_fw_phexdec("Version 2 Header size", sizeof(struct fw_header));
428
429 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
430 if (ntohl(hdr->boot_len) == 0)
431 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
432 else
433 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
434 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
435
436 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
437 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
438 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
439 } else {
440 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
441 }
442 if (ntohl(hdr->unk2) != 0)
443 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
444 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
445 "(purpose yet unknown, unchecked here)");
446
447 if (ntohl(hdr->unk3) != 0xffffffff)
448 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
449
450 if (ntohs(hdr->unk4) != 0x55aa)
451 inspect_fw_phexdec("Unknown value 4", hdr->unk4);
452
453 if (hdr->unk5 != 0xa5)
454 inspect_fw_phexdec("Unknown value 5", hdr->unk5);
455
456 printf("\n");
457
458 inspect_fw_pstr("Firmware version", hdr->fw_version);
459 inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id));
460 inspect_fw_phex("Hardware Revision",
461 ntohl(hdr->hw_rev));
462 inspect_fw_phex("Additional HW Version",
463 ntohl(hdr->hw_ver_add));
464
465 printf("%-23s: %d.%d.%d-%d.%d\n", "Software version",
466 hdr->ver_hi, hdr->ver_mid, hdr->ver_lo,
467 hdr->sver_hi, hdr->sver_lo);
468
469 printf("\n");
470
471 inspect_fw_phexdec("Kernel data offset",
472 ntohl(hdr->kernel_ofs));
473 inspect_fw_phexdec("Kernel data length",
474 ntohl(hdr->kernel_len));
475 inspect_fw_phex("Kernel load address",
476 ntohl(hdr->kernel_la));
477 inspect_fw_phex("Kernel entry point",
478 ntohl(hdr->kernel_ep));
479 inspect_fw_phexdec("Rootfs data offset",
480 ntohl(hdr->rootfs_ofs));
481 inspect_fw_phexdec("Rootfs data length",
482 ntohl(hdr->rootfs_len));
483 inspect_fw_phexdec("Boot loader data offset",
484 ntohl(hdr->boot_ofs));
485 inspect_fw_phexdec("Boot loader data length",
486 ntohl(hdr->boot_len));
487 inspect_fw_phexdec("Total firmware length",
488 ntohl(hdr->fw_length));
489
490 if (extract) {
491 FILE *fp;
492 char *filename;
493
494 printf("\n");
495
496 filename = malloc(strlen(inspect_info.file_name) + 8);
497 sprintf(filename, "%s-kernel", inspect_info.file_name);
498 printf("Extracting kernel to \"%s\"...\n", filename);
499 fp = fopen(filename, "w");
500 if (fp) {
501 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
502 ntohl(hdr->kernel_len), 1, fp)) {
503 ERR("error in fwrite(): %s", strerror(errno));
504 }
505 fclose(fp);
506 } else {
507 ERR("error in fopen(): %s", strerror(errno));
508 }
509 free(filename);
510
511 filename = malloc(strlen(inspect_info.file_name) + 8);
512 sprintf(filename, "%s-rootfs", inspect_info.file_name);
513 printf("Extracting rootfs to \"%s\"...\n", filename);
514 fp = fopen(filename, "w");
515 if (fp) {
516 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
517 ntohl(hdr->rootfs_len), 1, fp)) {
518 ERR("error in fwrite(): %s", strerror(errno));
519 }
520 fclose(fp);
521 } else {
522 ERR("error in fopen(): %s", strerror(errno));
523 }
524 free(filename);
525 }
526
527 out_free_buf:
528 free(buf);
529 out:
530 return ret;
531 }
532
533 int main(int argc, char *argv[])
534 {
535 int ret = EXIT_FAILURE;
536
537 progname = basename(argv[0]);
538
539 while ( 1 ) {
540 int c;
541
542 c = getopt(argc, argv, "a:H:E:F:L:V:N:W:w:ci:k:r:R:o:xhsjv:y:T:e");
543 if (c == -1)
544 break;
545
546 switch (c) {
547 case 'a':
548 sscanf(optarg, "0x%x", &rootfs_align);
549 break;
550 case 'H':
551 opt_hw_id = optarg;
552 break;
553 case 'E':
554 sscanf(optarg, "0x%x", &kernel_ep);
555 break;
556 case 'F':
557 layout_id = optarg;
558 break;
559 case 'W':
560 opt_hw_rev = optarg;
561 break;
562 case 'w':
563 opt_hw_ver_add = optarg;
564 break;
565 case 'L':
566 sscanf(optarg, "0x%x", &kernel_la);
567 break;
568 case 'V':
569 version = optarg;
570 break;
571 case 'v':
572 fw_ver = optarg;
573 break;
574 case 'y':
575 sver = optarg;
576 break;
577 case 'N':
578 vendor = optarg;
579 break;
580 case 'c':
581 combined++;
582 break;
583 case 'k':
584 kernel_info.file_name = optarg;
585 break;
586 case 'r':
587 rootfs_info.file_name = optarg;
588 break;
589 case 'R':
590 sscanf(optarg, "0x%x", &rootfs_ofs);
591 break;
592 case 'o':
593 ofname = optarg;
594 break;
595 case 's':
596 strip_padding = 1;
597 break;
598 case 'i':
599 inspect_info.file_name = optarg;
600 break;
601 case 'j':
602 add_jffs2_eof = 1;
603 break;
604 case 'x':
605 extract = 1;
606 break;
607 case 'T':
608 hdr_ver = atoi(optarg);
609 break;
610 case 'e':
611 custom_board.flags = FLAG_LE_KERNEL_LA_EP;
612 break;
613 case 'h':
614 usage(EXIT_SUCCESS);
615 break;
616 default:
617 usage(EXIT_FAILURE);
618 break;
619 }
620 }
621
622 ret = check_options();
623 if (ret)
624 goto out;
625
626 if (!inspect_info.file_name)
627 ret = build_fw(sizeof(struct fw_header));
628 else
629 ret = inspect_fw();
630
631 out:
632 return ret;
633 }
634