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)
223 if (inspect_info
.file_name
) {
224 ret
= get_file_stat(&inspect_info
);
229 } else if (extract
) {
230 ERR("no firmware for inspection specified");
234 if (opt_hw_id
== NULL
) {
235 ERR("hardware id must be specified");
239 board
= &custom_board
;
241 if (layout_id
== NULL
) {
242 ERR("flash layout is not specified");
246 board
->hw_id
= strtoul(opt_hw_id
, NULL
, 0);
249 board
->hw_ver_add
= 0;
252 board
->hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
254 board
->hw_ver_add
= strtoul(opt_hw_ver_add
, NULL
, 0);
256 layout
= find_layout(layouts
, layout_id
);
257 if (layout
== NULL
) {
258 ERR("unknown flash layout \"%s\"", layout_id
);
263 kernel_la
= layout
->kernel_la
;
265 kernel_ep
= layout
->kernel_ep
;
267 rootfs_ofs
= layout
->rootfs_ofs
;
269 /* check bootloader */
270 ret
= get_file_stat(&boot_info
);
272 ERR("Can not load bootloader image.");
276 if (boot_info
.file_size
> 2 * 64 * 1024) {
277 /* the offset in fill_header is hardcoded to 128k */
278 ERR("Bootloader image is bigger than 128k.");
282 if (kernel_info
.file_name
== NULL
) {
283 ERR("no kernel image specified");
286 ret
= get_file_stat(&kernel_info
);
290 kernel_len
= kernel_info
.file_size
;
293 if (kernel_info
.file_size
>
294 layout
->fw_max_len
- sizeof(struct fw_header
)) {
295 ERR("kernel image is too big");
299 if (rootfs_info
.file_name
== NULL
) {
300 ERR("no rootfs image specified");
304 ret
= get_file_stat(&rootfs_info
);
309 kernel_len
+= sizeof(struct fw_header
);
310 rootfs_ofs
= ALIGN(kernel_len
, rootfs_align
);
311 kernel_len
-= sizeof(struct fw_header
);
313 DBG("rootfs offset aligned to 0x%u", rootfs_ofs
);
315 exceed_bytes
= (kernel_len
+ rootfs_info
.file_size
) -
316 (layout
->fw_max_len
- sizeof(struct fw_header
));
317 if (exceed_bytes
> 0) {
318 ERR("images are too big by %i bytes", exceed_bytes
);
322 exceed_bytes
= kernel_info
.file_size
-
323 (rootfs_ofs
- sizeof(struct fw_header
));
324 if (exceed_bytes
> 0) {
325 ERR("images are too big by %i bytes", exceed_bytes
);
329 exceed_bytes
= rootfs_info
.file_size
-
330 (layout
->fw_max_len
- rootfs_ofs
);
331 if (exceed_bytes
> 0) {
332 ERR("images are too big by %i bytes", exceed_bytes
);
338 if (ofname
== NULL
) {
339 ERR("no output file specified");
343 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
345 ERR("invalid firmware version '%s'", fw_ver
);
349 ret
= sscanf(sver
, "%d.%d", &sver_hi
, &sver_lo
);
351 ERR("invalid secondary version '%s'", sver
);
358 void fill_header_bootloader(char *buf
, int len
, int with_bootloader
)
360 struct fw_header
*hdr
= (struct fw_header
*)buf
;
362 unsigned int offset
= 0;
364 if (with_bootloader
) {
365 /* ensure the bootloader is padded to 64k */
366 offset
= boot_info
.file_size
& (64 * 1024);
367 if (offset
< boot_info
.file_size
)
370 offset
+= sizeof(struct fw_header
);
373 memset(hdr
, '\xff', sizeof(struct fw_header
));
375 hdr
->version
= htonl(bswap_32(hdr_ver
));
376 ver_len
= strlen(version
);
377 if (ver_len
> (sizeof(hdr
->fw_version
) - 1))
378 ver_len
= sizeof(hdr
->fw_version
) - 1;
380 memcpy(hdr
->fw_version
, version
, ver_len
);
381 hdr
->fw_version
[ver_len
] = 0;
383 hdr
->hw_id
= htonl(board
->hw_id
);
384 hdr
->hw_rev
= htonl(board
->hw_rev
);
385 hdr
->hw_ver_add
= htonl(board
->hw_ver_add
);
387 if (boot_info
.file_size
== 0 || !with_bootloader
) {
388 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
389 hdr
->boot_ofs
= htonl(0);
390 hdr
->boot_len
= htonl(0);
392 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
393 hdr
->boot_ofs
= htonl(0);
394 hdr
->boot_len
= htonl(boot_info
.file_size
);
397 hdr
->kernel_la
= htonl(kernel_la
);
398 hdr
->kernel_ep
= htonl(kernel_ep
);
399 hdr
->fw_length
= htonl(layout
->fw_max_len
+ offset
);
400 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
) + offset
);
401 hdr
->kernel_len
= htonl(kernel_len
);
403 /* even the bootloader doesnt increased the root_ofs. Unsure if this parser
404 * always ignores the rootfs or only in the 841v13
406 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
407 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
410 hdr
->unk2
= htonl(0);
411 hdr
->unk3
= htonl(0xffffffff);
412 hdr
->unk4
= htons(0x55aa);
415 hdr
->sver_hi
= sver_hi
;
416 hdr
->sver_lo
= sver_lo
;
418 hdr
->ver_hi
= fw_ver_hi
;
419 hdr
->ver_mid
= fw_ver_mid
;
420 hdr
->ver_lo
= fw_ver_lo
;
422 if (board
->flags
& FLAG_LE_KERNEL_LA_EP
) {
423 hdr
->kernel_la
= bswap_32(hdr
->kernel_la
);
424 hdr
->kernel_ep
= bswap_32(hdr
->kernel_ep
);
427 get_md5(buf
, len
, hdr
->md5sum1
);
430 /* fill_header get called by mktplinkfw_lib to fill the header in front of the kernel. */
431 void fill_header(char *buf
, int len
) {
432 fill_header_bootloader(buf
, len
, 0);
435 static int inspect_fw(void)
438 struct fw_header
*hdr
;
439 uint8_t md5sum
[MD5SUM_LEN
];
440 struct board_info
*board
;
441 int ret
= EXIT_FAILURE
;
443 buf
= malloc(inspect_info
.file_size
);
445 ERR("no memory for buffer!\n");
449 ret
= read_to_buf(&inspect_info
, buf
);
452 hdr
= (struct fw_header
*)buf
;
454 board
= &custom_board
;
456 if (board
->flags
& FLAG_LE_KERNEL_LA_EP
) {
457 hdr
->kernel_la
= bswap_32(hdr
->kernel_la
);
458 hdr
->kernel_ep
= bswap_32(hdr
->kernel_ep
);
461 inspect_fw_pstr("File name", inspect_info
.file_name
);
462 inspect_fw_phexdec("File size", inspect_info
.file_size
);
464 switch(bswap_32(ntohl(hdr
->version
))) {
469 ERR("file does not seem to have V2/V3 header!\n");
473 inspect_fw_phexdec("Version 2 Header size", sizeof(struct fw_header
));
475 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
476 if (ntohl(hdr
->boot_len
) == 0)
477 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
479 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
480 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
482 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
483 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
484 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
486 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
488 if (ntohl(hdr
->unk2
) != 0)
489 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
490 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
491 "(purpose yet unknown, unchecked here)");
493 if (ntohl(hdr
->unk3
) != 0xffffffff)
494 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
496 if (ntohs(hdr
->unk4
) != 0x55aa)
497 inspect_fw_phexdec("Unknown value 4", hdr
->unk4
);
499 if (hdr
->unk5
!= 0xa5)
500 inspect_fw_phexdec("Unknown value 5", hdr
->unk5
);
504 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
505 inspect_fw_phex("Hardware ID", ntohl(hdr
->hw_id
));
506 inspect_fw_phex("Hardware Revision",
508 inspect_fw_phex("Additional HW Version",
509 ntohl(hdr
->hw_ver_add
));
511 printf("%-23s: %d.%d.%d-%d.%d\n", "Software version",
512 hdr
->ver_hi
, hdr
->ver_mid
, hdr
->ver_lo
,
513 hdr
->sver_hi
, hdr
->sver_lo
);
517 inspect_fw_phexdec("Kernel data offset",
518 ntohl(hdr
->kernel_ofs
));
519 inspect_fw_phexdec("Kernel data length",
520 ntohl(hdr
->kernel_len
));
521 inspect_fw_phex("Kernel load address",
522 ntohl(hdr
->kernel_la
));
523 inspect_fw_phex("Kernel entry point",
524 ntohl(hdr
->kernel_ep
));
525 inspect_fw_phexdec("Rootfs data offset",
526 ntohl(hdr
->rootfs_ofs
));
527 inspect_fw_phexdec("Rootfs data length",
528 ntohl(hdr
->rootfs_len
));
529 inspect_fw_phexdec("Boot loader data offset",
530 ntohl(hdr
->boot_ofs
));
531 inspect_fw_phexdec("Boot loader data length",
532 ntohl(hdr
->boot_len
));
533 inspect_fw_phexdec("Total firmware length",
534 ntohl(hdr
->fw_length
));
543 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
544 sprintf(filename
, "%s-bootloader", inspect_info
.file_name
);
545 printf("Extracting bootloader to \"%s\"...\n", filename
);
546 fp
= fopen(filename
, "w");
548 if (!fwrite(buf
+ sizeof(struct fw_header
) + ntohl(hdr
->boot_ofs
),
549 ntohl(hdr
->boot_len
), 1, fp
)) {
550 ERR("error in fwrite(): %s", strerror(errno
));
554 ERR("error in fopen(): %s", strerror(errno
));
559 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
560 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
561 printf("Extracting kernel to \"%s\"...\n", filename
);
562 fp
= fopen(filename
, "w");
564 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
565 ntohl(hdr
->kernel_len
), 1, fp
)) {
566 ERR("error in fwrite(): %s", strerror(errno
));
570 ERR("error in fopen(): %s", strerror(errno
));
574 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
575 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
576 printf("Extracting rootfs to \"%s\"...\n", filename
);
577 fp
= fopen(filename
, "w");
579 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
580 ntohl(hdr
->rootfs_len
), 1, fp
)) {
581 ERR("error in fwrite(): %s", strerror(errno
));
585 ERR("error in fopen(): %s", strerror(errno
));
596 /* prepend a second image header and the bootloader.
612 * The padding is optional. The second image header should begin a 64k boundary.
614 int prepend_bootloader() {
615 unsigned int buflen
= 0;
616 int ret
= 0, bootloader_padded
= 0;
617 char *buf
= 0, *p
= 0;
618 struct file_info image
;
620 /* calculate blocks to ensure padding */
621 bootloader_padded
= boot_info
.file_size
& (64 * 1024);
622 if (bootloader_padded
< boot_info
.file_size
)
623 bootloader_padded
+= 64 * 1024;
625 image
.file_name
= ofname
;
626 ret
= get_file_stat(&image
);
628 ERR("Can not load the output image");
632 buflen
= image
.file_size
+ bootloader_padded
+ sizeof(struct fw_header
);
633 buf
= malloc(buflen
);
635 ERR("Can not allocate buffer %d bytes", buflen
);
638 memset(buf
, 0xff, buflen
);
641 p
= buf
+ bootloader_padded
+ sizeof(struct fw_header
);
642 ret
= read_to_buf(&image
, p
);
644 ERR("Can not read output image");
648 p
= buf
+ sizeof(struct fw_header
);
649 ret
= read_to_buf(&boot_info
, p
);
651 fill_header_bootloader(buf
, buflen
, 1);
653 ret
= write_fw(ofname
, buf
, buflen
);
665 int main(int argc
, char *argv
[])
667 int ret
= EXIT_FAILURE
;
669 progname
= basename(argv
[0]);
674 c
= getopt(argc
, argv
, "a:b:H:E:F:L:V:N:W:w:ci:k:r:R:o:xhsjv:y:T:e");
680 sscanf(optarg
, "0x%x", &rootfs_align
);
683 boot_info
.file_name
= optarg
;
689 sscanf(optarg
, "0x%x", &kernel_ep
);
698 opt_hw_ver_add
= optarg
;
701 sscanf(optarg
, "0x%x", &kernel_la
);
719 kernel_info
.file_name
= optarg
;
722 rootfs_info
.file_name
= optarg
;
725 sscanf(optarg
, "0x%x", &rootfs_ofs
);
734 inspect_info
.file_name
= optarg
;
743 hdr_ver
= atoi(optarg
);
746 custom_board
.flags
= FLAG_LE_KERNEL_LA_EP
;
757 ret
= check_options();
761 if (!inspect_info
.file_name
) {
762 ret
= build_fw(sizeof(struct fw_header
));
763 if (ret
== 0 && boot_info
.file_size
> 0)
764 ret
= prepend_bootloader();