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