firmware-utils: mktplinkfw: add support for BR region code
[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 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 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 struct file_info kernel_info;
94 static uint32_t kernel_la = 0;
95 static uint32_t kernel_ep = 0;
96 uint32_t kernel_len = 0;
97 struct file_info rootfs_info;
98 uint32_t rootfs_ofs = 0;
99 uint32_t rootfs_align;
100 static struct file_info boot_info;
101 int combined;
102 int strip_padding;
103 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 {"BR", 0},
174 };
175
176 static const struct fw_region * find_region(const char *country) {
177 size_t i;
178
179 for (i = 0; i < ARRAY_SIZE(regions); i++) {
180 if (strcasecmp(regions[i].name, country) == 0)
181 return &regions[i];
182 }
183
184 return NULL;
185 }
186
187 static void usage(int status)
188 {
189 fprintf(stderr, "Usage: %s [OPTIONS...]\n", progname);
190 fprintf(stderr,
191 "\n"
192 "Options:\n"
193 " -c use combined kernel image\n"
194 " -e swap endianness in kernel load address and entry point\n"
195 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
196 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
197 " -H <hwid> use hardware id specified with <hwid>\n"
198 " -W <hwrev> use hardware revision specified with <hwrev>\n"
199 " -C <country> set region code to <country>\n"
200 " -F <id> use flash layout specified with <id>\n"
201 " -k <file> read kernel image from the file <file>\n"
202 " -r <file> read rootfs image from the file <file>\n"
203 " -a <align> align the rootfs start on an <align> bytes boundary\n"
204 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
205 " -o <file> write output to the file <file>\n"
206 " -s strip padding from the end of the image\n"
207 " -j add jffs2 end-of-filesystem markers\n"
208 " -N <vendor> set image vendor to <vendor>\n"
209 " -V <version> set image version to <version>\n"
210 " -v <version> set firmware version to <version>\n"
211 " -m <version> set header version to <version>\n"
212 " -i <file> inspect given firmware file <file>\n"
213 " -x extract kernel and rootfs while inspecting (requires -i)\n"
214 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
215 " -h show this screen\n"
216 );
217
218 exit(status);
219 }
220
221 static int check_options(void)
222 {
223 int ret;
224 int exceed_bytes;
225
226 if (inspect_info.file_name) {
227 ret = get_file_stat(&inspect_info);
228 if (ret)
229 return ret;
230
231 return 0;
232 } else if (extract) {
233 ERR("no firmware for inspection specified");
234 return -1;
235 }
236
237 if (opt_hw_id == NULL) {
238 ERR("hardware id not specified");
239 return -1;
240 }
241 hw_id = strtoul(opt_hw_id, NULL, 0);
242
243 if (!combined && layout_id == NULL) {
244 ERR("flash layout is not specified");
245 return -1;
246 }
247
248 if (opt_hw_rev)
249 hw_rev = strtoul(opt_hw_rev, NULL, 0);
250 else
251 hw_rev = 1;
252
253 if (country) {
254 region = find_region(country);
255 if (!region) {
256 ERR("unknown region code \"%s\"", country);
257 return -1;
258 }
259 }
260
261 if (combined) {
262 if (!kernel_la || !kernel_ep) {
263 ERR("kernel loading address and entry point must be specified for combined image");
264 return -1;
265 }
266 } else {
267 layout = find_layout(layouts, layout_id);
268 if (layout == NULL) {
269 ERR("unknown flash layout \"%s\"", layout_id);
270 return -1;
271 }
272
273 if (!kernel_la)
274 kernel_la = layout->kernel_la;
275 if (!kernel_ep)
276 kernel_ep = layout->kernel_ep;
277 if (!rootfs_ofs)
278 rootfs_ofs = layout->rootfs_ofs;
279
280 if (reserved_space > layout->fw_max_len) {
281 ERR("reserved space is not valid");
282 return -1;
283 }
284 }
285
286 if (kernel_info.file_name == NULL) {
287 ERR("no kernel image specified");
288 return -1;
289 }
290
291 ret = get_file_stat(&kernel_info);
292 if (ret)
293 return ret;
294
295 kernel_len = kernel_info.file_size;
296
297 if (!combined) {
298 fw_max_len = layout->fw_max_len - reserved_space;
299
300 if (rootfs_info.file_name == NULL) {
301 ERR("no rootfs image specified");
302 return -1;
303 }
304
305 ret = get_file_stat(&rootfs_info);
306 if (ret)
307 return ret;
308
309 if (rootfs_align) {
310 kernel_len += sizeof(struct fw_header);
311 rootfs_ofs = ALIGN(kernel_len, rootfs_align);
312 kernel_len -= sizeof(struct fw_header);
313
314 DBG("rootfs offset aligned to 0x%u", rootfs_ofs);
315
316 exceed_bytes = kernel_len + rootfs_info.file_size - (fw_max_len - sizeof(struct fw_header));
317 if (exceed_bytes > 0) {
318 ERR("images are too big by %i bytes", exceed_bytes);
319 return -1;
320 }
321 } else {
322 exceed_bytes = kernel_info.file_size - (rootfs_ofs - sizeof(struct fw_header));
323 if (exceed_bytes > 0) {
324 ERR("kernel image is too big by %i bytes", exceed_bytes);
325 return -1;
326 }
327
328 exceed_bytes = rootfs_info.file_size - (fw_max_len - rootfs_ofs);
329 if (exceed_bytes > 0) {
330 ERR("rootfs image is too big by %i bytes", exceed_bytes);
331 return -1;
332 }
333 }
334 }
335
336 if (ofname == NULL) {
337 ERR("no output file specified");
338 return -1;
339 }
340
341 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
342 if (ret != 3) {
343 ERR("invalid firmware version '%s'", fw_ver);
344 return -1;
345 }
346
347 if (opt_hdr_ver == 1) {
348 hdr_ver = HEADER_VERSION_V1;
349 } else if (opt_hdr_ver == 2) {
350 hdr_ver = HEADER_VERSION_V2;
351 } else {
352 ERR("invalid header version '%u'", opt_hdr_ver);
353 return -1;
354 }
355
356 return 0;
357 }
358
359 void fill_header(char *buf, int len)
360 {
361 struct fw_header *hdr = (struct fw_header *)buf;
362
363 memset(hdr, 0, sizeof(struct fw_header));
364
365 hdr->version = htonl(hdr_ver);
366 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
367 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
368 hdr->hw_id = htonl(hw_id);
369 hdr->hw_rev = htonl(hw_rev);
370
371 hdr->kernel_la = htonl(kernel_la);
372 hdr->kernel_ep = htonl(kernel_ep);
373 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
374 hdr->kernel_len = htonl(kernel_len);
375
376 if (!combined) {
377 if (boot_info.file_size == 0)
378 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
379 else
380 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
381
382 hdr->fw_length = htonl(layout->fw_max_len);
383 hdr->rootfs_ofs = htonl(rootfs_ofs);
384 hdr->rootfs_len = htonl(rootfs_info.file_size);
385 }
386
387 hdr->ver_hi = htons(fw_ver_hi);
388 hdr->ver_mid = htons(fw_ver_mid);
389 hdr->ver_lo = htons(fw_ver_lo);
390
391 if (region) {
392 hdr->region_code = htonl(region->code);
393 snprintf(
394 hdr->region_str1, sizeof(hdr->region_str1), "00000000;%02X%02X%02X%02X;",
395 region->name[0], region->name[1], region->name[2], region->name[3]
396 );
397 snprintf(
398 hdr->region_str2, sizeof(hdr->region_str2), "%02X%02X%02X%02X",
399 region->name[0], region->name[1], region->name[2], region->name[3]
400 );
401 }
402
403 if (endian_swap) {
404 hdr->kernel_la = bswap_32(hdr->kernel_la);
405 hdr->kernel_ep = bswap_32(hdr->kernel_ep);
406 }
407
408 if (!combined)
409 get_md5(buf, len, hdr->md5sum1);
410 }
411
412 static int inspect_fw(void)
413 {
414 char *buf;
415 struct fw_header *hdr;
416 uint8_t md5sum[MD5SUM_LEN];
417 int ret = EXIT_FAILURE;
418
419 buf = malloc(inspect_info.file_size);
420 if (!buf) {
421 ERR("no memory for buffer!\n");
422 goto out;
423 }
424
425 ret = read_to_buf(&inspect_info, buf);
426 if (ret)
427 goto out_free_buf;
428 hdr = (struct fw_header *)buf;
429
430 inspect_fw_pstr("File name", inspect_info.file_name);
431 inspect_fw_phexdec("File size", inspect_info.file_size);
432
433 if ((ntohl(hdr->version) != HEADER_VERSION_V1) &&
434 (ntohl(hdr->version) != HEADER_VERSION_V2)) {
435 ERR("file does not seem to have V1/V2 header!\n");
436 goto out_free_buf;
437 }
438
439 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
440
441 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
442 if (ntohl(hdr->boot_len) == 0)
443 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
444 else
445 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
446 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
447
448 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
449 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
450 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
451 } else {
452 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
453 }
454 if (ntohl(hdr->unk2) != 0)
455 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
456 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
457 "(purpose yet unknown, unchecked here)");
458 if (ntohl(hdr->unk3) != 0)
459 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
460
461 printf("\n");
462
463 inspect_fw_pstr("Vendor name", hdr->vendor_name);
464 inspect_fw_pstr("Firmware version", hdr->fw_version);
465 inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id));
466 inspect_fw_phex("Hardware Revision", ntohl(hdr->hw_rev));
467 inspect_fw_phex("Region code", ntohl(hdr->region_code));
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:m:V:N:W:C:ci:k:r:R:o:xX:ehsjv:");
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 'C':
563 country = optarg;
564 break;
565 case 'L':
566 sscanf(optarg, "0x%x", &kernel_la);
567 break;
568 case 'm':
569 sscanf(optarg, "%u", &opt_hdr_ver);
570 break;
571 case 'V':
572 version = optarg;
573 break;
574 case 'v':
575 fw_ver = 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 'e':
608 endian_swap = true;
609 break;
610 case 'h':
611 usage(EXIT_SUCCESS);
612 break;
613 case 'X':
614 sscanf(optarg, "0x%x", &reserved_space);
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 }