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