1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
5 * This tool was based on:
6 * TP-Link WR941 V2 firmware checksum fixing tool.
7 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
15 #include <unistd.h> /* for unlink() */
17 #include <getopt.h> /* for getopt() */
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
28 #include "mktplinkfw-lib.h"
31 uint32_t version
; /* 0x00: header version */
32 char fw_version
[48]; /* 0x04: fw version string */
33 uint32_t hw_id
; /* 0x34: hardware id */
34 uint32_t hw_rev
; /* 0x38: FIXME: hardware revision? */
35 uint32_t hw_ver_add
; /* 0x3c: additional hardware version */
36 uint8_t md5sum1
[MD5SUM_LEN
]; /* 0x40 */
37 uint32_t unk2
; /* 0x50: 0x00000000 */
38 uint8_t md5sum2
[MD5SUM_LEN
]; /* 0x54 */
39 uint32_t unk3
; /* 0x64: 0xffffffff */
41 uint32_t kernel_la
; /* 0x68: kernel load address */
42 uint32_t kernel_ep
; /* 0x6c: kernel entry point */
43 uint32_t fw_length
; /* 0x70: total length of the image */
44 uint32_t kernel_ofs
; /* 0x74: kernel data offset */
45 uint32_t kernel_len
; /* 0x78: kernel data length */
46 uint32_t rootfs_ofs
; /* 0x7c: rootfs data offset */
47 uint32_t rootfs_len
; /* 0x80: rootfs data length */
48 uint32_t boot_ofs
; /* 0x84: bootloader offset */
49 uint32_t boot_len
; /* 0x88: bootloader length */
50 uint16_t unk4
; /* 0x8c: 0x55aa */
51 uint8_t sver_hi
; /* 0x8e */
52 uint8_t sver_lo
; /* 0x8f */
53 uint8_t unk5
; /* 0x90: magic: 0xa5 */
54 uint8_t ver_hi
; /* 0x91 */
55 uint8_t ver_mid
; /* 0x92 */
56 uint8_t ver_lo
; /* 0x93 */
58 } __attribute__ ((packed
));
60 #define FLAG_LE_KERNEL_LA_EP 0x00000001 /* Little-endian used for kernel load address & entry point */
77 static char *vendor
= "TP-LINK Technologies";
78 static char *version
= "ver. 1.0";
79 static char *fw_ver
= "0.0.0";
80 static char *sver
= "1.0";
81 static uint32_t hdr_ver
= 2;
83 static struct board_info custom_board
;
85 static struct board_info
*board
;
86 static char *layout_id
;
87 struct flash_layout
*layout
;
88 static char *opt_hw_id
;
89 static char *opt_hw_rev
;
90 static char *opt_hw_ver_add
;
92 static int fw_ver_mid
;
96 struct file_info kernel_info
;
97 static uint32_t kernel_la
= 0;
98 static uint32_t kernel_ep
= 0;
99 uint32_t kernel_len
= 0;
100 struct file_info rootfs_info
;
101 uint32_t rootfs_ofs
= 0;
102 uint32_t rootfs_align
;
103 static struct file_info boot_info
= { 0 };
108 static struct file_info inspect_info
;
109 static int extract
= 0;
111 char md5salt_normal
[MD5SUM_LEN
] = {
112 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
113 0xdc, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x37,
116 char md5salt_boot
[MD5SUM_LEN
] = {
117 0x8c, 0xef, 0x33, 0x5f, 0xd5, 0xc5, 0xce, 0xfa,
118 0xac, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
121 static struct flash_layout layouts
[] = {
124 .fw_max_len
= 0x3c0000,
125 .kernel_la
= 0x80000000,
126 .kernel_ep
= 0x80000000,
127 .rootfs_ofs
= 0x140000,
130 .fw_max_len
= 0x3d0000,
131 .kernel_la
= 0x80000000,
132 .kernel_ep
= 0x80000000,
133 .rootfs_ofs
= 0x140000,
136 .fw_max_len
= 0x7a0000,
137 .kernel_la
= 0x80002000,
138 .kernel_ep
= 0x80002000,
139 .rootfs_ofs
= 0x140000,
142 .fw_max_len
= 0xf90000,
143 .kernel_la
= 0x80002000,
144 .kernel_ep
= 0x800061b0,
145 .rootfs_ofs
= 0x140000,
148 .fw_max_len
= 0x7a0000,
149 .kernel_la
= 0x80000000,
150 .kernel_ep
= 0x80000000,
151 .rootfs_ofs
= 0x140000,
153 .id
= "8MSUmtk", /* Split U-Boot OS */
154 .fw_max_len
= 0x770000,
155 .kernel_la
= 0x80000000,
156 .kernel_ep
= 0x80000000,
157 .rootfs_ofs
= 0x140000,
160 .fw_max_len
= 0x7b0000,
161 .kernel_la
= 0x80000000,
162 .kernel_ep
= 0x80000000,
163 .rootfs_ofs
= 0x140000,
166 .fw_max_len
= 0x7a0000,
167 .kernel_la
= 0x80060000,
168 .kernel_ep
= 0x80060000,
169 .rootfs_ofs
= 0x140000,
172 .fw_max_len
= 0xf90000,
173 .kernel_la
= 0x80060000,
174 .kernel_ep
= 0x80060000,
175 .rootfs_ofs
= 0x140000,
177 /* terminating entry */
181 static void usage(int status
)
183 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
185 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
189 " -c use combined kernel image\n"
190 " -e swap endianness in kernel load address and entry point\n"
191 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
192 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
193 " -H <hwid> use hardware id specified with <hwid>\n"
194 " -W <hwrev> use hardware revision specified with <hwrev>\n"
195 " -w <hwveradd> use additional hardware version specified with <hwveradd>\n"
196 " -F <id> use flash layout specified with <id>\n"
197 " -k <file> read kernel image from the file <file>\n"
198 " -r <file> read rootfs image from the file <file>\n"
199 " -b <file> read bootloader image from the file <file>\n"
200 " -a <align> align the rootfs start on an <align> bytes boundary\n"
201 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
202 " -o <file> write output to the file <file>\n"
203 " -s strip padding from the end of the image\n"
204 " -j add jffs2 end-of-filesystem markers\n"
205 " -N <vendor> set image vendor to <vendor>\n"
206 " -T <version> set header version to <version>\n"
207 " -V <version> set image version to <version>\n"
208 " -v <version> set firmware version to <version>\n"
209 " -y <version> set secondary version to <version>\n"
210 " -i <file> inspect given firmware file <file>\n"
211 " -x extract bootloader, kernel and rootfs while inspecting (requires -i)\n"
212 " -h show this screen\n"
218 static int check_options(void)
222 if (inspect_info
.file_name
) {
223 ret
= get_file_stat(&inspect_info
);
228 } else if (extract
) {
229 ERR("no firmware for inspection specified");
233 if (opt_hw_id
== NULL
) {
234 ERR("hardware id must be specified");
238 board
= &custom_board
;
240 if (layout_id
== NULL
) {
241 ERR("flash layout is not specified");
245 board
->hw_id
= strtoul(opt_hw_id
, NULL
, 0);
248 board
->hw_ver_add
= 0;
251 board
->hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
253 board
->hw_ver_add
= strtoul(opt_hw_ver_add
, NULL
, 0);
255 layout
= find_layout(layouts
, layout_id
);
256 if (layout
== NULL
) {
257 ERR("unknown flash layout \"%s\"", layout_id
);
262 kernel_la
= layout
->kernel_la
;
264 kernel_ep
= layout
->kernel_ep
;
266 rootfs_ofs
= layout
->rootfs_ofs
;
268 /* check bootloader */
269 ret
= get_file_stat(&boot_info
);
271 ERR("Can not load bootloader image.");
275 if (boot_info
.file_size
> 2 * 64 * 1024) {
276 /* the offset in fill_header is hardcoded to 128k */
277 ERR("Bootloader image is bigger than 128k.");
281 if (kernel_info
.file_name
== NULL
) {
282 ERR("no kernel image specified");
285 ret
= get_file_stat(&kernel_info
);
289 kernel_len
= kernel_info
.file_size
;
292 if (kernel_info
.file_size
>
293 layout
->fw_max_len
- sizeof(struct fw_header
)) {
294 ERR("kernel image is too big");
298 if (rootfs_info
.file_name
== NULL
) {
299 ERR("no rootfs image specified");
303 ret
= get_file_stat(&rootfs_info
);
308 kernel_len
+= sizeof(struct fw_header
);
309 rootfs_ofs
= ALIGN(kernel_len
, rootfs_align
);
310 kernel_len
-= sizeof(struct fw_header
);
312 DBG("rootfs offset aligned to 0x%u", rootfs_ofs
);
314 if (kernel_len
+ rootfs_info
.file_size
>
315 layout
->fw_max_len
- sizeof(struct fw_header
)) {
316 ERR("images are too big");
320 if (kernel_info
.file_size
>
321 rootfs_ofs
- sizeof(struct fw_header
)) {
322 ERR("kernel image is too big");
326 if (rootfs_info
.file_size
>
327 (layout
->fw_max_len
- rootfs_ofs
)) {
328 ERR("rootfs image is too big");
334 if (ofname
== NULL
) {
335 ERR("no output file specified");
339 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
341 ERR("invalid firmware version '%s'", fw_ver
);
345 ret
= sscanf(sver
, "%d.%d", &sver_hi
, &sver_lo
);
347 ERR("invalid secondary version '%s'", sver
);
354 void fill_header_bootloader(char *buf
, int len
, int with_bootloader
)
356 struct fw_header
*hdr
= (struct fw_header
*)buf
;
358 unsigned int offset
= 0;
360 if (with_bootloader
) {
361 /* ensure the bootloader is padded to 64k */
362 offset
= boot_info
.file_size
& (64 * 1024);
363 if (offset
< boot_info
.file_size
)
366 offset
+= sizeof(struct fw_header
);
369 memset(hdr
, '\xff', sizeof(struct fw_header
));
371 hdr
->version
= htonl(bswap_32(hdr_ver
));
372 ver_len
= strlen(version
);
373 if (ver_len
> (sizeof(hdr
->fw_version
) - 1))
374 ver_len
= sizeof(hdr
->fw_version
) - 1;
376 memcpy(hdr
->fw_version
, version
, ver_len
);
377 hdr
->fw_version
[ver_len
] = 0;
379 hdr
->hw_id
= htonl(board
->hw_id
);
380 hdr
->hw_rev
= htonl(board
->hw_rev
);
381 hdr
->hw_ver_add
= htonl(board
->hw_ver_add
);
383 if (boot_info
.file_size
== 0 || !with_bootloader
) {
384 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
385 hdr
->boot_ofs
= htonl(0);
386 hdr
->boot_len
= htonl(0);
388 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
389 hdr
->boot_ofs
= htonl(0);
390 hdr
->boot_len
= htonl(boot_info
.file_size
);
393 hdr
->kernel_la
= htonl(kernel_la
);
394 hdr
->kernel_ep
= htonl(kernel_ep
);
395 hdr
->fw_length
= htonl(layout
->fw_max_len
+ offset
);
396 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
) + offset
);
397 hdr
->kernel_len
= htonl(kernel_len
);
399 /* even the bootloader doesnt increased the root_ofs. Unsure if this parser
400 * always ignores the rootfs or only in the 841v13
402 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
403 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
406 hdr
->unk2
= htonl(0);
407 hdr
->unk3
= htonl(0xffffffff);
408 hdr
->unk4
= htons(0x55aa);
411 hdr
->sver_hi
= sver_hi
;
412 hdr
->sver_lo
= sver_lo
;
414 hdr
->ver_hi
= fw_ver_hi
;
415 hdr
->ver_mid
= fw_ver_mid
;
416 hdr
->ver_lo
= fw_ver_lo
;
418 if (board
->flags
& FLAG_LE_KERNEL_LA_EP
) {
419 hdr
->kernel_la
= bswap_32(hdr
->kernel_la
);
420 hdr
->kernel_ep
= bswap_32(hdr
->kernel_ep
);
423 get_md5(buf
, len
, hdr
->md5sum1
);
426 /* fill_header get called by mktplinkfw_lib to fill the header in front of the kernel. */
427 void fill_header(char *buf
, int len
) {
428 fill_header_bootloader(buf
, len
, 0);
431 static int inspect_fw(void)
434 struct fw_header
*hdr
;
435 uint8_t md5sum
[MD5SUM_LEN
];
436 struct board_info
*board
;
437 int ret
= EXIT_FAILURE
;
439 buf
= malloc(inspect_info
.file_size
);
441 ERR("no memory for buffer!\n");
445 ret
= read_to_buf(&inspect_info
, buf
);
448 hdr
= (struct fw_header
*)buf
;
450 board
= &custom_board
;
452 if (board
->flags
& FLAG_LE_KERNEL_LA_EP
) {
453 hdr
->kernel_la
= bswap_32(hdr
->kernel_la
);
454 hdr
->kernel_ep
= bswap_32(hdr
->kernel_ep
);
457 inspect_fw_pstr("File name", inspect_info
.file_name
);
458 inspect_fw_phexdec("File size", inspect_info
.file_size
);
460 switch(bswap_32(ntohl(hdr
->version
))) {
465 ERR("file does not seem to have V2/V3 header!\n");
469 inspect_fw_phexdec("Version 2 Header size", sizeof(struct fw_header
));
471 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
472 if (ntohl(hdr
->boot_len
) == 0)
473 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
475 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
476 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
478 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
479 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
480 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
482 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
484 if (ntohl(hdr
->unk2
) != 0)
485 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
486 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
487 "(purpose yet unknown, unchecked here)");
489 if (ntohl(hdr
->unk3
) != 0xffffffff)
490 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
492 if (ntohs(hdr
->unk4
) != 0x55aa)
493 inspect_fw_phexdec("Unknown value 4", hdr
->unk4
);
495 if (hdr
->unk5
!= 0xa5)
496 inspect_fw_phexdec("Unknown value 5", hdr
->unk5
);
500 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
501 inspect_fw_phex("Hardware ID", ntohl(hdr
->hw_id
));
502 inspect_fw_phex("Hardware Revision",
504 inspect_fw_phex("Additional HW Version",
505 ntohl(hdr
->hw_ver_add
));
507 printf("%-23s: %d.%d.%d-%d.%d\n", "Software version",
508 hdr
->ver_hi
, hdr
->ver_mid
, hdr
->ver_lo
,
509 hdr
->sver_hi
, hdr
->sver_lo
);
513 inspect_fw_phexdec("Kernel data offset",
514 ntohl(hdr
->kernel_ofs
));
515 inspect_fw_phexdec("Kernel data length",
516 ntohl(hdr
->kernel_len
));
517 inspect_fw_phex("Kernel load address",
518 ntohl(hdr
->kernel_la
));
519 inspect_fw_phex("Kernel entry point",
520 ntohl(hdr
->kernel_ep
));
521 inspect_fw_phexdec("Rootfs data offset",
522 ntohl(hdr
->rootfs_ofs
));
523 inspect_fw_phexdec("Rootfs data length",
524 ntohl(hdr
->rootfs_len
));
525 inspect_fw_phexdec("Boot loader data offset",
526 ntohl(hdr
->boot_ofs
));
527 inspect_fw_phexdec("Boot loader data length",
528 ntohl(hdr
->boot_len
));
529 inspect_fw_phexdec("Total firmware length",
530 ntohl(hdr
->fw_length
));
539 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
540 sprintf(filename
, "%s-bootloader", inspect_info
.file_name
);
541 printf("Extracting bootloader to \"%s\"...\n", filename
);
542 fp
= fopen(filename
, "w");
544 if (!fwrite(buf
+ sizeof(struct fw_header
) + ntohl(hdr
->boot_ofs
),
545 ntohl(hdr
->boot_len
), 1, fp
)) {
546 ERR("error in fwrite(): %s", strerror(errno
));
550 ERR("error in fopen(): %s", strerror(errno
));
555 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
556 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
557 printf("Extracting kernel to \"%s\"...\n", filename
);
558 fp
= fopen(filename
, "w");
560 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
561 ntohl(hdr
->kernel_len
), 1, fp
)) {
562 ERR("error in fwrite(): %s", strerror(errno
));
566 ERR("error in fopen(): %s", strerror(errno
));
570 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
571 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
572 printf("Extracting rootfs to \"%s\"...\n", filename
);
573 fp
= fopen(filename
, "w");
575 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
576 ntohl(hdr
->rootfs_len
), 1, fp
)) {
577 ERR("error in fwrite(): %s", strerror(errno
));
581 ERR("error in fopen(): %s", strerror(errno
));
592 /* prepend a second image header and the bootloader.
608 * The padding is optional. The second image header should begin a 64k boundary.
610 int prepend_bootloader() {
611 unsigned int buflen
= 0;
612 int ret
= 0, bootloader_padded
= 0;
613 char *buf
= 0, *p
= 0;
614 struct file_info image
;
616 /* calculate blocks to ensure padding */
617 bootloader_padded
= boot_info
.file_size
& (64 * 1024);
618 if (bootloader_padded
< boot_info
.file_size
)
619 bootloader_padded
+= 64 * 1024;
621 image
.file_name
= ofname
;
622 ret
= get_file_stat(&image
);
624 ERR("Can not load the output image");
628 buflen
= image
.file_size
+ bootloader_padded
+ sizeof(struct fw_header
);
629 buf
= malloc(buflen
);
631 ERR("Can not allocate buffer %d bytes", buflen
);
634 memset(buf
, 0xff, buflen
);
637 p
= buf
+ bootloader_padded
+ sizeof(struct fw_header
);
638 ret
= read_to_buf(&image
, p
);
640 ERR("Can not read output image");
644 p
= buf
+ sizeof(struct fw_header
);
645 ret
= read_to_buf(&boot_info
, p
);
647 fill_header_bootloader(buf
, buflen
, 1);
649 ret
= write_fw(ofname
, buf
, buflen
);
661 int main(int argc
, char *argv
[])
663 int ret
= EXIT_FAILURE
;
665 progname
= basename(argv
[0]);
670 c
= getopt(argc
, argv
, "a:b:H:E:F:L:V:N:W:w:ci:k:r:R:o:xhsjv:y:T:e");
676 sscanf(optarg
, "0x%x", &rootfs_align
);
679 boot_info
.file_name
= optarg
;
685 sscanf(optarg
, "0x%x", &kernel_ep
);
694 opt_hw_ver_add
= optarg
;
697 sscanf(optarg
, "0x%x", &kernel_la
);
715 kernel_info
.file_name
= optarg
;
718 rootfs_info
.file_name
= optarg
;
721 sscanf(optarg
, "0x%x", &rootfs_ofs
);
730 inspect_info
.file_name
= optarg
;
739 hdr_ver
= atoi(optarg
);
742 custom_board
.flags
= FLAG_LE_KERNEL_LA_EP
;
753 ret
= check_options();
757 if (!inspect_info
.file_name
) {
758 ret
= build_fw(sizeof(struct fw_header
));
759 if (ret
== 0 && boot_info
.file_size
> 0)
760 ret
= prepend_bootloader();