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_WT15XXX 0x30353332
62 #define HWID_NEXX_WT3020A 0x30323033
64 /* Recognized XOR obfuscation keys */
77 char *file_name; /* Name of the file */
78 uint32_t file_size; /* Length of the file */
82 uint32_t hw_id; /* Hardware id */
83 uint32_t firmware_len; /* Firmware data length */
84 uint32_t flags; /* Header flags */
86 } __attribute__ ((packed));
104 static char *progname;
106 static char *board_id;
107 static struct board_info *board;
108 static char *layout_id;
109 static struct flash_layout *layout;
110 static char *opt_hw_id;
111 static uint32_t hw_id;
112 static struct file_info firmware_info;
113 static uint32_t firmware_len = 0;
115 static int inspect = 0;
116 static int extract = 0;
118 static uint8_t key[][KEY_LEN] = {
119 {0xC8, 0x3C, 0x3A, 0x93, 0xA2, 0x95, 0xC3, 0x63, 0x48, 0x45, 0x58, 0x09, 0x12, 0x03, 0x08},
120 {0x89, 0x6B, 0x5A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
121 {0xC9, 0x1C, 0x3A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
122 {0x19, 0x1B, 0x3A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
123 {0x79, 0x7B, 0x7A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
124 {0x19, 0x1C, 0x4A, 0x93, 0x96, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0x16, 0xC6},
125 {0x39, 0x1C, 0x4A, 0x93, 0x96, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0x16, 0xC6},
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_HAME_MPR_A1_L8,
148 .hw_id = HWID_HAME_MPR_A1_L8,
153 .hw_id = HWID_PORAY_R50B,
158 .hw_id = HWID_PORAY_R50D,
163 .hw_id = HWID_PORAY_R50E,
168 .hw_id = HWID_PORAY_M3,
173 .hw_id = HWID_PORAY_M4,
178 .hw_id = HWID_PORAY_Q3,
183 .hw_id = HWID_PORAY_X5_X6,
188 .hw_id = HWID_PORAY_X5_X6,
193 .hw_id = HWID_PORAY_X5_X6,
198 .hw_id = HWID_PORAY_X8,
203 .hw_id = HWID_PORAY_X1,
208 .hw_id = HWID_NEXX_WT15XXX,
213 .hw_id = HWID_NEXX_WT3020A,
217 /* terminating entry */
224 #define ERR(fmt, ...) do { \
226 fprintf(stderr, "[%s] *** error: " fmt "\n", \
227 progname, ## __VA_ARGS__ ); \
230 #define ERRS(fmt, ...) do { \
233 fprintf(stderr, "[%s] *** error: " fmt ":%s\n", \
234 progname, ## __VA_ARGS__, strerror(save)); \
237 #define DBG(fmt, ...) do { \
238 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
242 * Find a board by its name
244 static struct board_info *find_board(char *id)
246 struct board_info *ret;
247 struct board_info *board;
250 for (board = boards; board->id != NULL; board++){
251 if (strcasecmp(id, board->id) == 0) {
261 * Find a board by its hardware ID
263 static struct board_info *find_board_by_hwid(uint32_t hw_id)
265 struct board_info *board;
267 for (board = boards; board->id != NULL; board++) {
268 if (hw_id == board->hw_id)
276 * Find a Flash memory layout by its name
278 static struct flash_layout *find_layout(char *id)
280 struct flash_layout *ret;
281 struct flash_layout *l;
284 for (l = layouts; l->id != NULL; l++){
285 if (strcasecmp(id, l->id) == 0) {
297 static void usage(int status)
299 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
301 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
305 " -B <board> create image for the board specified with <board>\n"
306 " -H <hwid> use hardware id specified with <hwid>\n"
307 " -F <id> use flash layout specified with <id>\n"
308 " -f <file> read firmware image from the file <file>\n"
309 " -o <file> write output to the file <file>\n"
310 " -i inspect given firmware file (requires -f)\n"
311 " -x extract combined kernel and rootfs while inspecting (implies -i)\n"
312 " -h show this screen\n"
319 * Get file statistics
321 static int get_file_stat(struct file_info *fdata)
326 if (fdata->file_name == NULL) {
329 res = stat(fdata->file_name, &st);
331 ERRS("stat failed on %s", fdata->file_name);
335 fdata->file_size = st.st_size;
340 * Read file into buffer
342 static int read_to_buf(struct file_info *fdata, uint8_t *buf)
345 int ret = EXIT_FAILURE;
347 f = fopen(fdata->file_name, "rb");
349 ERRS("could not open \"%s\" for reading", fdata->file_name);
354 fread(buf, fdata->file_size, 1, f);
356 ERRS("unable to read from file \"%s\"", fdata->file_name);
369 * Check command line options
371 static int check_options(void)
375 if (firmware_info.file_name == NULL) {
376 ERR("no firmware image specified");
380 ret = get_file_stat(&firmware_info);
387 if (board_id == NULL && opt_hw_id == NULL) {
388 ERR("either board or hardware id must be specified");
393 board = find_board(board_id);
395 ERR("unknown/unsupported board id \"%s\"", board_id);
398 if (layout_id == NULL) {
399 layout_id = board->layout_id;
401 hw_id = board->hw_id;
403 hw_id = strtoul(opt_hw_id, NULL, 0);
404 board = find_board_by_hwid(hw_id);
405 if (layout_id == NULL) {
406 layout_id = board->layout_id;
410 layout = find_layout(layout_id);
411 if (layout == NULL) {
412 ERR("unknown flash layout \"%s\"", layout_id);
416 firmware_len = firmware_info.file_size;
418 if (firmware_info.file_size >
419 layout->fw_max_len - sizeof (struct fw_header)) {
420 ERR("firmware image is too big");
424 if (ofname == NULL) {
425 ERR("no output file specified");
432 * Fill in firmware header
434 static void fill_header(uint8_t *buf)
436 struct fw_header *hdr = (struct fw_header *) buf;
438 memset(hdr, 0, sizeof (struct fw_header));
439 hdr->hw_id = HOST_TO_LE32(hw_id);
440 hdr->firmware_len = HOST_TO_LE32(firmware_len);
441 hdr->flags = HOST_TO_LE32(HEADER_FLAGS);
445 * Compute firmware checksum
447 static uint16_t checksum_fw(uint8_t *data, int len)
450 int32_t checksum = 0;
452 for (i = 0; i < len - 1; i += 2) {
453 checksum += (data[i + 1] << 8) | data[i];
458 checksum = checksum + (checksum >> 16) + 0xffff;
459 checksum = ~(checksum + (checksum >> 16)) & 0xffff;
460 return (uint16_t) checksum;
464 * (De)obfuscate firmware using an XOR operation with a fixed length key
466 static void xor_fw(uint8_t *data, int len)
470 for (i = 0; i <= len; i++) {
471 data[i] ^= key[board->key][i % KEY_LEN];
476 * Write firmware to file
478 static int write_fw(uint8_t *data, int len)
481 int ret = EXIT_FAILURE;
483 f = fopen(ofname, "wb");
485 ERRS("could not open \"%s\" for writing", ofname);
490 fwrite(data, len, 1, f);
492 ERRS("unable to write output file");
496 DBG("firmware file \"%s\" completed", ofname);
503 if (ret != EXIT_SUCCESS) {
511 * Build firmware file
513 static int build_fw(void)
517 int ret = EXIT_FAILURE;
521 buflen = layout->fw_max_len;
523 buf = (uint8_t *) malloc(buflen);
525 ERR("no memory for buffer\n");
529 memset(buf, 0xff, buflen);
530 p = buf + sizeof (struct fw_header);
531 ret = read_to_buf(&firmware_info, p);
535 writelen = sizeof (struct fw_header) + firmware_len + 2;
540 /* Compute firmware checksum */
541 checksum = checksum_fw(buf + sizeof (struct fw_header), firmware_len);
543 /* Cannot use network order function because checksum is not word-aligned */
544 buf[writelen - 1] = checksum >> 8;
545 buf[writelen - 2] = checksum & 0xff;
547 /* XOR obfuscate firmware */
548 xor_fw(buf + sizeof (struct fw_header), firmware_len + 2);
550 /* Write firmware file */
551 ret = write_fw(buf, writelen);
563 /* Helper functions to inspect_fw() representing different output formats */
564 static inline void inspect_fw_pstr(char *label, char *str)
566 printf("%-23s: %s\n", label, str);
569 static inline void inspect_fw_phex(char *label, uint32_t val)
571 printf("%-23s: 0x%08x\n", label, val);
574 static inline void inspect_fw_phexpost(char *label,
575 uint32_t val, char *post)
577 printf("%-23s: 0x%08x (%s)\n", label, val, post);
580 static inline void inspect_fw_phexdef(char *label,
581 uint32_t val, uint32_t defval)
583 printf("%-23s: 0x%08x ", label, val);
586 printf("(== OpenWrt default)\n");
588 printf("(OpenWrt default: 0x%08x)\n", defval);
592 static inline void inspect_fw_phexexp(char *label,
593 uint32_t val, uint32_t expval)
595 printf("%-23s: 0x%08x ", label, val);
600 printf("(expected: 0x%08x)\n", expval);
604 static inline void inspect_fw_phexdec(char *label, uint32_t val)
606 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
609 static inline void inspect_fw_pchecksum(char *label,
610 uint16_t val, uint16_t expval)
612 printf("%-23s: 0x%04x ", label, val);
616 printf("(expected: 0x%04x)\n", expval);
620 static int inspect_fw(void)
623 struct fw_header *hdr;
624 int ret = EXIT_FAILURE;
625 uint16_t computed_checksum, file_checksum;
627 buf = (uint8_t *) malloc(firmware_info.file_size);
629 ERR("no memory for buffer!\n");
633 ret = read_to_buf(&firmware_info, buf);
637 hdr = (struct fw_header *)buf;
639 inspect_fw_pstr("File name", firmware_info.file_name);
640 inspect_fw_phexdec("File size", firmware_info.file_size);
644 inspect_fw_phexdec("Header size", sizeof (struct fw_header));
645 board = find_board_by_hwid(LE32_TO_HOST(hdr->hw_id));
647 layout = find_layout(board->layout_id);
648 inspect_fw_phexpost("Hardware ID",
649 LE32_TO_HOST( hdr->hw_id), board->id);
651 inspect_fw_phexpost("Hardware ID",
652 LE32_TO_HOST(hdr->hw_id), "unknown");
654 inspect_fw_phexdec("Firmware data length",
655 LE32_TO_HOST(hdr->firmware_len));
657 inspect_fw_phexexp("Flags",
658 LE32_TO_HOST(hdr->flags), HEADER_FLAGS);
661 /* XOR unobfuscate firmware */
662 xor_fw(buf + sizeof (struct fw_header), LE32_TO_HOST(hdr->firmware_len) + 2);
664 /* Compute firmware checksum */
665 computed_checksum = checksum_fw(buf + sizeof (struct fw_header), LE32_TO_HOST(hdr->firmware_len));
667 /* Cannot use network order function because checksum is not word-aligned */
668 file_checksum = (buf[firmware_info.file_size - 1] << 8) | buf[firmware_info.file_size - 2];
669 inspect_fw_pchecksum("Firmware checksum", computed_checksum, file_checksum);
671 /* Verify checksum */
672 if (computed_checksum != file_checksum) {
674 ERR("checksums do not match");
684 if (ofname == NULL) {
685 filename = malloc(strlen(firmware_info.file_name) + 10);
686 sprintf(filename, "%s-firmware", firmware_info.file_name);
690 printf("Extracting firmware to \"%s\"...\n", filename);
691 fp = fopen(filename, "wb");
693 if (!fwrite(buf + sizeof (struct fw_header),
694 LE32_TO_HOST(hdr->firmware_len), 1, fp)) {
695 ERRS("error in fwrite(): %s", strerror(errno));
699 ERRS("error in fopen(): %s", strerror(errno));
701 if (ofname == NULL) {
716 int main(int argc, char *argv[])
718 int ret = EXIT_FAILURE;
720 progname = basename(argv[0]);
724 while ((c = getopt(argc, argv, "B:H:F:f:o:ixh")) != -1) {
736 firmware_info.file_name = optarg;
757 ret = check_options();