ramips: add support for A5-V11 board (resubmit)
[openwrt/staging/mkresin.git] / tools / firmware-utils / src / mkporayfw.c
1 /*
2 * Builder/viewer/extractor utility for Poray firmware image files
3 *
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>
7 *
8 * This tool is based on:
9 * TP-Link firmware upgrade tool.
10 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
11 *
12 * Itself based on:
13 * TP-Link WR941 V2 firmware checksum fixing tool.
14 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
15 *
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.
19 *
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <libgen.h>
28 #include <getopt.h>
29 #include <stdarg.h>
30 #include <errno.h>
31 #include <sys/stat.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
34
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)
40 #else
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)
45 #endif
46
47 /* Fixed header flags */
48 #define HEADER_FLAGS 0x020e0000
49
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
63 #define HWID_A5_V11 0x32473352
64
65 /* Recognized XOR obfuscation keys */
66 #define KEY_HAME 0
67 #define KEY_PORAY_1 1
68 #define KEY_PORAY_2 2
69 #define KEY_PORAY_3 3
70 #define KEY_PORAY_4 4
71 #define KEY_NEXX_1 5
72 #define KEY_NEXX_2 6
73 #define KEY_A5_V11 7
74
75 /* XOR key length */
76 #define KEY_LEN 15
77
78 struct file_info {
79 char *file_name; /* Name of the file */
80 uint32_t file_size; /* Length of the file */
81 };
82
83 struct fw_header {
84 uint32_t hw_id; /* Hardware id */
85 uint32_t firmware_len; /* Firmware data length */
86 uint32_t flags; /* Header flags */
87 uint8_t pad[16];
88 } __attribute__ ((packed));
89
90 struct flash_layout {
91 char *id;
92 uint32_t fw_max_len;
93 };
94
95 struct board_info {
96 char *id;
97 uint32_t hw_id;
98 char *layout_id;
99 uint32_t key;
100 };
101
102 /*
103 * Globals
104 */
105 static char *ofname;
106 static char *progname;
107
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;
116
117 static int inspect = 0;
118 static int extract = 0;
119
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},
129 };
130
131 static struct flash_layout layouts[] = {
132 {
133 .id = "4M",
134 .fw_max_len = 0x3c0000,
135 }, {
136 .id = "8M",
137 .fw_max_len = 0x7c0000,
138 }, {
139 /* terminating entry */
140 }
141 };
142
143 static struct board_info boards[] = {
144 {
145 .id = "A5-V11",
146 .hw_id = HWID_A5_V11,
147 .layout_id = "4M",
148 .key = KEY_A5_V11,
149 }, {
150 .id = "MPR-A1",
151 .hw_id = HWID_HAME_MPR_A1_L8,
152 .layout_id = "4M",
153 .key = KEY_HAME,
154 }, {
155 .id = "MPR-L8",
156 .hw_id = HWID_HAME_MPR_A1_L8,
157 .layout_id = "4M",
158 .key = KEY_HAME,
159 }, {
160 .id = "R50B",
161 .hw_id = HWID_PORAY_R50B,
162 .layout_id = "4M",
163 .key = KEY_PORAY_2,
164 }, {
165 .id = "R50D",
166 .hw_id = HWID_PORAY_R50D,
167 .layout_id = "4M",
168 .key = KEY_PORAY_3,
169 }, {
170 .id = "R50E",
171 .hw_id = HWID_PORAY_R50E,
172 .layout_id = "4M",
173 .key = KEY_PORAY_4,
174 }, {
175 .id = "M3",
176 .hw_id = HWID_PORAY_M3,
177 .layout_id = "4M",
178 .key = KEY_PORAY_1,
179 }, {
180 .id = "M4",
181 .hw_id = HWID_PORAY_M4,
182 .layout_id = "4M",
183 .key = KEY_PORAY_1,
184 }, {
185 .id = "Q3",
186 .hw_id = HWID_PORAY_Q3,
187 .layout_id = "4M",
188 .key = KEY_PORAY_1,
189 }, {
190 .id = "X5 or X6",
191 .hw_id = HWID_PORAY_X5_X6,
192 .layout_id = "8M",
193 .key = KEY_PORAY_1,
194 }, {
195 .id = "X5",
196 .hw_id = HWID_PORAY_X5_X6,
197 .layout_id = "8M",
198 .key = KEY_PORAY_1,
199 }, {
200 .id = "X6",
201 .hw_id = HWID_PORAY_X5_X6,
202 .layout_id = "8M",
203 .key = KEY_PORAY_1,
204 }, {
205 .id = "X8",
206 .hw_id = HWID_PORAY_X8,
207 .layout_id = "8M",
208 .key = KEY_PORAY_1,
209 }, {
210 .id = "X1",
211 .hw_id = HWID_PORAY_X1,
212 .layout_id = "8M",
213 .key = KEY_PORAY_1,
214 }, {
215 .id = "WT15XXX",
216 .hw_id = HWID_NEXX_WT15XXX,
217 .layout_id = "4M",
218 .key = KEY_NEXX_1,
219 }, {
220 .id = "WT3020A",
221 .hw_id = HWID_NEXX_WT3020A,
222 .layout_id = "4M",
223 .key = KEY_NEXX_2,
224 }, {
225 /* terminating entry */
226 }
227 };
228
229 /*
230 * Message macros
231 */
232 #define ERR(fmt, ...) do { \
233 fflush(0); \
234 fprintf(stderr, "[%s] *** error: " fmt "\n", \
235 progname, ## __VA_ARGS__ ); \
236 } while (0)
237
238 #define ERRS(fmt, ...) do { \
239 int save = errno; \
240 fflush(0); \
241 fprintf(stderr, "[%s] *** error: " fmt ":%s\n", \
242 progname, ## __VA_ARGS__, strerror(save)); \
243 } while (0)
244
245 #define DBG(fmt, ...) do { \
246 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
247 } while (0)
248
249 /*
250 * Find a board by its name
251 */
252 static struct board_info *find_board(char *id)
253 {
254 struct board_info *ret;
255 struct board_info *board;
256
257 ret = NULL;
258 for (board = boards; board->id != NULL; board++){
259 if (strcasecmp(id, board->id) == 0) {
260 ret = board;
261 break;
262 }
263 };
264
265 return ret;
266 }
267
268 /*
269 * Find a board by its hardware ID
270 */
271 static struct board_info *find_board_by_hwid(uint32_t hw_id)
272 {
273 struct board_info *board;
274
275 for (board = boards; board->id != NULL; board++) {
276 if (hw_id == board->hw_id)
277 return board;
278 };
279
280 return NULL;
281 }
282
283 /*
284 * Find a Flash memory layout by its name
285 */
286 static struct flash_layout *find_layout(char *id)
287 {
288 struct flash_layout *ret;
289 struct flash_layout *l;
290
291 ret = NULL;
292 for (l = layouts; l->id != NULL; l++){
293 if (strcasecmp(id, l->id) == 0) {
294 ret = l;
295 break;
296 }
297 };
298
299 return ret;
300 }
301
302 /*
303 * Display usage
304 */
305 static void usage(int status)
306 {
307 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
308
309 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
310 fprintf(stream,
311 "\n"
312 "Options:\n"
313 " -B <board> create image for the board specified with <board>\n"
314 " -H <hwid> use hardware id specified with <hwid>\n"
315 " -F <id> use flash layout specified with <id>\n"
316 " -f <file> read firmware image from the file <file>\n"
317 " -o <file> write output to the file <file>\n"
318 " -i inspect given firmware file (requires -f)\n"
319 " -x extract combined kernel and rootfs while inspecting (implies -i)\n"
320 " -h show this screen\n"
321 );
322
323 exit(status);
324 }
325
326 /*
327 * Get file statistics
328 */
329 static int get_file_stat(struct file_info *fdata)
330 {
331 struct stat st;
332 int res;
333
334 if (fdata->file_name == NULL) {
335 return 0;
336 }
337 res = stat(fdata->file_name, &st);
338 if (res){
339 ERRS("stat failed on %s", fdata->file_name);
340 return res;
341 }
342
343 fdata->file_size = st.st_size;
344 return 0;
345 }
346
347 /*
348 * Read file into buffer
349 */
350 static int read_to_buf(struct file_info *fdata, uint8_t *buf)
351 {
352 FILE *f;
353 int ret = EXIT_FAILURE;
354
355 f = fopen(fdata->file_name, "rb");
356 if (f == NULL) {
357 ERRS("could not open \"%s\" for reading", fdata->file_name);
358 goto out;
359 }
360
361 errno = 0;
362 fread(buf, fdata->file_size, 1, f);
363 if (errno != 0) {
364 ERRS("unable to read from file \"%s\"", fdata->file_name);
365 goto out_close;
366 }
367
368 ret = EXIT_SUCCESS;
369
370 out_close:
371 fclose(f);
372 out:
373 return ret;
374 }
375
376 /*
377 * Check command line options
378 */
379 static int check_options(void)
380 {
381 int ret;
382
383 if (firmware_info.file_name == NULL) {
384 ERR("no firmware image specified");
385 return -1;
386 }
387
388 ret = get_file_stat(&firmware_info);
389 if (ret)
390 return ret;
391
392 if (inspect)
393 return 0;
394
395 if (board_id == NULL && opt_hw_id == NULL) {
396 ERR("either board or hardware id must be specified");
397 return -1;
398 }
399
400 if (board_id) {
401 board = find_board(board_id);
402 if (board == NULL) {
403 ERR("unknown/unsupported board id \"%s\"", board_id);
404 return -1;
405 }
406 if (layout_id == NULL) {
407 layout_id = board->layout_id;
408 }
409 hw_id = board->hw_id;
410 } else {
411 hw_id = strtoul(opt_hw_id, NULL, 0);
412 board = find_board_by_hwid(hw_id);
413 if (layout_id == NULL) {
414 layout_id = board->layout_id;
415 }
416 }
417
418 layout = find_layout(layout_id);
419 if (layout == NULL) {
420 ERR("unknown flash layout \"%s\"", layout_id);
421 return -1;
422 }
423
424 firmware_len = firmware_info.file_size;
425
426 if (firmware_info.file_size >
427 layout->fw_max_len - sizeof (struct fw_header)) {
428 ERR("firmware image is too big");
429 return -1;
430 }
431
432 if (ofname == NULL) {
433 ERR("no output file specified");
434 return -1;
435 }
436 return 0;
437 }
438
439 /*
440 * Fill in firmware header
441 */
442 static void fill_header(uint8_t *buf)
443 {
444 struct fw_header *hdr = (struct fw_header *) buf;
445
446 memset(hdr, 0, sizeof (struct fw_header));
447 hdr->hw_id = HOST_TO_LE32(hw_id);
448 hdr->firmware_len = HOST_TO_LE32(firmware_len);
449 hdr->flags = HOST_TO_LE32(HEADER_FLAGS);
450 }
451
452 /*
453 * Compute firmware checksum
454 */
455 static uint16_t checksum_fw(uint8_t *data, int len)
456 {
457 int i;
458 int32_t checksum = 0;
459
460 for (i = 0; i < len - 1; i += 2) {
461 checksum += (data[i + 1] << 8) | data[i];
462 }
463 if (i < len) {
464 checksum += data[i];
465 }
466 checksum = checksum + (checksum >> 16) + 0xffff;
467 checksum = ~(checksum + (checksum >> 16)) & 0xffff;
468 return (uint16_t) checksum;
469 }
470
471 /*
472 * (De)obfuscate firmware using an XOR operation with a fixed length key
473 */
474 static void xor_fw(uint8_t *data, int len)
475 {
476 int i;
477
478 for (i = 0; i <= len; i++) {
479 data[i] ^= key[board->key][i % KEY_LEN];
480 }
481 }
482
483 /*
484 * Write firmware to file
485 */
486 static int write_fw(uint8_t *data, int len)
487 {
488 FILE *f;
489 int ret = EXIT_FAILURE;
490
491 f = fopen(ofname, "wb");
492 if (f == NULL) {
493 ERRS("could not open \"%s\" for writing", ofname);
494 goto out;
495 }
496
497 errno = 0;
498 fwrite(data, len, 1, f);
499 if (errno) {
500 ERRS("unable to write output file");
501 goto out_flush;
502 }
503
504 DBG("firmware file \"%s\" completed", ofname);
505
506 ret = EXIT_SUCCESS;
507
508 out_flush:
509 fflush(f);
510 fclose(f);
511 if (ret != EXIT_SUCCESS) {
512 unlink(ofname);
513 }
514 out:
515 return ret;
516 }
517
518 /*
519 * Build firmware file
520 */
521 static int build_fw(void)
522 {
523 int buflen;
524 uint8_t *buf, *p;
525 int ret = EXIT_FAILURE;
526 int writelen = 0;
527 uint16_t checksum;
528
529 buflen = layout->fw_max_len;
530
531 buf = (uint8_t *) malloc(buflen);
532 if (!buf) {
533 ERR("no memory for buffer\n");
534 goto out;
535 }
536
537 memset(buf, 0xff, buflen);
538 p = buf + sizeof (struct fw_header);
539 ret = read_to_buf(&firmware_info, p);
540 if (ret) {
541 goto out_free_buf;
542 }
543 writelen = sizeof (struct fw_header) + firmware_len + 2;
544
545 /* Fill in header */
546 fill_header(buf);
547
548 /* Compute firmware checksum */
549 checksum = checksum_fw(buf + sizeof (struct fw_header), firmware_len);
550
551 /* Cannot use network order function because checksum is not word-aligned */
552 buf[writelen - 1] = checksum >> 8;
553 buf[writelen - 2] = checksum & 0xff;
554
555 /* XOR obfuscate firmware */
556 xor_fw(buf + sizeof (struct fw_header), firmware_len + 2);
557
558 /* Write firmware file */
559 ret = write_fw(buf, writelen);
560 if (ret) {
561 goto out_free_buf;
562 }
563 ret = EXIT_SUCCESS;
564
565 out_free_buf:
566 free(buf);
567 out:
568 return ret;
569 }
570
571 /* Helper functions to inspect_fw() representing different output formats */
572 static inline void inspect_fw_pstr(char *label, char *str)
573 {
574 printf("%-23s: %s\n", label, str);
575 }
576
577 static inline void inspect_fw_phex(char *label, uint32_t val)
578 {
579 printf("%-23s: 0x%08x\n", label, val);
580 }
581
582 static inline void inspect_fw_phexpost(char *label,
583 uint32_t val, char *post)
584 {
585 printf("%-23s: 0x%08x (%s)\n", label, val, post);
586 }
587
588 static inline void inspect_fw_phexdef(char *label,
589 uint32_t val, uint32_t defval)
590 {
591 printf("%-23s: 0x%08x ", label, val);
592
593 if (val == defval) {
594 printf("(== OpenWrt default)\n");
595 } else {
596 printf("(OpenWrt default: 0x%08x)\n", defval);
597 }
598 }
599
600 static inline void inspect_fw_phexexp(char *label,
601 uint32_t val, uint32_t expval)
602 {
603 printf("%-23s: 0x%08x ", label, val);
604
605 if (val == expval) {
606 printf("(ok)\n");
607 } else {
608 printf("(expected: 0x%08x)\n", expval);
609 }
610 }
611
612 static inline void inspect_fw_phexdec(char *label, uint32_t val)
613 {
614 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
615 }
616
617 static inline void inspect_fw_pchecksum(char *label,
618 uint16_t val, uint16_t expval)
619 {
620 printf("%-23s: 0x%04x ", label, val);
621 if (val == expval) {
622 printf("(ok)\n");
623 } else {
624 printf("(expected: 0x%04x)\n", expval);
625 }
626 }
627
628 static int inspect_fw(void)
629 {
630 uint8_t *buf;
631 struct fw_header *hdr;
632 int ret = EXIT_FAILURE;
633 uint16_t computed_checksum, file_checksum;
634
635 buf = (uint8_t *) malloc(firmware_info.file_size);
636 if (!buf) {
637 ERR("no memory for buffer!\n");
638 goto out;
639 }
640
641 ret = read_to_buf(&firmware_info, buf);
642 if (ret) {
643 goto out_free_buf;
644 }
645 hdr = (struct fw_header *)buf;
646
647 inspect_fw_pstr("File name", firmware_info.file_name);
648 inspect_fw_phexdec("File size", firmware_info.file_size);
649
650 printf("\n");
651
652 inspect_fw_phexdec("Header size", sizeof (struct fw_header));
653 board = find_board_by_hwid(LE32_TO_HOST(hdr->hw_id));
654 if (board) {
655 layout = find_layout(board->layout_id);
656 inspect_fw_phexpost("Hardware ID",
657 LE32_TO_HOST( hdr->hw_id), board->id);
658 } else {
659 inspect_fw_phexpost("Hardware ID",
660 LE32_TO_HOST(hdr->hw_id), "unknown");
661 }
662 inspect_fw_phexdec("Firmware data length",
663 LE32_TO_HOST(hdr->firmware_len));
664
665 inspect_fw_phexexp("Flags",
666 LE32_TO_HOST(hdr->flags), HEADER_FLAGS);
667 printf("\n");
668
669 /* XOR unobfuscate firmware */
670 xor_fw(buf + sizeof (struct fw_header), LE32_TO_HOST(hdr->firmware_len) + 2);
671
672 /* Compute firmware checksum */
673 computed_checksum = checksum_fw(buf + sizeof (struct fw_header), LE32_TO_HOST(hdr->firmware_len));
674
675 /* Cannot use network order function because checksum is not word-aligned */
676 file_checksum = (buf[firmware_info.file_size - 1] << 8) | buf[firmware_info.file_size - 2];
677 inspect_fw_pchecksum("Firmware checksum", computed_checksum, file_checksum);
678
679 /* Verify checksum */
680 if (computed_checksum != file_checksum) {
681 ret = -1;
682 ERR("checksums do not match");
683 goto out_free_buf;
684 }
685
686 printf("\n");
687
688 if (extract) {
689 FILE *fp;
690 char *filename;
691
692 if (ofname == NULL) {
693 filename = malloc(strlen(firmware_info.file_name) + 10);
694 sprintf(filename, "%s-firmware", firmware_info.file_name);
695 } else {
696 filename = ofname;
697 }
698 printf("Extracting firmware to \"%s\"...\n", filename);
699 fp = fopen(filename, "wb");
700 if (fp) {
701 if (!fwrite(buf + sizeof (struct fw_header),
702 LE32_TO_HOST(hdr->firmware_len), 1, fp)) {
703 ERRS("error in fwrite(): %s", strerror(errno));
704 }
705 fclose(fp);
706 } else {
707 ERRS("error in fopen(): %s", strerror(errno));
708 }
709 if (ofname == NULL) {
710 free(filename);
711 }
712 printf("\n");
713 }
714
715 out_free_buf:
716 free(buf);
717 out:
718 return ret;
719 }
720
721 /*
722 * Main entry point
723 */
724 int main(int argc, char *argv[])
725 {
726 int ret = EXIT_FAILURE;
727
728 progname = basename(argv[0]);
729
730 int c;
731
732 while ((c = getopt(argc, argv, "B:H:F:f:o:ixh")) != -1) {
733 switch (c) {
734 case 'B':
735 board_id = optarg;
736 break;
737 case 'H':
738 opt_hw_id = optarg;
739 break;
740 case 'F':
741 layout_id = optarg;
742 break;
743 case 'f':
744 firmware_info.file_name = optarg;
745 break;
746 case 'o':
747 ofname = optarg;
748 break;
749 case 'i':
750 inspect = 1;
751 break;
752 case 'x':
753 inspect = 1;
754 extract = 1;
755 break;
756 case 'h':
757 usage(EXIT_SUCCESS);
758 break;
759 default:
760 usage(EXIT_FAILURE);
761 break;
762 }
763 }
764
765 ret = check_options();
766 if (ret) {
767 goto out;
768 }
769 if (!inspect) {
770 ret = build_fw();
771 } else {
772 ret = inspect_fw();
773 }
774
775 out:
776 return ret;
777 }