2 * Builder/viewer/extractor utility for Poray firmware image files
4 * Copyright (C) 2013 Michel Stempin <michel.stempin@wanadoo.fr>
5 * Copyright (C) 2013 Felix Kaechele <felix@fetzig.org>
6 * Copyright (C) 2013 <admin@openschemes.com>
8 * This tool is based on:
9 * TP-Link firmware upgrade tool.
10 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
13 * TP-Link WR941 V2 firmware checksum fixing tool.
14 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License version 2 as published
18 * by the Free Software Foundation.
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
35 #if (__BYTE_ORDER == __BIG_ENDIAN)
36 # define HOST_TO_BE32(x) (x)
37 # define BE32_TO_HOST(x) (x)
38 # define HOST_TO_LE32(x) bswap_32(x)
39 # define LE32_TO_HOST(x) bswap_32(x)
41 # define HOST_TO_BE32(x) bswap_32(x)
42 # define BE32_TO_HOST(x) bswap_32(x)
43 # define HOST_TO_LE32(x) (x)
44 # define LE32_TO_HOST(x) (x)
47 /* Fixed header flags */
48 #define HEADER_FLAGS 0x020e0000
50 /* Recognized Hardware ID magic */
51 #define HWID_HAME_MPR_A1_L8 0x32473352
52 #define HWID_PORAY_R50B 0x31353033
53 #define HWID_PORAY_R50D 0x33353033
54 #define HWID_PORAY_R50E 0x34353033
55 #define HWID_PORAY_M3 0x31353335
56 #define HWID_PORAY_M4 0x32353335
57 #define HWID_PORAY_Q3 0x33353335
58 #define HWID_PORAY_X5_X6 0x35353335
59 #define HWID_PORAY_X8 0x36353335
60 #define HWID_PORAY_X1 0x38353335
61 #define HWID_NEXX_WT1520 0x30353332
62 #define HWID_NEXX_WT3020 0x30323033
63 #define HWID_A5_V11 0x32473352
65 /* Recognized XOR obfuscation keys */
79 char *file_name; /* Name of the file */
80 uint32_t file_size; /* Length of the file */
84 uint32_t hw_id; /* Hardware id */
85 uint32_t firmware_len; /* Firmware data length */
86 uint32_t flags; /* Header flags */
88 } __attribute__ ((packed));
106 static char *progname;
108 static char *board_id;
109 static struct board_info *board;
110 static char *layout_id;
111 static struct flash_layout *layout;
112 static char *opt_hw_id;
113 static uint32_t hw_id;
114 static struct file_info firmware_info;
115 static uint32_t firmware_len = 0;
117 static int inspect = 0;
118 static int extract = 0;
120 static uint8_t key[][KEY_LEN] = {
121 {0xC8, 0x3C, 0x3A, 0x93, 0xA2, 0x95, 0xC3, 0x63, 0x48, 0x45, 0x58, 0x09, 0x12, 0x03, 0x08},
122 {0x89, 0x6B, 0x5A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
123 {0xC9, 0x1C, 0x3A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
124 {0x19, 0x1B, 0x3A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
125 {0x79, 0x7B, 0x7A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
126 {0x19, 0x1C, 0x4A, 0x93, 0x96, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0x16, 0xC6},
127 {0x39, 0x1C, 0x4A, 0x93, 0x96, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0x16, 0xC6},
128 {0xC8, 0x3C, 0x3A, 0x93, 0xA2, 0x95, 0xC3, 0x63, 0x48, 0x45, 0x58, 0x09, 0x20, 0x11, 0x08},
131 static struct flash_layout layouts[] = {
134 .fw_max_len = 0x3c0000,
137 .fw_max_len = 0x7c0000,
139 /* terminating entry */
143 static struct board_info boards[] = {
146 .hw_id = HWID_A5_V11,
151 .hw_id = HWID_HAME_MPR_A1_L8,
156 .hw_id = HWID_HAME_MPR_A1_L8,
161 .hw_id = HWID_PORAY_R50B,
166 .hw_id = HWID_PORAY_R50D,
171 .hw_id = HWID_PORAY_R50E,
176 .hw_id = HWID_PORAY_M3,
181 .hw_id = HWID_PORAY_M4,
186 .hw_id = HWID_PORAY_Q3,
191 .hw_id = HWID_PORAY_X5_X6,
196 .hw_id = HWID_PORAY_X5_X6,
201 .hw_id = HWID_PORAY_X5_X6,
206 .hw_id = HWID_PORAY_X8,
211 .hw_id = HWID_PORAY_X1,
216 .hw_id = HWID_NEXX_WT1520,
221 .hw_id = HWID_NEXX_WT1520,
226 .hw_id = HWID_NEXX_WT3020,
231 .hw_id = HWID_NEXX_WT3020,
239 /* terminating entry */
246 #define ERR(fmt, ...) do { \
248 fprintf(stderr, "[%s] *** error: " fmt "\n", \
249 progname, ## __VA_ARGS__ ); \
252 #define ERRS(fmt, ...) do { \
255 fprintf(stderr, "[%s] *** error: " fmt ":%s\n", \
256 progname, ## __VA_ARGS__, strerror(save)); \
259 #define DBG(fmt, ...) do { \
260 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
264 * Find a board by its name
266 static struct board_info *find_board(char *id)
268 struct board_info *ret;
269 struct board_info *board;
272 for (board = boards; board->id != NULL; board++){
273 if (strcasecmp(id, board->id) == 0) {
283 * Find a board by its hardware ID
285 static struct board_info *find_board_by_hwid(uint32_t hw_id)
287 struct board_info *board;
289 for (board = boards; board->id != NULL; board++) {
290 if (hw_id == board->hw_id)
298 * Find a Flash memory layout by its name
300 static struct flash_layout *find_layout(char *id)
302 struct flash_layout *ret;
303 struct flash_layout *l;
306 for (l = layouts; l->id != NULL; l++){
307 if (strcasecmp(id, l->id) == 0) {
319 static void usage(int status)
321 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
323 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
327 " -B <board> create image for the board specified with <board>\n"
328 " -H <hwid> use hardware id specified with <hwid>\n"
329 " -F <id> use flash layout specified with <id>\n"
330 " -f <file> read firmware image from the file <file>\n"
331 " -o <file> write output to the file <file>\n"
332 " -i inspect given firmware file (requires -f)\n"
333 " -x extract combined kernel and rootfs while inspecting (implies -i)\n"
334 " -h show this screen\n"
341 * Get file statistics
343 static int get_file_stat(struct file_info *fdata)
348 if (fdata->file_name == NULL) {
351 res = stat(fdata->file_name, &st);
353 ERRS("stat failed on %s", fdata->file_name);
357 fdata->file_size = st.st_size;
362 * Read file into buffer
364 static int read_to_buf(struct file_info *fdata, uint8_t *buf)
367 int ret = EXIT_FAILURE;
369 f = fopen(fdata->file_name, "rb");
371 ERRS("could not open \"%s\" for reading", fdata->file_name);
376 fread(buf, fdata->file_size, 1, f);
378 ERRS("unable to read from file \"%s\"", fdata->file_name);
391 * Check command line options
393 static int check_options(void)
397 if (firmware_info.file_name == NULL) {
398 ERR("no firmware image specified");
402 ret = get_file_stat(&firmware_info);
409 if (board_id == NULL && opt_hw_id == NULL) {
410 ERR("either board or hardware id must be specified");
415 board = find_board(board_id);
417 ERR("unknown/unsupported board id \"%s\"", board_id);
420 if (layout_id == NULL) {
421 layout_id = board->layout_id;
423 hw_id = board->hw_id;
425 hw_id = strtoul(opt_hw_id, NULL, 0);
426 board = find_board_by_hwid(hw_id);
427 if (layout_id == NULL) {
428 layout_id = board->layout_id;
432 layout = find_layout(layout_id);
433 if (layout == NULL) {
434 ERR("unknown flash layout \"%s\"", layout_id);
438 firmware_len = firmware_info.file_size;
440 if (firmware_info.file_size >
441 layout->fw_max_len - sizeof (struct fw_header)) {
442 ERR("firmware image is too big");
446 if (ofname == NULL) {
447 ERR("no output file specified");
454 * Fill in firmware header
456 static void fill_header(uint8_t *buf)
458 struct fw_header *hdr = (struct fw_header *) buf;
460 memset(hdr, 0, sizeof (struct fw_header));
461 hdr->hw_id = HOST_TO_LE32(hw_id);
462 hdr->firmware_len = HOST_TO_LE32(firmware_len);
463 hdr->flags = HOST_TO_LE32(HEADER_FLAGS);
467 * Compute firmware checksum
469 static uint16_t checksum_fw(uint8_t *data, int len)
472 int32_t checksum = 0;
474 for (i = 0; i < len - 1; i += 2) {
475 checksum += (data[i + 1] << 8) | data[i];
480 checksum = checksum + (checksum >> 16) + 0xffff;
481 checksum = ~(checksum + (checksum >> 16)) & 0xffff;
482 return (uint16_t) checksum;
486 * (De)obfuscate firmware using an XOR operation with a fixed length key
488 static void xor_fw(uint8_t *data, int len)
492 for (i = 0; i <= len; i++) {
493 data[i] ^= key[board->key][i % KEY_LEN];
498 * Write firmware to file
500 static int write_fw(uint8_t *data, int len)
503 int ret = EXIT_FAILURE;
505 f = fopen(ofname, "wb");
507 ERRS("could not open \"%s\" for writing", ofname);
512 fwrite(data, len, 1, f);
514 ERRS("unable to write output file");
518 DBG("firmware file \"%s\" completed", ofname);
525 if (ret != EXIT_SUCCESS) {
533 * Build firmware file
535 static int build_fw(void)
539 int ret = EXIT_FAILURE;
543 buflen = layout->fw_max_len;
545 buf = (uint8_t *) malloc(buflen);
547 ERR("no memory for buffer\n");
551 memset(buf, 0xff, buflen);
552 p = buf + sizeof (struct fw_header);
553 ret = read_to_buf(&firmware_info, p);
557 writelen = sizeof (struct fw_header) + firmware_len + 2;
562 /* Compute firmware checksum */
563 checksum = checksum_fw(buf + sizeof (struct fw_header), firmware_len);
565 /* Cannot use network order function because checksum is not word-aligned */
566 buf[writelen - 1] = checksum >> 8;
567 buf[writelen - 2] = checksum & 0xff;
569 /* XOR obfuscate firmware */
570 xor_fw(buf + sizeof (struct fw_header), firmware_len + 2);
572 /* Write firmware file */
573 ret = write_fw(buf, writelen);
585 /* Helper functions to inspect_fw() representing different output formats */
586 static inline void inspect_fw_pstr(char *label, char *str)
588 printf("%-23s: %s\n", label, str);
591 static inline void inspect_fw_phex(char *label, uint32_t val)
593 printf("%-23s: 0x%08x\n", label, val);
596 static inline void inspect_fw_phexpost(char *label,
597 uint32_t val, char *post)
599 printf("%-23s: 0x%08x (%s)\n", label, val, post);
602 static inline void inspect_fw_phexdef(char *label,
603 uint32_t val, uint32_t defval)
605 printf("%-23s: 0x%08x ", label, val);
608 printf("(== OpenWrt default)\n");
610 printf("(OpenWrt default: 0x%08x)\n", defval);
614 static inline void inspect_fw_phexexp(char *label,
615 uint32_t val, uint32_t expval)
617 printf("%-23s: 0x%08x ", label, val);
622 printf("(expected: 0x%08x)\n", expval);
626 static inline void inspect_fw_phexdec(char *label, uint32_t val)
628 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
631 static inline void inspect_fw_pchecksum(char *label,
632 uint16_t val, uint16_t expval)
634 printf("%-23s: 0x%04x ", label, val);
638 printf("(expected: 0x%04x)\n", expval);
642 static int inspect_fw(void)
645 struct fw_header *hdr;
646 int ret = EXIT_FAILURE;
647 uint16_t computed_checksum, file_checksum;
649 buf = (uint8_t *) malloc(firmware_info.file_size);
651 ERR("no memory for buffer!\n");
655 ret = read_to_buf(&firmware_info, buf);
659 hdr = (struct fw_header *)buf;
661 inspect_fw_pstr("File name", firmware_info.file_name);
662 inspect_fw_phexdec("File size", firmware_info.file_size);
666 inspect_fw_phexdec("Header size", sizeof (struct fw_header));
667 board = find_board_by_hwid(LE32_TO_HOST(hdr->hw_id));
669 layout = find_layout(board->layout_id);
670 inspect_fw_phexpost("Hardware ID",
671 LE32_TO_HOST( hdr->hw_id), board->id);
673 inspect_fw_phexpost("Hardware ID",
674 LE32_TO_HOST(hdr->hw_id), "unknown");
676 inspect_fw_phexdec("Firmware data length",
677 LE32_TO_HOST(hdr->firmware_len));
679 inspect_fw_phexexp("Flags",
680 LE32_TO_HOST(hdr->flags), HEADER_FLAGS);
683 /* XOR unobfuscate firmware */
684 xor_fw(buf + sizeof (struct fw_header), LE32_TO_HOST(hdr->firmware_len) + 2);
686 /* Compute firmware checksum */
687 computed_checksum = checksum_fw(buf + sizeof (struct fw_header), LE32_TO_HOST(hdr->firmware_len));
689 /* Cannot use network order function because checksum is not word-aligned */
690 file_checksum = (buf[firmware_info.file_size - 1] << 8) | buf[firmware_info.file_size - 2];
691 inspect_fw_pchecksum("Firmware checksum", computed_checksum, file_checksum);
693 /* Verify checksum */
694 if (computed_checksum != file_checksum) {
696 ERR("checksums do not match");
706 if (ofname == NULL) {
707 filename = malloc(strlen(firmware_info.file_name) + 10);
708 sprintf(filename, "%s-firmware", firmware_info.file_name);
712 printf("Extracting firmware to \"%s\"...\n", filename);
713 fp = fopen(filename, "wb");
715 if (!fwrite(buf + sizeof (struct fw_header),
716 LE32_TO_HOST(hdr->firmware_len), 1, fp)) {
717 ERRS("error in fwrite(): %s", strerror(errno));
721 ERRS("error in fopen(): %s", strerror(errno));
723 if (ofname == NULL) {
738 int main(int argc, char *argv[])
740 int ret = EXIT_FAILURE;
742 progname = basename(argv[0]);
746 while ((c = getopt(argc, argv, "B:H:F:f:o:ixh")) != -1) {
758 firmware_info.file_name = optarg;
779 ret = check_options();