1 // SPDX-License-Identifier: GPL-2.0-only
3 * Builder/viewer/extractor utility for Poray firmware image files
5 * Copyright (C) 2013 Michel Stempin <michel.stempin@wanadoo.fr>
6 * Copyright (C) 2013 Felix Kaechele <felix@fetzig.org>
7 * Copyright (C) 2013 <admin@openschemes.com>
9 * This tool is based on:
10 * TP-Link firmware upgrade tool.
11 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
14 * TP-Link WR941 V2 firmware checksum fixing tool.
15 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
29 #include <arpa/inet.h>
30 #include <netinet/in.h>
32 #if (__BYTE_ORDER == __BIG_ENDIAN)
33 # define HOST_TO_BE32(x) (x)
34 # define BE32_TO_HOST(x) (x)
35 # define HOST_TO_LE32(x) bswap_32(x)
36 # define LE32_TO_HOST(x) bswap_32(x)
38 # define HOST_TO_BE32(x) bswap_32(x)
39 # define BE32_TO_HOST(x) bswap_32(x)
40 # define HOST_TO_LE32(x) (x)
41 # define LE32_TO_HOST(x) (x)
44 /* Fixed header flags */
45 #define HEADER_FLAGS 0x020e0000
47 /* Recognized Hardware ID magic */
48 #define HWID_HAME_MPR_A1_L8 0x32473352
49 #define HWID_PORAY_R50B 0x31353033
50 #define HWID_PORAY_R50D 0x33353033
51 #define HWID_PORAY_R50E 0x34353033
52 #define HWID_PORAY_M3 0x31353335
53 #define HWID_PORAY_M4 0x32353335
54 #define HWID_PORAY_Q3 0x33353335
55 #define HWID_PORAY_X5_X6 0x35353335
56 #define HWID_PORAY_X8 0x36353335
57 #define HWID_PORAY_X1 0x38353335
58 #define HWID_NEXX_WT1520 0x30353332
59 #define HWID_NEXX_WT3020 0x30323033
60 #define HWID_A5_V11 0x32473352
62 /* Recognized XOR obfuscation keys */
76 char *file_name
; /* Name of the file */
77 uint32_t file_size
; /* Length of the file */
81 uint32_t hw_id
; /* Hardware id */
82 uint32_t firmware_len
; /* Firmware data length */
83 uint32_t flags
; /* Header flags */
85 } __attribute__ ((packed
));
103 static char *progname
;
105 static char *board_id
;
106 static struct board_info
*board
;
107 static char *layout_id
;
108 static struct flash_layout
*layout
;
109 static char *opt_hw_id
;
110 static uint32_t hw_id
;
111 static struct file_info firmware_info
;
112 static uint32_t firmware_len
= 0;
114 static int inspect
= 0;
115 static int extract
= 0;
117 static uint8_t key
[][KEY_LEN
] = {
118 {0xC8, 0x3C, 0x3A, 0x93, 0xA2, 0x95, 0xC3, 0x63, 0x48, 0x45, 0x58, 0x09, 0x12, 0x03, 0x08},
119 {0x89, 0x6B, 0x5A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
120 {0xC9, 0x1C, 0x3A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
121 {0x19, 0x1B, 0x3A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
122 {0x79, 0x7B, 0x7A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
123 {0x19, 0x1C, 0x4A, 0x93, 0x96, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0x16, 0xC6},
124 {0x39, 0x1C, 0x4A, 0x93, 0x96, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0x16, 0xC6},
125 {0xC8, 0x3C, 0x3A, 0x93, 0xA2, 0x95, 0xC3, 0x63, 0x48, 0x45, 0x58, 0x09, 0x20, 0x11, 0x08},
128 static struct flash_layout layouts
[] = {
131 .fw_max_len
= 0x3c0000,
134 .fw_max_len
= 0x7c0000,
136 /* terminating entry */
140 static struct board_info boards
[] = {
143 .hw_id
= HWID_A5_V11
,
148 .hw_id
= HWID_HAME_MPR_A1_L8
,
153 .hw_id
= HWID_HAME_MPR_A1_L8
,
158 .hw_id
= HWID_PORAY_R50B
,
163 .hw_id
= HWID_PORAY_R50D
,
168 .hw_id
= HWID_PORAY_R50E
,
173 .hw_id
= HWID_PORAY_M3
,
178 .hw_id
= HWID_PORAY_M4
,
183 .hw_id
= HWID_PORAY_Q3
,
188 .hw_id
= HWID_PORAY_X5_X6
,
193 .hw_id
= HWID_PORAY_X5_X6
,
198 .hw_id
= HWID_PORAY_X5_X6
,
203 .hw_id
= HWID_PORAY_X8
,
208 .hw_id
= HWID_PORAY_X1
,
213 .hw_id
= HWID_NEXX_WT1520
,
218 .hw_id
= HWID_NEXX_WT1520
,
223 .hw_id
= HWID_NEXX_WT3020
,
228 .hw_id
= HWID_NEXX_WT3020
,
236 /* terminating entry */
243 #define ERR(fmt, ...) do { \
245 fprintf(stderr, "[%s] *** error: " fmt "\n", \
246 progname, ## __VA_ARGS__ ); \
249 #define ERRS(fmt, ...) do { \
252 fprintf(stderr, "[%s] *** error: " fmt ":%s\n", \
253 progname, ## __VA_ARGS__, strerror(save)); \
256 #define DBG(fmt, ...) do { \
257 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
261 * Find a board by its name
263 static struct board_info
*find_board(char *id
)
265 struct board_info
*ret
;
266 struct board_info
*board
;
269 for (board
= boards
; board
->id
!= NULL
; board
++){
270 if (strcasecmp(id
, board
->id
) == 0) {
280 * Find a board by its hardware ID
282 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
284 struct board_info
*board
;
286 for (board
= boards
; board
->id
!= NULL
; board
++) {
287 if (hw_id
== board
->hw_id
)
295 * Find a Flash memory layout by its name
297 static struct flash_layout
*find_layout(char *id
)
299 struct flash_layout
*ret
;
300 struct flash_layout
*l
;
303 for (l
= layouts
; l
->id
!= NULL
; l
++){
304 if (strcasecmp(id
, l
->id
) == 0) {
316 static void usage(int status
)
318 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
320 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
324 " -B <board> create image for the board specified with <board>\n"
325 " -H <hwid> use hardware id specified with <hwid>\n"
326 " -F <id> use flash layout specified with <id>\n"
327 " -f <file> read firmware image from the file <file>\n"
328 " -o <file> write output to the file <file>\n"
329 " -i inspect given firmware file (requires -f)\n"
330 " -x extract combined kernel and rootfs while inspecting (implies -i)\n"
331 " -h show this screen\n"
338 * Get file statistics
340 static int get_file_stat(struct file_info
*fdata
)
345 if (fdata
->file_name
== NULL
) {
348 res
= stat(fdata
->file_name
, &st
);
350 ERRS("stat failed on %s", fdata
->file_name
);
354 fdata
->file_size
= st
.st_size
;
359 * Read file into buffer
361 static int read_to_buf(struct file_info
*fdata
, uint8_t *buf
)
364 int ret
= EXIT_FAILURE
;
366 f
= fopen(fdata
->file_name
, "rb");
368 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
373 fread(buf
, fdata
->file_size
, 1, f
);
375 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
388 * Check command line options
390 static int check_options(void)
394 if (firmware_info
.file_name
== NULL
) {
395 ERR("no firmware image specified");
399 ret
= get_file_stat(&firmware_info
);
406 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
407 ERR("either board or hardware id must be specified");
412 board
= find_board(board_id
);
414 ERR("unknown/unsupported board id \"%s\"", board_id
);
417 if (layout_id
== NULL
) {
418 layout_id
= board
->layout_id
;
420 hw_id
= board
->hw_id
;
422 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
423 board
= find_board_by_hwid(hw_id
);
424 if (layout_id
== NULL
) {
425 layout_id
= board
->layout_id
;
429 layout
= find_layout(layout_id
);
430 if (layout
== NULL
) {
431 ERR("unknown flash layout \"%s\"", layout_id
);
435 firmware_len
= firmware_info
.file_size
;
437 if (firmware_info
.file_size
>
438 layout
->fw_max_len
- sizeof (struct fw_header
)) {
439 ERR("firmware image is too big");
443 if (ofname
== NULL
) {
444 ERR("no output file specified");
451 * Fill in firmware header
453 static void fill_header(uint8_t *buf
)
455 struct fw_header
*hdr
= (struct fw_header
*) buf
;
457 memset(hdr
, 0, sizeof (struct fw_header
));
458 hdr
->hw_id
= HOST_TO_LE32(hw_id
);
459 hdr
->firmware_len
= HOST_TO_LE32(firmware_len
);
460 hdr
->flags
= HOST_TO_LE32(HEADER_FLAGS
);
464 * Compute firmware checksum
466 static uint16_t checksum_fw(uint8_t *data
, int len
)
469 int32_t checksum
= 0;
471 for (i
= 0; i
< len
- 1; i
+= 2) {
472 checksum
+= (data
[i
+ 1] << 8) | data
[i
];
477 checksum
= checksum
+ (checksum
>> 16) + 0xffff;
478 checksum
= ~(checksum
+ (checksum
>> 16)) & 0xffff;
479 return (uint16_t) checksum
;
483 * (De)obfuscate firmware using an XOR operation with a fixed length key
485 static void xor_fw(uint8_t *data
, int len
)
489 for (i
= 0; i
<= len
; i
++) {
490 data
[i
] ^= key
[board
->key
][i
% KEY_LEN
];
495 * Write firmware to file
497 static int write_fw(uint8_t *data
, int len
)
500 int ret
= EXIT_FAILURE
;
502 f
= fopen(ofname
, "wb");
504 ERRS("could not open \"%s\" for writing", ofname
);
509 fwrite(data
, len
, 1, f
);
511 ERRS("unable to write output file");
515 DBG("firmware file \"%s\" completed", ofname
);
522 if (ret
!= EXIT_SUCCESS
) {
530 * Build firmware file
532 static int build_fw(void)
536 int ret
= EXIT_FAILURE
;
540 buflen
= layout
->fw_max_len
;
542 buf
= (uint8_t *) malloc(buflen
);
544 ERR("no memory for buffer\n");
548 memset(buf
, 0xff, buflen
);
549 p
= buf
+ sizeof (struct fw_header
);
550 ret
= read_to_buf(&firmware_info
, p
);
554 writelen
= sizeof (struct fw_header
) + firmware_len
+ 2;
559 /* Compute firmware checksum */
560 checksum
= checksum_fw(buf
+ sizeof (struct fw_header
), firmware_len
);
562 /* Cannot use network order function because checksum is not word-aligned */
563 buf
[writelen
- 1] = checksum
>> 8;
564 buf
[writelen
- 2] = checksum
& 0xff;
566 /* XOR obfuscate firmware */
567 xor_fw(buf
+ sizeof (struct fw_header
), firmware_len
+ 2);
569 /* Write firmware file */
570 ret
= write_fw(buf
, writelen
);
582 /* Helper functions to inspect_fw() representing different output formats */
583 static inline void inspect_fw_pstr(char *label
, char *str
)
585 printf("%-23s: %s\n", label
, str
);
588 static inline void inspect_fw_phex(char *label
, uint32_t val
)
590 printf("%-23s: 0x%08x\n", label
, val
);
593 static inline void inspect_fw_phexpost(char *label
,
594 uint32_t val
, char *post
)
596 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
599 static inline void inspect_fw_phexdef(char *label
,
600 uint32_t val
, uint32_t defval
)
602 printf("%-23s: 0x%08x ", label
, val
);
605 printf("(== OpenWrt default)\n");
607 printf("(OpenWrt default: 0x%08x)\n", defval
);
611 static inline void inspect_fw_phexexp(char *label
,
612 uint32_t val
, uint32_t expval
)
614 printf("%-23s: 0x%08x ", label
, val
);
619 printf("(expected: 0x%08x)\n", expval
);
623 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
625 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
628 static inline void inspect_fw_pchecksum(char *label
,
629 uint16_t val
, uint16_t expval
)
631 printf("%-23s: 0x%04x ", label
, val
);
635 printf("(expected: 0x%04x)\n", expval
);
639 static int inspect_fw(void)
642 struct fw_header
*hdr
;
643 int ret
= EXIT_FAILURE
;
644 uint16_t computed_checksum
, file_checksum
;
646 buf
= (uint8_t *) malloc(firmware_info
.file_size
);
648 ERR("no memory for buffer!\n");
652 ret
= read_to_buf(&firmware_info
, buf
);
656 hdr
= (struct fw_header
*)buf
;
658 inspect_fw_pstr("File name", firmware_info
.file_name
);
659 inspect_fw_phexdec("File size", firmware_info
.file_size
);
663 inspect_fw_phexdec("Header size", sizeof (struct fw_header
));
664 board
= find_board_by_hwid(LE32_TO_HOST(hdr
->hw_id
));
666 layout
= find_layout(board
->layout_id
);
667 inspect_fw_phexpost("Hardware ID",
668 LE32_TO_HOST( hdr
->hw_id
), board
->id
);
670 inspect_fw_phexpost("Hardware ID",
671 LE32_TO_HOST(hdr
->hw_id
), "unknown");
673 inspect_fw_phexdec("Firmware data length",
674 LE32_TO_HOST(hdr
->firmware_len
));
676 inspect_fw_phexexp("Flags",
677 LE32_TO_HOST(hdr
->flags
), HEADER_FLAGS
);
680 /* XOR unobfuscate firmware */
681 xor_fw(buf
+ sizeof (struct fw_header
), LE32_TO_HOST(hdr
->firmware_len
) + 2);
683 /* Compute firmware checksum */
684 computed_checksum
= checksum_fw(buf
+ sizeof (struct fw_header
), LE32_TO_HOST(hdr
->firmware_len
));
686 /* Cannot use network order function because checksum is not word-aligned */
687 file_checksum
= (buf
[firmware_info
.file_size
- 1] << 8) | buf
[firmware_info
.file_size
- 2];
688 inspect_fw_pchecksum("Firmware checksum", computed_checksum
, file_checksum
);
690 /* Verify checksum */
691 if (computed_checksum
!= file_checksum
) {
693 ERR("checksums do not match");
703 if (ofname
== NULL
) {
704 filename
= malloc(strlen(firmware_info
.file_name
) + 10);
705 sprintf(filename
, "%s-firmware", firmware_info
.file_name
);
709 printf("Extracting firmware to \"%s\"...\n", filename
);
710 fp
= fopen(filename
, "wb");
712 if (!fwrite(buf
+ sizeof (struct fw_header
),
713 LE32_TO_HOST(hdr
->firmware_len
), 1, fp
)) {
714 ERRS("error in fwrite(): %s", strerror(errno
));
718 ERRS("error in fopen(): %s", strerror(errno
));
720 if (ofname
== NULL
) {
735 int main(int argc
, char *argv
[])
737 int ret
= EXIT_FAILURE
;
739 progname
= basename(argv
[0]);
743 while ((c
= getopt(argc
, argv
, "B:H:F:f:o:ixh")) != -1) {
755 firmware_info
.file_name
= optarg
;
776 ret
= check_options();