hc
2024-05-10 61598093bbdd283a7edc367d900f223070ead8d2
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
/*==========================================================================
 * Module Name : console_debug.c
 * Purpose     : 
 * Author      : 
 * Date        : 
 * Notes       :
 * $Log$
 *==========================================================================
 */
 
/* INCLUDE FILE DECLARATIONS */
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/sockios.h>
#include <ctype.h>
#include <stdlib.h>
#if NET_INTERFACE == INTERFACE_SCAN
#include <ifaddrs.h>
#endif
#include "ioctl.h"
 
/* STATIC VARIABLE DECLARATIONS */
#define AX88772C_IOCTL_VERSION        "AX88772C/AX88772B/AX88772A/AX88760/AX88772/AX88178 Linux SROM IOCTL Tool version 1.6.0"
 
/* LOCAL SUBPROGRAM DECLARATIONS */
static unsigned long STR_TO_U32(const char *cp,char **endp,unsigned int base);
 
 
/* LOCAL SUBPROGRAM BODIES */
static void debug_func(char *func_name, unsigned short *buf, unsigned long wLen)
{
#if (DEBUG_PARAM & DEB_TOOL)
   int i;
   char str_buf[50];
   printf("%s :\n", func_name);
   printf("---------------------------------------\n");
   for (i = 0; i < wLen / 8; i++) {
       int j = 8 * i;
       snprintf(str_buf, 50,
                "%04x %04x %04x %04x %04x %04x "
                "%04x %04x\n", 
                *(buf + j + 0), *(buf + j + 1),
                *(buf + j + 2), *(buf + j + 3),
                *(buf + j + 4), *(buf + j + 5),
                *(buf + j + 6), *(buf + j + 7));
       printf("%s", str_buf);
   }
   printf("------------------------------------%3ld\n", wLen);
#endif
}
 
 
static void show_usage(void)
{
   int i;
   printf ("\n%s\n",AX88772C_IOCTL_VERSION);
   printf ("Usage:\n");
   for (i = 0; command_list[i].cmd != NULL; i++)
       printf ("%s\n", command_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;
}
 
void help_func (struct ax_command_info *info)
{
   int i;
 
   if (info->argv[2] == NULL) {
       for(i=0; command_list[i].cmd != NULL; i++) {
           printf ("%s%s\n", command_list[i].help_ins, command_list[i].help_desc);
       }
   }
 
   for (i = 0; command_list[i].cmd != NULL; i++)
   {
       if (strncmp(info->argv[1], command_list[i].cmd, strlen(command_list[i].cmd)) == 0 ) {
           printf ("%s%s\n", command_list[i].help_ins, command_list[i].help_desc);
           return;
       }
   }
 
}
 
int compare_file(struct ax_command_info *info)
{
   struct ifreq *ifr = (struct ifreq *)info->ifr;
   unsigned short *rout_buf;
   unsigned short *ori_buf;
   AX_IOCTL_COMMAND *ioctl_cmd = (AX_IOCTL_COMMAND *)(ifr->ifr_data);
   int i;
 
   rout_buf = malloc(sizeof(unsigned short) * ioctl_cmd->size);
 
   ori_buf = ioctl_cmd->buf;    
 
   ioctl_cmd->ioctl_cmd = AX_READ_EEPROM;
   ioctl_cmd->buf = rout_buf;
 
   if (ioctl(info->inet_sock, AX_PRIVATE, ifr) < 0) {
       perror("ioctl");
       return -1;
   }
 
   
 
   for (i = 0; i < ioctl_cmd->size; i++) {
       if (*(ioctl_cmd->buf + i) != *(ori_buf + i)) {
           debug_func("compare_file reeprom", rout_buf, ioctl_cmd->size);    
           ioctl_cmd->buf = ori_buf;
           free(rout_buf);                
           return -2;
       }
   }
 
   ioctl_cmd->buf = ori_buf;
   free(rout_buf);
   return 0;
}
 
void readeeprom_func(struct ax_command_info *info)
{
   struct ifreq *ifr = (struct ifreq *)info->ifr;
   AX_IOCTL_COMMAND ioctl_cmd;
   unsigned short *buf;
   unsigned short wLen;
   char str_buf[50];
   FILE *pFile;
   int i;    
 
   if (info->argc < 4) {
       for(i=0; command_list[i].cmd != NULL; i++) {
           if (strncmp(info->argv[1], command_list[i].cmd, 
                   strlen(command_list[i].cmd)) == 0 ) {
               printf ("%s%s\n", command_list[i].help_ins, 
                       command_list[i].help_desc);
               return;
           }
       }
   }    
 
   wLen = STR_TO_U32(info->argv[3], NULL, 0) / 2;
 
   pFile = fopen(info->argv[2],"w");
   buf = (unsigned short *)malloc((sizeof(unsigned short) * wLen));
 
   ioctl_cmd.ioctl_cmd = info->ioctl_cmd;
   ioctl_cmd.size = wLen;
   ioctl_cmd.buf = buf;
   ioctl_cmd.delay = 0;
 
   ifr->ifr_data = (caddr_t)&ioctl_cmd;
   if (ioctl(info->inet_sock, AX_PRIVATE, ifr) < 0) {
       perror("ioctl");
       free(buf);
       fclose(pFile);
       return;
   }
   for (i = 0; i < wLen / 8; i++) {
       int j = 8 * i;
       snprintf(str_buf, 50,
                "%04x %04x %04x %04x %04x %04x "
                "%04x %04x\n", 
                *(buf + j + 0), *(buf + j + 1),
                *(buf + j + 2), *(buf + j + 3),
                *(buf + j + 4), *(buf + j + 5),
                *(buf + j + 6), *(buf + j + 7));
       fputs(str_buf, pFile);
   }
   printf("Read completely\n\n");
   free(buf);
   fclose(pFile);
   return;
}
 
void writeeeprom_func(struct ax_command_info *info)
{
   struct ifreq *ifr = (struct ifreq *)info->ifr;
   AX_IOCTL_COMMAND ioctl_cmd;
   int i;
   unsigned short *buf;
   unsigned short wLen;
   char c[2] = {'\0'};
   FILE *pFile;
   unsigned char retried = 0;
 
   if (info->argc < 4) {
       for(i=0; command_list[i].cmd != NULL; i++) {
           if (strncmp(info->argv[1], command_list[i].cmd,
                   strlen(command_list[i].cmd)) == 0) {
               printf ("%s%s\n", command_list[i].help_ins,
                       command_list[i].help_desc);
               return;
           }
       }
   }
 
   pFile = fopen(info->argv[2], "r");
 
   wLen = STR_TO_U32(info->argv[3], NULL, 0) / 2;
 
   buf = (unsigned short *)malloc(sizeof(unsigned short) * wLen);
 
   for (i = 0; i < wLen / 8; i++) {
       int j = 8 * i;
       fscanf(pFile, "%04X %04X %04X %04X %04X %04X %04X %04X%c", 
               (unsigned int *)&buf[j + 0], (unsigned int *)&buf[j + 1],
               (unsigned int *)&buf[j + 2], (unsigned int *)&buf[j + 3],
               (unsigned int *)&buf[j + 4], (unsigned int *)&buf[j + 5],
               (unsigned int *)&buf[j + 6], (unsigned int *)&buf[j + 7], c);
   }
 
   ioctl_cmd.ioctl_cmd = info->ioctl_cmd;
   ioctl_cmd.size = wLen;
   ioctl_cmd.buf = buf;
   ioctl_cmd.delay = 5;
io:    
   ifr->ifr_data = (caddr_t)&ioctl_cmd;
 
debug_func("writeeeprom_func", buf, wLen);
 
     if (ioctl(info->inet_sock, AX_PRIVATE, ifr) < 0) {
       free(buf);
       fclose(pFile);
       perror("ioctl");
       return;
   }
   else {
       if (compare_file(info) && retried < 3) { 
           ioctl_cmd.delay += 5;
           ioctl_cmd.ioctl_cmd = info->ioctl_cmd;
           retried++;
           goto io;
       }
       if (retried == 3) {
           printf("Failure to write\n\n");
           free(buf);
           fclose(pFile);
           return;
       }
   }
 
   printf("Write completely\n\n");
   free(buf);
   fclose(pFile);    
   return;
}
 
void chgmac_func(struct ax_command_info *info)
{
   struct ifreq *ifr = (struct ifreq *)info->ifr;
   AX_IOCTL_COMMAND ioctl_cmd;
   int i;
   unsigned short *buf;
   unsigned int tmp;
   unsigned short wLen;
   unsigned char retried = 0;
   char * pch;
   unsigned int MAC[6] = {0};
   int ret = 0;
 
   if (info->argc < 4) {
       for(i=0; command_list[i].cmd != NULL; i++) {
           if (strncmp(info->argv[1], command_list[i].cmd,
                   strlen(command_list[i].cmd)) == 0) {
               printf ("%s%s\n", command_list[i].help_ins,
                       command_list[i].help_desc);
               return;
           }
       }
   }
 
   wLen = STR_TO_U32(info->argv[3], NULL, 0) / 2;
 
   buf = (unsigned short *)malloc(sizeof(unsigned short) * wLen);
 
   ioctl_cmd.ioctl_cmd = AX_READ_EEPROM;
   ioctl_cmd.size = wLen;
   ioctl_cmd.buf = buf;
   ioctl_cmd.delay = 0;
 
   ifr->ifr_data = (caddr_t)&ioctl_cmd;
 
   if (ioctl(info->inet_sock, AX_PRIVATE, ifr) < 0) {
       perror("ioctl");
       free(buf);
       return;
   }
 
   ret = sscanf(info->argv[2], "%02X:%02X:%02X:%02X:%02X:%02X",(unsigned int*)&MAC[0],
                               (unsigned int*)&MAC[1],
                               (unsigned int*)&MAC[2],
                               (unsigned int*)&MAC[3],
                               (unsigned int*)&MAC[4],
                               (unsigned int*)&MAC[5]);
   if (6 != ret) {
       printf("Invalid MAC address\n\n");
       return;
   }
   
   if (*buf == 0x1500) {
       printf("No eeprom exists!\n");
       printf("Or you must burn the default value first!\n\n");
       return;
   }
 
   *(((char*)buf) + 8) = (unsigned char)MAC[1];
   *(((char*)buf) + 9) = (unsigned char)MAC[0];
   *(((char*)buf) + 10) = (unsigned char)MAC[3];
   *(((char*)buf) + 11) = (unsigned char)MAC[2];
   *(((char*)buf) + 12) = (unsigned char)MAC[5];
   *(((char*)buf) + 13) = (unsigned char)MAC[4]; 
 
   ioctl_cmd.ioctl_cmd = info->ioctl_cmd;
   ioctl_cmd.size = wLen;
   ioctl_cmd.buf = buf;
   ioctl_cmd.delay = 5;
 
io:    
   ifr->ifr_data = (caddr_t)&ioctl_cmd;
 
debug_func("chgmac_func", buf, wLen);
 
      if (ioctl(info->inet_sock, AX_PRIVATE, ifr) < 0) {
       perror("ioctl");
       free(buf);
       return;
   }
 
   else {
       if (compare_file(info) && retried < 3) { 
           ioctl_cmd.delay += 5;
           ioctl_cmd.ioctl_cmd = info->ioctl_cmd;
           retried++;
           goto io;
       }
       if (retried == 3) {
           printf("Failure to write\n\n");
           free(buf);
           return;
       }
   }
 
   printf("Write completely\n\n");
   free(buf);
   return;
}
 
/* EXPORTED SUBPROGRAM BODIES */
int main(int argc, char **argv)
{
#if NET_INTERFACE == INTERFACE_SCAN
   struct ifaddrs *addrs, *tmp;
   unsigned char    dev_exist;
#endif    
   struct ifreq ifr;
   struct ax_command_info info;
   AX_IOCTL_COMMAND ioctl_cmd;
   int inet_sock;
   unsigned char i;    
 
   if (argc < 2) {
       show_usage();
       return 0;
   }
 
   inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
 
#if NET_INTERFACE == INTERFACE_SCAN
   /* Get Device */
   getifaddrs(&addrs);
   tmp = addrs;
   dev_exist = 0;
 
   while (tmp) {
       memset (&ioctl_cmd, 0, sizeof (AX_IOCTL_COMMAND));
       ioctl_cmd.ioctl_cmd = AX_SIGNATURE;
       // get network interface name
       sprintf (ifr.ifr_name, "%s", tmp->ifa_name);
 
       ifr.ifr_data = (caddr_t)&ioctl_cmd;
       tmp = tmp->ifa_next;
 
 
       if (ioctl (inet_sock, AX_PRIVATE, &ifr) < 0) {            
           continue;
       }
       
       if (strncmp (ioctl_cmd.sig, AX88772B_DRV_NAME, strlen(AX88772B_DRV_NAME)) == 0 ) {
           dev_exist = 1;
           break;
       }            
   }
 
   freeifaddrs(addrs);
 
   if (dev_exist == 0) {
       printf ("\n%s\n",AX88772C_IOCTL_VERSION);
       printf("No %s found\n\n", AX88772B_SIGNATURE);
       return 0;
   }
#else
   for (i = 0; i < 255; i++) {
 
       memset (&ioctl_cmd, 0, sizeof (AX_IOCTL_COMMAND));
       ioctl_cmd.ioctl_cmd = AX_SIGNATURE;
 
       sprintf (ifr.ifr_name, "eth%d", i);
       ifr.ifr_data = (caddr_t)&ioctl_cmd;
       
       if (ioctl (inet_sock, AX_PRIVATE, &ifr) < 0) {
           continue;
       }
 
       if (strncmp (ioctl_cmd.sig, AX88772B_DRV_NAME, strlen(AX88772B_DRV_NAME)) == 0 ) {
           break;
       }
 
   }
 
   if (i == 255) {
       printf ("\n%s\n",AX88772C_IOCTL_VERSION);
       printf ("No %s found\n\n", AX88772B_SIGNATURE);
       return 0;
   }
#endif
   for(i=0; command_list[i].cmd != NULL; i++)
   {
       if (strncmp(argv[1], command_list[i].cmd, strlen(command_list[i].cmd)) == 0 ) {
           printf ("\n%s\n",AX88772C_IOCTL_VERSION);
           info.help_ins = command_list[i].help_ins;
           info.help_desc = command_list[i].help_desc;
           info.ifr = &ifr;
           info.argc = argc;
           info.argv = argv;
           info.inet_sock = inet_sock;
           info.ioctl_cmd = command_list[i].ioctl_cmd;
           (command_list[i].OptFunc)(&info);
           return 0;
       }
   }
 
   printf ("Wrong command\n\n");
 
   return 0;
}