e6d76a194279192bdb3bde1e3c4ca2e121cbc5eb
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>
14 #include <unistd.h> /* for unlink() */
16 #include <getopt.h> /* for getopt() */
23 #include <arpa/inet.h>
24 #include <netinet/in.h>
27 #include "mktplinkfw-lib.h"
29 #define HEADER_VERSION_V1 0x01000000
30 #define HEADER_VERSION_V2 0x02000000
33 uint32_t version
; /* header version */
36 uint32_t hw_id
; /* hardware id */
37 uint32_t hw_rev
; /* hardware revision */
38 uint32_t region_code
; /* region code */
39 uint8_t md5sum1
[MD5SUM_LEN
];
41 uint8_t md5sum2
[MD5SUM_LEN
];
43 uint32_t kernel_la
; /* kernel load address */
44 uint32_t kernel_ep
; /* kernel entry point */
45 uint32_t fw_length
; /* total length of the firmware */
46 uint32_t kernel_ofs
; /* kernel data offset */
47 uint32_t kernel_len
; /* kernel data length */
48 uint32_t rootfs_ofs
; /* rootfs data offset */
49 uint32_t rootfs_len
; /* rootfs data length */
50 uint32_t boot_ofs
; /* bootloader data offset */
51 uint32_t boot_len
; /* bootloader data length */
59 } __attribute__ ((packed
));
72 static char *vendor
= "TP-LINK Technologies";
73 static char *version
= "ver. 1.0";
74 static char *fw_ver
= "0.0.0";
75 static uint32_t hdr_ver
= HEADER_VERSION_V1
;
77 static char *layout_id
;
78 struct flash_layout
*layout
;
79 static char *opt_hw_id
;
80 static uint32_t hw_id
;
81 static char *opt_hw_rev
;
82 static uint32_t hw_rev
;
83 static uint32_t opt_hdr_ver
= 1;
85 static const struct fw_region
*region
;
87 static int fw_ver_mid
;
89 struct file_info kernel_info
;
90 static uint32_t kernel_la
= 0;
91 static uint32_t kernel_ep
= 0;
92 uint32_t kernel_len
= 0;
93 struct file_info rootfs_info
;
94 uint32_t rootfs_ofs
= 0;
95 uint32_t rootfs_align
;
96 static struct file_info boot_info
;
100 static uint32_t fw_max_len
;
101 static uint32_t reserved_space
;
103 static struct file_info inspect_info
;
104 static int extract
= 0;
105 static bool endian_swap
= false;
106 static bool rootfs_ofs_calc
= false;
108 static const char md5salt_normal
[MD5SUM_LEN
] = {
109 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
110 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
113 static const char md5salt_boot
[MD5SUM_LEN
] = {
114 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
115 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
118 static struct flash_layout layouts
[] = {
121 .fw_max_len
= 0x3c0000,
122 .kernel_la
= 0x80060000,
123 .kernel_ep
= 0x80060000,
124 .rootfs_ofs
= 0x140000,
127 .fw_max_len
= 0x3c0000,
128 .kernel_la
= 0x80060000,
129 .kernel_ep
= 0x80060000,
130 .rootfs_ofs
= 0x100000,
133 .fw_max_len
= 0x7c0000,
134 .kernel_la
= 0x80060000,
135 .kernel_ep
= 0x80060000,
136 .rootfs_ofs
= 0x140000,
139 .fw_max_len
= 0x7c0000,
140 .kernel_la
= 0x80060000,
141 .kernel_ep
= 0x80060000,
142 .rootfs_ofs
= 0x100000,
145 .fw_max_len
= 0x7c0000,
146 .kernel_la
= 0x80000000,
147 .kernel_ep
= 0x8000c310,
148 .rootfs_ofs
= 0x100000,
151 .fw_max_len
= 0xf80000,
152 .kernel_la
= 0x80060000,
153 .kernel_ep
= 0x80060000,
154 .rootfs_ofs
= 0x140000,
157 .fw_max_len
= 0xf80000,
158 .kernel_la
= 0x80060000,
159 .kernel_ep
= 0x80060000,
160 .rootfs_ofs
= 0x100000,
163 .fw_max_len
= 0xf80000,
164 .kernel_la
= 0x00000000 ,
165 .kernel_ep
= 0xc0000000,
166 .rootfs_ofs
= 0x2a0000,
168 /* terminating entry */
172 static const struct fw_region regions
[] = {
173 /* Default region (universal) uses code 0 as well */
179 static const struct fw_region
* find_region(const char *country
) {
182 for (i
= 0; i
< ARRAY_SIZE(regions
); i
++) {
183 if (strcasecmp(regions
[i
].name
, country
) == 0)
190 static void usage(int status
)
192 fprintf(stderr
, "Usage: %s [OPTIONS...]\n", progname
);
196 " -c use combined kernel image\n"
197 " -e swap endianness in kernel load address and entry point\n"
198 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
199 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
200 " -H <hwid> use hardware id specified with <hwid>\n"
201 " -W <hwrev> use hardware revision specified with <hwrev>\n"
202 " -C <country> set region code to <country>\n"
203 " -F <id> use flash layout specified with <id>\n"
204 " -k <file> read kernel image from the file <file>\n"
205 " -r <file> read rootfs image from the file <file>\n"
206 " -a <align> align the rootfs start on an <align> bytes boundary\n"
207 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
208 " -O calculate rootfs offset for combined images\n"
209 " -o <file> write output to the file <file>\n"
210 " -s strip padding from the end of the image\n"
211 " -j add jffs2 end-of-filesystem markers\n"
212 " -N <vendor> set image vendor to <vendor>\n"
213 " -V <version> set image version to <version>\n"
214 " -v <version> set firmware version to <version>\n"
215 " -m <version> set header version to <version>\n"
216 " -i <file> inspect given firmware file <file>\n"
217 " -x extract kernel and rootfs while inspecting (requires -i)\n"
218 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
219 " -h show this screen\n"
225 static int check_options(void)
230 if (inspect_info
.file_name
) {
231 ret
= get_file_stat(&inspect_info
);
236 } else if (extract
) {
237 ERR("no firmware for inspection specified");
241 if (opt_hw_id
== NULL
) {
242 ERR("hardware id not specified");
245 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
247 if (!combined
&& layout_id
== NULL
) {
248 ERR("flash layout is not specified");
253 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
258 region
= find_region(country
);
260 ERR("unknown region code \"%s\"", country
);
266 if (!kernel_la
|| !kernel_ep
) {
267 ERR("kernel loading address and entry point must be specified for combined image");
271 layout
= find_layout(layouts
, layout_id
);
272 if (layout
== NULL
) {
273 ERR("unknown flash layout \"%s\"", layout_id
);
278 kernel_la
= layout
->kernel_la
;
280 kernel_ep
= layout
->kernel_ep
;
282 rootfs_ofs
= layout
->rootfs_ofs
;
284 if (reserved_space
> layout
->fw_max_len
) {
285 ERR("reserved space is not valid");
290 if (kernel_info
.file_name
== NULL
) {
291 ERR("no kernel image specified");
295 ret
= get_file_stat(&kernel_info
);
299 kernel_len
= kernel_info
.file_size
;
302 fw_max_len
= layout
->fw_max_len
- reserved_space
;
304 if (rootfs_info
.file_name
== NULL
) {
305 ERR("no rootfs image specified");
309 ret
= get_file_stat(&rootfs_info
);
314 kernel_len
+= sizeof(struct fw_header
);
315 rootfs_ofs
= ALIGN(kernel_len
, rootfs_align
);
316 kernel_len
-= sizeof(struct fw_header
);
318 DBG("rootfs offset aligned to 0x%u", rootfs_ofs
);
320 exceed_bytes
= kernel_len
+ rootfs_info
.file_size
- (fw_max_len
- sizeof(struct fw_header
));
321 if (exceed_bytes
> 0) {
322 ERR("images are too big by %i bytes", exceed_bytes
);
326 exceed_bytes
= kernel_info
.file_size
- (rootfs_ofs
- sizeof(struct fw_header
));
327 if (exceed_bytes
> 0) {
328 ERR("kernel image is too big by %i bytes", exceed_bytes
);
332 exceed_bytes
= rootfs_info
.file_size
- (fw_max_len
- rootfs_ofs
);
333 if (exceed_bytes
> 0) {
334 ERR("rootfs image is too big by %i bytes", exceed_bytes
);
340 if (ofname
== NULL
) {
341 ERR("no output file specified");
345 ret
= sscanf(fw_ver
, "%d.%d.%d", &fw_ver_hi
, &fw_ver_mid
, &fw_ver_lo
);
347 ERR("invalid firmware version '%s'", fw_ver
);
351 if (opt_hdr_ver
== 1) {
352 hdr_ver
= HEADER_VERSION_V1
;
353 } else if (opt_hdr_ver
== 2) {
354 hdr_ver
= HEADER_VERSION_V2
;
356 ERR("invalid header version '%u'", opt_hdr_ver
);
363 void fill_header(char *buf
, int len
)
365 struct fw_header
*hdr
= (struct fw_header
*)buf
;
367 memset(hdr
, 0, sizeof(struct fw_header
));
369 hdr
->version
= htonl(hdr_ver
);
370 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
371 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
372 hdr
->hw_id
= htonl(hw_id
);
373 hdr
->hw_rev
= htonl(hw_rev
);
375 hdr
->kernel_la
= htonl(kernel_la
);
376 hdr
->kernel_ep
= htonl(kernel_ep
);
377 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
378 hdr
->kernel_len
= htonl(kernel_len
);
381 if (boot_info
.file_size
== 0)
382 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
384 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
386 hdr
->fw_length
= htonl(layout
->fw_max_len
);
387 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
388 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
391 if (combined
&& rootfs_ofs_calc
) {
392 hdr
->rootfs_ofs
= htonl(sizeof(struct fw_header
) + kernel_len
);
395 hdr
->ver_hi
= htons(fw_ver_hi
);
396 hdr
->ver_mid
= htons(fw_ver_mid
);
397 hdr
->ver_lo
= htons(fw_ver_lo
);
400 hdr
->region_code
= htonl(region
->code
);
402 hdr
->region_str1
, sizeof(hdr
->region_str1
), "00000000;%02X%02X%02X%02X;",
403 region
->name
[0], region
->name
[1], region
->name
[2], region
->name
[3]
406 hdr
->region_str2
, sizeof(hdr
->region_str2
), "%02X%02X%02X%02X",
407 region
->name
[0], region
->name
[1], region
->name
[2], region
->name
[3]
412 hdr
->kernel_la
= bswap_32(hdr
->kernel_la
);
413 hdr
->kernel_ep
= bswap_32(hdr
->kernel_ep
);
417 get_md5(buf
, len
, hdr
->md5sum1
);
420 static int inspect_fw(void)
423 struct fw_header
*hdr
;
424 uint8_t md5sum
[MD5SUM_LEN
];
425 int ret
= EXIT_FAILURE
;
427 buf
= malloc(inspect_info
.file_size
);
429 ERR("no memory for buffer!\n");
433 ret
= read_to_buf(&inspect_info
, buf
);
436 hdr
= (struct fw_header
*)buf
;
438 inspect_fw_pstr("File name", inspect_info
.file_name
);
439 inspect_fw_phexdec("File size", inspect_info
.file_size
);
441 if ((ntohl(hdr
->version
) != HEADER_VERSION_V1
) &&
442 (ntohl(hdr
->version
) != HEADER_VERSION_V2
)) {
443 ERR("file does not seem to have V1/V2 header!\n");
447 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
449 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
450 if (ntohl(hdr
->boot_len
) == 0)
451 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
453 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
454 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
456 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
457 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
458 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
460 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
462 if (ntohl(hdr
->unk2
) != 0)
463 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
464 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
465 "(purpose yet unknown, unchecked here)");
466 if (ntohl(hdr
->unk3
) != 0)
467 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
471 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
472 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
473 inspect_fw_phex("Hardware ID", ntohl(hdr
->hw_id
));
474 inspect_fw_phex("Hardware Revision", ntohl(hdr
->hw_rev
));
475 inspect_fw_phex("Region code", ntohl(hdr
->region_code
));
479 inspect_fw_phexdec("Kernel data offset",
480 ntohl(hdr
->kernel_ofs
));
481 inspect_fw_phexdec("Kernel data length",
482 ntohl(hdr
->kernel_len
));
483 inspect_fw_phex("Kernel load address",
484 ntohl(hdr
->kernel_la
));
485 inspect_fw_phex("Kernel entry point",
486 ntohl(hdr
->kernel_ep
));
487 inspect_fw_phexdec("Rootfs data offset",
488 ntohl(hdr
->rootfs_ofs
));
489 inspect_fw_phexdec("Rootfs data length",
490 ntohl(hdr
->rootfs_len
));
491 inspect_fw_phexdec("Boot loader data offset",
492 ntohl(hdr
->boot_ofs
));
493 inspect_fw_phexdec("Boot loader data length",
494 ntohl(hdr
->boot_len
));
495 inspect_fw_phexdec("Total firmware length",
496 ntohl(hdr
->fw_length
));
504 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
505 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
506 printf("Extracting kernel to \"%s\"...\n", filename
);
507 fp
= fopen(filename
, "w");
509 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
510 ntohl(hdr
->kernel_len
), 1, fp
)) {
511 ERR("error in fwrite(): %s", strerror(errno
));
515 ERR("error in fopen(): %s", strerror(errno
));
519 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
520 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
521 printf("Extracting rootfs to \"%s\"...\n", filename
);
522 fp
= fopen(filename
, "w");
524 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
525 ntohl(hdr
->rootfs_len
), 1, fp
)) {
526 ERR("error in fwrite(): %s", strerror(errno
));
530 ERR("error in fopen(): %s", strerror(errno
));
541 int main(int argc
, char *argv
[])
543 int ret
= EXIT_FAILURE
;
545 progname
= basename(argv
[0]);
550 c
= getopt(argc
, argv
, "a:H:E:F:L:m:V:N:W:C:ci:k:r:R:o:OxX:ehsjv:");
556 sscanf(optarg
, "0x%x", &rootfs_align
);
562 sscanf(optarg
, "0x%x", &kernel_ep
);
574 sscanf(optarg
, "0x%x", &kernel_la
);
577 sscanf(optarg
, "%u", &opt_hdr_ver
);
592 kernel_info
.file_name
= optarg
;
595 rootfs_info
.file_name
= optarg
;
598 sscanf(optarg
, "0x%x", &rootfs_ofs
);
610 inspect_info
.file_name
= optarg
;
625 sscanf(optarg
, "0x%x", &reserved_space
);
633 ret
= check_options();
637 if (!inspect_info
.file_name
)
638 ret
= build_fw(sizeof(struct fw_header
));