-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathax88179a_programmer.c
1532 lines (1252 loc) · 33.8 KB
/
ax88179a_programmer.c
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*******************************************************************************
* Copyright (c) 2022 ASIX Electronic Corporation All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
******************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <getopt.h>
#include <endian.h>
#if NET_INTERFACE == INTERFACE_SCAN
#include <ifaddrs.h>
#endif
#include "ax_ioctl.h"
#ifdef ENABLE_IOCTL_DEBUG
#define NOT_PROGRAM
#endif
#define RELOAD_DELAY_TIME 10 // sec
#define PRINT_IOCTL_FAIL(ret) \
fprintf(stderr, "%s: ioctl failed. (err: %d)\n", __func__, ret)
#define PRINT_SCAN_DEV_FAIL \
fprintf(stderr, "%s: Scaning device failed.\n", __func__)
#define PRINT_ALLCATE_MEM_FAIL \
fprintf(stderr, "%s: Fail to allocate memory.\n", __func__)
#define PRINT_LOAD_FILE_FAIL \
fprintf(stderr, "%s: Read file failed.\n", __func__)
#define AX88179A_IOCTL_VERSION \
"AX88179B/AX88179A/AX88772E/AX88772D Linux Flash/eFuse Programming Tool v2.0.0"
const char help_str1[] =
"./ax88179b_179a_772e_772d_programmer help [command]\n"
" -- command description\n";
const char help_str2[] =
" [command] - Display usage of specified command\n";
const char readverion_str1[] =
"./ax88179b_179a_772e_772d_programmer rversion\n"
" -- AX88179B_179A_772E_772D Read Firmware Verion\n";
static const char readverion_str2[] = "";
const char readmac_str1[] =
"./ax88179b_179a_772e_772d_programmer rmacaddr\n"
" -- AX88179B_179A_772E_772D Read MAC Address\n";
static const char readmac_str2[] = "";
const char writeflash_str1[] =
"./ax88179b_179a_772e_772d_programmer wflash [file]\n"
" -- AX88179B_179A_772E_772D Write Flash\n";
const char writeflash_str2[] =
" [file] - Flash file path\n";
const char writeefuse_str1[] =
"./ax88179b_179a_772e_772d_programmer wefuse -m [MAC] -s [SN] -f [File] --led0 [value]"
" --led1 [value] -p [device]\n"
" -- AX88179B_179A_772E_772D Write eFuse\n";
const char writeefuse_str2[] =
" -m [MAC] - MAC address (XX:XX:XX:XX:XX:XX)\n"
" -s [SN] - Serial number\n"
" -f [File] - eFuse file path\n"
" --led0 [value] - value: control_blink (XXXX_XXXX)\n"
" --led1 [value] - value: control_blink (XXXX_XXXX)\n"
" -p [device] - device: \"AX88179B\" or \"AX88179A\" or \"AX88772E\" or \"AX88772D\"\n";
const char readefuse_str1[] =
"./ax88179b_179a_772e_772d_programmer refuse -f [File]\n"
" -- AX88179B_179A_772E_772D Read eFuse\n";
const char readefuse_str2[] =
" -f [File] - eFuse file path\n";
const char reload_str1[] =
"./ax88179b_179a_772e_772d_programmer reload\n"
" -- AX88179B_179A_772E_772D Reload\n";
static const char reload_str2[] = "";
static int help_func(struct ax_command_info *info);
static int readversion_func(struct ax_command_info *info);
static int readmac_func(struct ax_command_info *info);
static int writeflash_func(struct ax_command_info *info);
static int writeefuse_func(struct ax_command_info *info);
static int readefuse_func(struct ax_command_info *info);
static int reload_func(struct ax_command_info *info);
static int scan_ax_device(struct ifreq *ifr, int inet_sock);
struct _command_list ax88179a_cmd_list[] = {
{
"help",
AX_SIGNATURE,
help_func,
help_str1,
help_str2
},
{
"rversion",
AX88179A_READ_VERSION,
readversion_func,
readverion_str1,
readverion_str2
},
{
"rmacaddr",
~0,
readmac_func,
readmac_str1,
readmac_str2
},
{
"wflash",
AX88179A_WRITE_FLASH,
writeflash_func,
writeflash_str1,
writeflash_str2
},
{
"wefuse",
AX88179A_PROGRAM_EFUSE,
writeefuse_func,
writeefuse_str1,
writeefuse_str2
},
{
"refuse",
AX88179A_DUMP_EFUSE,
readefuse_func,
readefuse_str1,
readefuse_str2
},
{
"reload",
~0,
reload_func,
reload_str1,
reload_str2
},
{
NULL,
0,
NULL,
NULL,
NULL
}
};
static unsigned char sample_type1[] = {
0x01, 0x0B, 0x95, 0x17,
0x90, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02,
0x00, 0x0A, 0x07, 0xFF,
0x17, 0x32, 0x20, 0x00
};
static unsigned char sample_type4[] = {
0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
static unsigned char sample_type11[] = {
0x0B, 0x1F, 0x00, 0x00,
0x00, 0x00, 0x1F, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x45, 0x00
};
#pragma pack(push)
#pragma pack(1)
enum _ef_Type_Def {
EF_TYPE_REV = 0x00,
EF_TYPE_01 = 0x01,
EF_TYPE_04 = 0x04,
EF_TYPE_11 = 0x0B,
};
struct _ef_type {
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned char checksum: 4;
unsigned char type : 4;
#else
unsigned char type : 4;
unsigned char checksum: 4;
#endif
};
struct _ef_type01 {
struct _ef_type type;
unsigned short vid;
unsigned short pid;
unsigned char mac[6];
unsigned short bcdDevice;
unsigned char bU1DevExitLat;
unsigned short wU2DevExitLat;
unsigned char SS_Max_Bus_Pw;
unsigned char HS_Max_Bus_Pw;
unsigned char IPSleep_Polling_Count;
unsigned char reserve;
};
#define EF_TYPE_STRUCT_SIZE_01 sizeof(struct _ef_type01)
struct _ef_type04 {
struct _ef_type type;
unsigned char serial[18];
unsigned char reserve;
};
#define EF_TYPE_STRUCT_SIZE_04 sizeof(struct _ef_type04)
struct _ef_type11 {
struct _ef_type type;
unsigned char dev_type0;
unsigned short reg0;
unsigned short value0;
unsigned char dev_type1;
unsigned short reg1;
unsigned short value1;
unsigned char dev_type2;
unsigned short reg2;
unsigned short value2;
unsigned char reserved1[2];
unsigned char subtype;
unsigned char reserved2;
};
#define EF_TYPE_STRUCT_SIZE_11 sizeof(struct _ef_type11)
struct _ef_data_struct {
union {
struct _ef_type01 type01;
struct _ef_type04 type04;
struct _ef_type11 type11;
} ef_data;
};
#define EF_DATA_STRUCT_SIZE sizeof(struct _ef_data_struct)
#pragma pack(pop)
static void show_usage(void)
{
int i;
printf("Usage:\n");
for (i = 0; ax88179a_cmd_list[i].cmd != NULL; i++)
printf("%s\n", ax88179a_cmd_list[i].help_ins);
}
static unsigned long STR_TO_U32(const char *cp, char **endp, unsigned int base)
{
unsigned long result = 0, value;
if (*cp == '0') {
cp++;
if ((*cp == 'x') && isxdigit(cp[1])) {
base = 16;
cp++;
}
if (!base)
base = 8;
}
if (!base)
base = 10;
while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
? toupper(*cp) : *cp)-'A'+10) < base) {
result = result*base + value;
cp++;
}
if (endp)
*endp = (char *)cp;
return result;
}
static int help_func(struct ax_command_info *info)
{
int i;
if (info->argv[2] == NULL)
return -FAIL_INVALID_PARAMETER;
for (i = 0; ax88179a_cmd_list[i].cmd != NULL; i++) {
if (strncmp(info->argv[2],
ax88179a_cmd_list[i].cmd,
strlen(ax88179a_cmd_list[i].cmd)) == 0) {
printf("%s%s\n", ax88179a_cmd_list[i].help_ins,
ax88179a_cmd_list[i].help_desc);
return -FAIL_INVALID_PARAMETER;
}
}
return SUCCESS;
}
static int autosuspend_enable(struct ax_command_info *info,
unsigned char enable)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
struct _ax_ioctl_command ioctl_cmd;
int ret;
DEBUG_PRINT("=== %s - Start\n", __func__);
ioctl_cmd.ioctl_cmd = AX88179A_AUTOSUSPEND_EN;
ioctl_cmd.autosuspend.enable = enable;
ifr->ifr_data = (caddr_t)&ioctl_cmd;
ret = ioctl(info->inet_sock, AX_PRIVATE, ifr);
if (ret < 0) {
PRINT_IOCTL_FAIL(ret);
return -FAIL_IOCTL;
}
return SUCCESS;
}
static int read_version(struct ax_command_info *info, char *version)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
struct _ax_ioctl_command ioctl_cmd;
int ret;
DEBUG_PRINT("=== %s - Start\n", __func__);
ioctl_cmd.ioctl_cmd = AX88179A_READ_VERSION;
memset(ioctl_cmd.version.version, 0, 16);
ifr->ifr_data = (caddr_t)&ioctl_cmd;
ret = ioctl(info->inet_sock, AX_PRIVATE, ifr);
if (ret < 0) {
PRINT_IOCTL_FAIL(ret);
return -FAIL_IOCTL;
}
memcpy(version, ioctl_cmd.version.version, 16);
return SUCCESS;
}
static int read_mac_address(struct ax_command_info *info, unsigned char *mac)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
int ret;
DEBUG_PRINT("=== %s - Start\n", __func__);
ret = scan_ax_device(ifr, info->inet_sock);
if (ret < 0) {
PRINT_SCAN_DEV_FAIL;
return ret;
}
ifr->ifr_flags &= 0;
ret = ioctl(info->inet_sock, SIOCSIFFLAGS, ifr);
if (ret < 0) {
PRINT_IOCTL_FAIL(ret);
return ret;
}
usleep(20000);
ifr->ifr_flags = IFF_UP | IFF_BROADCAST | IFF_MULTICAST;
ret = ioctl(info->inet_sock, SIOCSIFFLAGS, ifr);
if (ret < 0) {
PRINT_IOCTL_FAIL(ret);
return ret;
}
usleep(20000);
ret = ioctl(info->inet_sock, SIOCGIFHWADDR, ifr);
if (ret < 0) {
PRINT_IOCTL_FAIL(ret);
return ret;
}
memcpy(mac, ifr->ifr_hwaddr.sa_data, 6);
return SUCCESS;
}
static int readversion_func(struct ax_command_info *info)
{
char version[16] = {0};
int i, ret;
DEBUG_PRINT("=== %s - Start\n", __func__);
if (info->argc != 2) {
for (i = 0; ax88179a_cmd_list[i].cmd != NULL; i++) {
if (strncmp(info->argv[1], ax88179a_cmd_list[i].cmd,
strlen(ax88179a_cmd_list[i].cmd)) == 0) {
printf("%s%s\n", ax88179a_cmd_list[i].help_ins,
ax88179a_cmd_list[i].help_desc);
return -FAIL_INVALID_PARAMETER;
}
}
}
autosuspend_enable(info, 0);
ret = read_version(info, version);
if (ret == SUCCESS)
printf("Firmware Version: %s\n", version);
usleep(20000);
autosuspend_enable(info, 1);
return ret;
}
static int readmac_func(struct ax_command_info *info)
{
unsigned char mac[6] = {0};
int i, ret;
DEBUG_PRINT("=== %s - Start\n", __func__);
if (info->argc != 2) {
for (i = 0; ax88179a_cmd_list[i].cmd != NULL; i++) {
if (strncmp(info->argv[1], ax88179a_cmd_list[i].cmd,
strlen(ax88179a_cmd_list[i].cmd)) == 0) {
printf("%s%s\n", ax88179a_cmd_list[i].help_ins,
ax88179a_cmd_list[i].help_desc);
return -FAIL_INVALID_PARAMETER;
}
}
}
ret = read_mac_address(info, mac);
if (ret == SUCCESS)
printf("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
mac[0],
mac[1],
mac[2],
mac[3],
mac[4],
mac[5]);
return ret;
}
static int write_flash(struct ax_command_info *info, unsigned char *data,
unsigned long offset, unsigned long len)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
struct _ax_ioctl_command ioctl_cmd;
int ret;
DEBUG_PRINT("=== %s - Start\n", __func__);
ioctl_cmd.ioctl_cmd = AX88179A_WRITE_FLASH;
ioctl_cmd.flash.status = 0;
ioctl_cmd.flash.offset = offset;
ioctl_cmd.flash.length = len;
ioctl_cmd.flash.buf = data;
ifr->ifr_data = (caddr_t)&ioctl_cmd;
ret = ioctl(info->inet_sock, AX_PRIVATE, ifr);
if (ret < 0) {
if (ioctl_cmd.flash.status)
fprintf(stderr, "FLASH WRITE status: %d",
ioctl_cmd.flash.status);
PRINT_IOCTL_FAIL(ret);
return ret;
}
return SUCCESS;
}
static int read_flash(struct ax_command_info *info, unsigned char *data,
unsigned long offset, unsigned long len)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
struct _ax_ioctl_command ioctl_cmd;
int ret;
DEBUG_PRINT("=== %s - Start\n", __func__);
ioctl_cmd.ioctl_cmd = AX88179A_READ_FLASH;
ioctl_cmd.flash.status = 0;
ioctl_cmd.flash.offset = offset;
ioctl_cmd.flash.length = len;
ioctl_cmd.flash.buf = data;
ifr->ifr_data = (caddr_t)&ioctl_cmd;
ret = ioctl(info->inet_sock, AX_PRIVATE, ifr);
if (ret < 0) {
if (ioctl_cmd.flash.status)
fprintf(stderr, "FLASH READ status: %d",
ioctl_cmd.flash.status);
PRINT_IOCTL_FAIL(ret);
return -FAIL_IOCTL;
}
return SUCCESS;
}
static int erase_flash(struct ax_command_info *info)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
struct _ax_ioctl_command ioctl_cmd;
int ret;
DEBUG_PRINT("=== %s - Start\n", __func__);
ioctl_cmd.ioctl_cmd = AX88179A_ERASE_FLASH;
ioctl_cmd.flash.status = 0;
ifr->ifr_data = (caddr_t)&ioctl_cmd;
ret = ioctl(info->inet_sock, AX_PRIVATE, ifr);
if (ret < 0) {
PRINT_IOCTL_FAIL(ret);
return -FAIL_IOCTL;
}
return SUCCESS;
}
static int boot_to_rom(struct ax_command_info *info)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
struct _ax_ioctl_command ioctl_cmd;
DEBUG_PRINT("=== %s - Start\n", __func__);
ioctl_cmd.ioctl_cmd = AX88179A_ROOT_2_ROM;
ifr->ifr_data = (caddr_t)&ioctl_cmd;
ioctl(info->inet_sock, AX_PRIVATE, ifr);
return SUCCESS;
}
static int sw_reset(struct ax_command_info *info)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
struct _ax_ioctl_command ioctl_cmd;
DEBUG_PRINT("=== %s - Start\n", __func__);
ioctl_cmd.ioctl_cmd = AX88179A_SW_RESET;
ifr->ifr_data = (caddr_t)&ioctl_cmd;
ioctl(info->inet_sock, AX_PRIVATE, ifr);
usleep(RELOAD_DELAY_TIME * 1000000);
return SUCCESS;
}
static int writeflash_func(struct ax_command_info *info)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
unsigned char *wbuf = NULL, *rbuf = NULL;
FILE *pFile = NULL;
size_t result;
int length = 0;
int i, offset, len, ret;
char fw_version[16] = {0};
DEBUG_PRINT("=== %s - Start\n", __func__);
if (info->argc != 3) {
for (i = 0; ax88179a_cmd_list[i].cmd != NULL; i++) {
if (strncmp(info->argv[1], ax88179a_cmd_list[i].cmd,
strlen(ax88179a_cmd_list[i].cmd)) == 0) {
printf("%s%s\n", ax88179a_cmd_list[i].help_ins,
ax88179a_cmd_list[i].help_desc);
return -FAIL_INVALID_PARAMETER;
}
}
}
autosuspend_enable(info, 0);
boot_to_rom(info);
usleep(1000000);
ret = scan_ax_device(ifr, info->inet_sock);
if (ret < 0) {
PRINT_SCAN_DEV_FAIL;
return ret;
}
ret = erase_flash(info);
if (ret < 0)
return ret;
pFile = fopen(info->argv[2], "rb");
if (pFile == NULL) {
fprintf(stderr, "%s: Fail to open %s file.\n",
__func__, info->argv[2]);
ret = -FAIL_LOAD_FILE;
goto fail;
}
fseek(pFile, 0, SEEK_END);
length = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
wbuf = (unsigned char *)malloc((length + 256) & ~(0xFF));
if (!wbuf) {
PRINT_ALLCATE_MEM_FAIL;
ret = -FAIL_ALLCATE_MEM;
goto fail;
}
memset(wbuf, 0, (length + 256) & ~(0xFF));
rbuf = (unsigned char *)malloc((length + 256) & ~(0xFF));
if (!rbuf) {
PRINT_ALLCATE_MEM_FAIL;
ret = -FAIL_ALLCATE_MEM;
goto fail;
}
memset(rbuf, 0, (length + 256) & ~(0xFF));
result = fread(wbuf, 1, length, pFile);
if (result != length) {
PRINT_LOAD_FILE_FAIL;
ret = -PRINT_LOAD_FILE_FAIL;
goto fail;
}
offset = SWAP_32(*(unsigned long *)&wbuf[4]);
len = (SWAP_32(*(unsigned long *)&wbuf[8]) + 256) & ~(0xFF);
sprintf(fw_version, "v%d.%d.%d",
wbuf[offset + 0x1000],
wbuf[offset + 0x1001],
wbuf[offset + 0x1002]);
printf("File FW Version: %s\n", fw_version);
ret = write_flash(info, wbuf, offset, len);
if (ret < 0)
goto fail;
ret = read_flash(info, rbuf, offset, len);
if (ret < 0)
goto fail;
if (memcmp(&wbuf[offset], &rbuf[offset], len) != 0) {
fprintf(stderr, "%s: Program the FW failed.\n", __func__);
ret = -FAIL_FLASH_WRITE;
goto fail;
}
len = SWAP_32(*(unsigned long *)&wbuf[4]);
ret = write_flash(info, wbuf, 0, len);
if (ret < 0)
goto fail;
ret = read_flash(info, rbuf, 0, (length + 256) & ~(0xFF));
if (ret < 0)
goto fail;
if (memcmp(wbuf, rbuf,
(SWAP_32(*(unsigned long *)&wbuf[8]) +
SWAP_32(*(unsigned long *)&wbuf[4]))) != 0) {
fprintf(stderr, "%s: Program the Flash failed.\n", __func__);
ret = -FAIL_FLASH_WRITE;
goto fail;
}
ret = SUCCESS;
goto out;
fail:
erase_flash(info);
out:
if (rbuf)
free(rbuf);
if (wbuf)
free(wbuf);
if (pFile)
fclose(pFile);
autosuspend_enable(info, 1);
return ret;
}
#define EFUSE_NUM_BLOCK 32
static void checksum_efuse_block(unsigned char *block)
{
unsigned int Sum = 0;
int j;
DEBUG_PRINT("=== %s - Start\n", __func__);
for (j = 0; j < 4; j++) {
if (j == 0)
Sum += block[j] & 0xF;
else
Sum += block[j];
}
while (Sum > 0xF)
Sum = (Sum & 0xF) + (Sum >> 4);
Sum = 0xF - Sum;
block[0] = (block[0] & 0xF) | ((Sum << 4) & 0xF0);
}
static int __find_efuse_index(struct _ef_data_struct *efuse,
enum _ef_Type_Def type)
{
int i;
DEBUG_PRINT("=== %s - Start\n", __func__);
for (i = 5; i < EFUSE_NUM_BLOCK; i++) {
if (efuse[i].ef_data.type01.type.type == type)
return i;
}
return -FAIL_GENERIAL_ERROR;
}
static int change_mac_address(struct _ef_data_struct *efuse, unsigned int *mac)
{
int index, i;
DEBUG_PRINT("=== %s - Start\n", __func__);
index = __find_efuse_index(efuse, EF_TYPE_01);
if (index == -FAIL_GENERIAL_ERROR) {
fprintf(stderr, "%s: Not found type 1 from eFuese file\n",
__func__);
return -FAIL_GENERIAL_ERROR;
}
for (i = 0; i < 6; i++)
efuse[index].ef_data.type01.mac[i] = (unsigned char)mac[i];
checksum_efuse_block((unsigned char *)&efuse[index]);
return SUCCESS;
}
static int change_serial_number(struct _ef_data_struct *efuse, char *serial)
{
int index;
DEBUG_PRINT("=== %s - Start\n", __func__);
index = __find_efuse_index(efuse, EF_TYPE_04);
if (index == -FAIL_GENERIAL_ERROR) {
fprintf(stderr, "%s: Not found type 4 from eFuese file\n",
__func__);
return -FAIL_GENERIAL_ERROR;
}
memset(efuse[index].ef_data.type04.serial, 0, 18);
memcpy(efuse[index].ef_data.type04.serial, serial, strlen(serial));
checksum_efuse_block((unsigned char *)&efuse[index]);
return SUCCESS;
}
static void set_led(struct _ef_data_struct *efuse, char *led, int led_num)
{
DEBUG_PRINT("=== %s - Start\n", __func__);
memcpy(efuse, sample_type11, EF_TYPE_STRUCT_SIZE_11);
efuse->ef_data.type11.reg0 = htobe16(0x0024 + (led_num << 1));
efuse->ef_data.type11.reg1 = htobe16(0x0025 + (led_num << 1));
led[4] = '\0';
efuse->ef_data.type11.value0 = htobe16(strtol(led, NULL, 16));
efuse->ef_data.type11.value1 = htobe16(strtol(&led[5], NULL, 16));
checksum_efuse_block((unsigned char *)efuse);
}
static int load_efuse_from_file(char *file_path, unsigned char *data)
{
FILE *pFile = NULL;
int i, j;
DEBUG_PRINT("=== %s - Start\n", __func__);
if (!file_path)
return -FAIL_LOAD_FILE;
pFile = fopen(file_path, "rb");
if (pFile == NULL) {
fprintf(stderr, "%s: Fail to open %s file.\n",
__func__, file_path);
return -FAIL_LOAD_FILE;
}
for (i = 0; i < (20 * EFUSE_NUM_BLOCK); i += 4) {
unsigned int size = 0;
for (j = 3; j >= 0; j--) {
unsigned int tmp;
size = fscanf(pFile, "%02X ", &tmp);
if (size == ~0)
break;
data[i + j] = tmp & 0xFF;
}
if (size == ~0)
break;
}
if (pFile)
fclose(pFile);
return SUCCESS;
}
static int __dump_efuse(struct ax_command_info *info,
struct _ef_data_struct *efuse,
int block_offset, int block_num)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
int i, limit = (block_offset + block_num);
struct _ax_ioctl_command ioctl_cmd;
DEBUG_PRINT("=== %s - Start\n", __func__);
if (limit > EFUSE_NUM_BLOCK) {
fprintf(stderr, "%s: Invalid dump block size\n", __func__);
return -FAIL_IVALID_VALUE;
}
if (scan_ax_device(ifr, info->inet_sock)) {
PRINT_SCAN_DEV_FAIL;
return -FAIL_SCAN_DEV;
}
ioctl_cmd.ioctl_cmd = AX88179A_DUMP_EFUSE;
ioctl_cmd.flash.length = 20;
ifr->ifr_data = (caddr_t)&ioctl_cmd;
for (i = block_offset; i < limit; i++) {
int ret;
ioctl_cmd.flash.status = 0;
ioctl_cmd.flash.offset = i;
ioctl_cmd.flash.buf = (unsigned char *)&efuse[i];
ret = ioctl(info->inet_sock, AX_PRIVATE, ifr);
if (ret < 0) {
if (ioctl_cmd.flash.status)
fprintf(stderr, "FLASH READ status: %d",
ioctl_cmd.flash.status);
PRINT_IOCTL_FAIL(ret);
return -FAIL_IOCTL;
}
usleep(200000);
}
return SUCCESS;
}
static int dump_efuse_from_chip(struct ax_command_info *info,
struct _ef_data_struct *efuse)
{
DEBUG_PRINT("=== %s - Start\n", __func__);
return __dump_efuse(info, efuse, 0, 32);
}
static int find_empty_block_index(struct _ef_data_struct *efuse)
{
int i;
DEBUG_PRINT("=== %s - Start\n", __func__);
for (i = 5; i < EFUSE_NUM_BLOCK; i++) {
if (efuse[i].ef_data.type01.type.type == EF_TYPE_REV)
break;
}
return (i == EFUSE_NUM_BLOCK) ? -1 : i;
}
#ifdef ENABLE_IOCTL_DEBUG
static void dump_efuse_data(struct _ef_data_struct *efuse)
{
int i;
unsigned char *data = (unsigned char *)efuse;
for (i = 0; i < (20 * EFUSE_NUM_BLOCK); i += 4) {
printf("%02X %02X %02X %02X",
data[i + 3], data[i + 2], data[i + 1], data[i]);
if (i % 20 == 0)
printf(" == %d", i / 20);
printf("\n");
}
}
#define DUMP_EFUSE_DATA(efuse) dump_efuse_data(efuse)
#else
#define DUMP_EFUSE_DATA(efuse) while(0){}
#endif
static int check_efuse_block_valid(unsigned char *data)
{
unsigned int sum = 0;
unsigned int tmp;
int j = 0;
DEBUG_PRINT("=== %s - Start\n", __func__);
if ((data[0] & 0xF) == EF_TYPE_REV)
return -FAIL_IVALID_VALUE;
for (j = 0; j < 4; j++) {
if (j == 0)
sum += data[j] & 0xF;
else
sum += data[j];
}
while (sum > 0xF)
sum = (sum & 0xF) + (sum >> 4);
sum = 0xF - sum;
tmp = (data[0] & 0xF) | ((sum << 4) & 0xF0);
if (tmp != data[0])
return -FAIL_IVALID_CHKSUM;
return SUCCESS;
}
static int merge_efuse(struct _ef_data_struct *dump_efuse,
struct _ef_data_struct *file_efuse,
unsigned int *program_block, unsigned int *program_index)
{
int dump_empty_index, i, j;
DEBUG_PRINT("=== %s - Start\n", __func__);
dump_empty_index = find_empty_block_index(dump_efuse);
if (dump_empty_index < 0) {
fprintf(stderr, "%s: Non empty block.\n", __func__);
return -FAIL_NON_EMPTY_RFUSE_BLOCK;
}
*program_block = 0;
*program_index = -1;
for (i = 5, j = dump_empty_index; i < EFUSE_NUM_BLOCK; i++, j++) {
unsigned char *block = (unsigned char *)&file_efuse[i];
int ret;
if (file_efuse[i].ef_data.type01.type.type == EF_TYPE_REV)
break;
ret = check_efuse_block_valid(block);
if (ret < 0) {
fprintf(stderr,
"%s: ERROR eFuse block in file.\n", __func__);
return ret;
}
memcpy(&dump_efuse[j], &file_efuse[i], EF_DATA_STRUCT_SIZE);
*program_block += 1;
}
*program_index = dump_empty_index;
return SUCCESS;
}
static int __program_efuse_block(struct ax_command_info *info,
struct _ef_data_struct *efuse,
unsigned int block_offset,
unsigned int block_num)
{
struct ifreq *ifr = (struct ifreq *)info->ifr;
int i, limit = (block_offset + block_num);
struct _ax_ioctl_command ioctl_cmd;
DEBUG_PRINT("=== %s - Start\n", __func__);
if (limit > EFUSE_NUM_BLOCK) {
fprintf(stderr, "%s: eFuse block not enough\n", __func__);
return -FAIL_IVALID_VALUE;