/
bcmutils.c
2012 lines (1719 loc) · 42.9 KB
/
bcmutils.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
// #define CDEBUG
#include <sys/sysinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include <syslog.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#undef _LINUX_KERNEL_H
#include <sys/sysinfo.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <resolv.h>
#include <signal.h>
#include <utils.h>
#include <wlutils.h>
#include <bcmnvram.h>
#include <shutils.h>
#include <cy_conf.h>
#include <code_pattern.h>
#include <bcmdevs.h>
#include <net/route.h>
#include <cy_conf.h>
#include <linux/if_ether.h>
// #include <linux/mii.h>
#include <linux/sockios.h>
#include <broadcom.h>
#include <md5.h>
#define SIOCGMIIREG 0x8948 /* Read MII PHY register. */
#define SIOCSMIIREG 0x8949 /* Write MII PHY register. */
struct mii_ioctl_data {
unsigned short phy_id;
unsigned short reg_num;
unsigned short val_in;
unsigned short val_out;
};
int getcpurev(void)
{
FILE *fp = fopen("/proc/cpuinfo", "rb");
if (fp == NULL) {
return -1;
}
int cnt = 0;
int b = 0;
while (b != EOF) {
b = getc(fp);
if (b == ':')
cnt++;
if (cnt == 1) {
getc(fp);
char cpumodel[65];
int i = 0;
for (i = 0; i < 64; i++) {
cpumodel[i] = getc(fp);
if (cpumodel[i] == '\n')
break;
if (cpumodel[i] == ':') {
cnt++;
break;
}
}
cpumodel[i] = 0;
if (strstr(cpumodel, "BCM5357")) {
fclose(fp);
return 51;
}
}
if (cnt == 3) {
getc(fp);
char cpurev[33];
int i = 0;
for (i = 0; i < 32; i++) {
cpurev[i] = getc(fp);
if (cpurev[i] == '\n')
break;
}
cpurev[i] = 0;
fclose(fp);
if (strstr(cpurev, "BCM4710 V0.0") || strstr(cpurev, "BMIPS3300 V0.0")) // BCM4702, BCM4710
// (old 125 MHz)
return 0;
if (strstr(cpurev, "BCM3302 V0.6") || strstr(cpurev, "BMIPS3300 V0.6")) // BCM4704
return 6;
if (strstr(cpurev, "BCM3302 V0.7") || strstr(cpurev, "BMIPS3300 V0.7")) // BCM4712, BCM5365
return 7;
if (strstr(cpurev, "BCM3302 V0.8") || strstr(cpurev, "BMIPS3300 V0.8")) // BCM5350, BCM5352
return 8;
if (strstr(cpurev, "BCM3302 V2.9") || strstr(cpurev, "BMIPS3300 V2.9")) // BCM5354
return 29;
if (strstr(cpurev, "BCM3302 V1.10") || strstr(cpurev, "BMIPS3300 V1.10")) // BCM4785 (BCM3302 V1.10)
return 110;
if (strstr(cpurev, "MIPS 74K V4.9")) // BCM4716B0 (Broadcom BCMB83A chip rev 0)
return 50; // BCM5356B0 (Broadcom BCM5356 chip rev 1)
if (strstr(cpurev, "MIPS 74K V4.0")) // BCM4716B0 (Broadcom BCMB83A chip rev 0)
return 51; // BCM5356B0 (Broadcom BCM5356 chip rev 1)
if (strstr(cpurev, "MIPS 74Kc V4.9")) // BCM4716B0 (Broadcom BCMB83A chip rev 0) //newer kernels
return 50; // BCM5356B0 (Broadcom BCM5356 chip rev 1)
if (strstr(cpurev, "MIPS 74Kc V4.0")) // BCM4716B0 (Broadcom BCMB83A chip rev 0) //newer kernels
return 51; // BCM5356B0 (Broadcom BCM5356 chip rev 1)
return -1;
}
}
fclose(fp);
return -1;
}
int cpu_plltype(void)
{
#ifdef HAVE_NORTHSTAR
FILE *fp = fopen("/proc/bcm_chipinfo", "rb");
if (!fp) {
return 9;
}
int chipid;
int chiprevision;
int packageoption;
fscanf(fp, "%*s %X\n", &chipid);
fscanf(fp, "%*s %X\n", &chiprevision);
fscanf(fp, "%*s %X\n", &packageoption);
fclose(fp);
if (chipid == 53573)
return 11;
if (packageoption == 2) {
if (chipid == 53030)
return 10; // 600 / 800 / 1000
else
return 9; // 600 / 800
}
if (packageoption == 0) {
if (chipid == 53030)
return 8; // 600 / 800 / 1000 / 1200 / 1400
else
return 7; // 600 / 800 / 1000
}
return 9;
#endif
#if defined(HAVE_BUFFALO) || defined(BUFFALO_JP)
if (nvram_match("DD_BOARD", "Buffalo WHR-G54S") || //
nvram_match("DD_BOARD", "Buffalo WHR-HP-G54") || //
nvram_match("DD_BOARD", "Buffalo AS-A100") || //
nvram_match("DD_BOARD", "Buffalo WHR-HP-G54DD")) //
return 0;
#endif
int cpurev = getcpurev();
int cputype = check_hw_type();
if (cpurev == 0) // BCM4702, BCM4710 (old 125 MHz)
return 0;
if (cpurev == 6) // BCM4704
return 0;
if (cpurev == 7 && cputype == BCM5365_CHIP) // BCM5365 only
// supports fixed 200
// MHz
return 0;
if (cpurev == 7 && cputype != BCM5365_CHIP) // BCM4712
return 4;
if (cpurev == 8 && cputype == BCM5350_CHIP) // BCM5350
return 3;
if (cpurev == 8 && cputype != BCM5350_CHIP) // BCM5352
return 7;
if (cpurev == 29) // BCM5354, only supports fixed 240 MHz
return 0;
if (cpurev == 110) // BCM4705
return 2;
if (cpurev == 50) // BCM4706
return 8;
if (cpurev == 51) // BCM4706
return 10;
return 0;
}
/* In the space-separated/null-terminated list(haystack), try to
* locate the string "needle"
*/
char *find_in_list(const char *haystack, const char *needle)
{
const char *ptr = haystack;
int needle_len = 0;
int haystack_len = 0;
int len = 0;
if (!haystack || !needle || !*haystack || !*needle)
return NULL;
needle_len = strlen(needle);
haystack_len = strlen(haystack);
while (*ptr != 0 && ptr < &haystack[haystack_len]) {
/* consume leading spaces */
ptr += strspn(ptr, " ");
/* what's the length of the next word */
len = strcspn(ptr, " ");
if ((needle_len == len) && (!strncmp(needle, ptr, len)))
return (char *)ptr;
ptr += len;
}
return NULL;
}
/**
* remove_from_list
* Remove the specified word from the list.
* @param name word to be removed from the list
* @param list Space separated list to modify
* @param listsize Max size the list can occupy
* @return error code
*/
int remove_from_list(const char *name, char *list, int listsize)
{
int listlen = 0;
int namelen = 0;
char *occurrence = list;
if (!list || !name || (listsize <= 0))
return EINVAL;
listlen = strlen(list);
namelen = strlen(name);
occurrence = find_in_list(occurrence, name);
if (!occurrence)
return EINVAL;
/* last item in list? */
if (occurrence[namelen] == 0) {
/* only item in list? */
if (occurrence != list)
occurrence--;
occurrence[0] = 0;
} else if (occurrence[namelen] == ' ') {
strncpy(occurrence, &occurrence[namelen + 1 /* space */ ],
strlen(&occurrence[namelen + 1 /* space */ ]) +
1 /* terminate */ );
}
return 0;
}
/**
* add_to_list
* Add the specified interface(string) to the list as long as
* it will fit in the space left in the list.
* NOTE: If item is already in list, it won't be added again.
* @param name Name of interface to be added to the list
* @param list List to modify
* @param listsize Max size the list can occupy
* @return error code
*/
int add_to_list(const char *name, char *list, int listsize)
{
int listlen = 0;
int namelen = 0;
if (!list || !name || (listsize <= 0))
return EINVAL;
listlen = strlen(list);
namelen = strlen(name);
/* is the item already in the list? */
if (find_in_list(list, name))
return 0;
if (listsize <= listlen + namelen + 1 /* space */ + 1 /* NULL */ )
return EMSGSIZE;
/* add a space if the list isn't empty and it doesn't already have space */
if (list[0] != 0 && list[listlen - 1] != ' ') {
list[listlen++] = 0x20;
}
strncpy(&list[listlen], name, namelen + 1 /* terminate */ );
return 0;
}
int count_occurences(char *source, int cmp)
{
int i, cnt = 0;
int len = strlen(source);
for (i = 0; i < len; i++) {
if (source[i] == cmp)
cnt++;
}
return cnt;
}
int pos_nthoccurence(char *source, int cmp, int which)
{
int i, cnt = 0;
int len = strlen(source);
for (i = 0; i < len; i++) {
if (source[i] == cmp)
cnt++;
if (cnt == which)
return i;
}
return -1;
}
char *substring(int start, int stop, const char *src, char *dst)
{
sprintf(dst, "%.*s", stop - start, src + start);
return dst;
}
/*
* given month, day, year, returns day of week, eg. Monday = 0 etc.
*/
int weekday(int month, int day, int year)
{
int ix, tx, vx = 0;
switch (month) {
case 2:
case 6:
vx = 0;
break;
case 8:
vx = 4;
break;
case 10:
vx = 8;
break;
case 9:
case 12:
vx = 12;
break;
case 3:
case 11:
vx = 16;
break;
case 1:
case 5:
vx = 20;
break;
case 4:
case 7:
vx = 24;
break;
}
if (year > 1900) // 1900 was not a leap year
year -= 1900;
ix = ((year - 21) % 28) + vx + (month > 2); // take care of
// February
tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
return (tx % 7);
}
const char *_getdefaultconfig(char *path, char *configname) {
sprintf(path, "/jffs/etc/%s", configname);
FILE *fp = fopen(path, "r"); //test if custom config is available
if (fp != NULL) {
fclose(fp);
} else {
sprintf(path, "/tmp/%s", configname);
}
return path;
}
char *get_ipfrominterface(char *ifname, char *ip)
{
int fd;
struct ifreq ifr;
struct ifreq ifrnm;
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFADDR, &ifr) < 0)
return "255.255.255.255/32";
memcpy(&ifrnm, &ifr, sizeof(ifr));
if (ioctl(fd, SIOCGIFNETMASK, &ifrnm) < 0)
return "255.255.255.255/32";
close(fd);
inet_addr_to_cidr(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr, ((struct sockaddr_in *)&ifrnm.ifr_netmask)->sin_addr, ip);
return ip;
}
char *get_mac_from_ip(char *mac, char *ip)
{
FILE *fp;
char line[256];
char ipa[50]; // ip address
char hwa[50]; // HW address / MAC
char mask[50]; // ntemask
char dev[50]; // interface
int type; // HW type
int flags; // flags
if ((fp = fopen("/proc/net/arp", "rb")) == NULL)
return NULL;
// Bypass header -- read until newline
if (fgets(line, sizeof(line) - 1, fp) != NULL) {
// Read the ARP cache entries.
// IP address HW type Flags HW address Mask Device
// 192.168.1.1 0x1 0x2 00:90:4C:21:00:2A * eth0
while (fgets(line, sizeof(line) - 1, fp)) {
if (sscanf(line, "%s 0x%x 0x%x %49s %49s %49s\n", ipa, &type, &flags, hwa, mask, dev) != 6)
continue;
if (strcmp(ip, ipa))
continue;
strncpy(mac, hwa, 17);
fclose(fp);
return mac;
}
}
fclose(fp);
return NULL;
}
static void add_dnslist_internal(struct dns_lists *dns_list, char *dns, int custom, int ipv6)
{
int i;
if (strcmp(dns, "0.0.0.0") && strcmp(dns, "")) {
int match = 0;
for (i = 0; i < dns_list->num_servers; i++) { // Skip same DNS
if (!strcmp(dns_list->dns_server[i].ip, dns))
match = 1;
}
if (!match) {
dns_list->dns_server = (struct dns_entry *)realloc(dns_list->dns_server, sizeof(struct dns_entry) * (dns_list->num_servers + 1));
dns_list->dns_server[dns_list->num_servers].ip = strdup(dns);
dns_list->dns_server[dns_list->num_servers].type = custom;
dns_list->dns_server[dns_list->num_servers].ipv6 = ipv6;
dns_list->num_servers++;
}
}
}
static void add_dnslist(struct dns_lists *dns_list, char *dns, int custom, int ipv6)
{
char *next, word[32];
if (!dns)
return;
foreach(word, dns, next) {
add_dnslist_internal(dns_list, word, custom, ipv6);
}
}
void free_dns_list(struct dns_lists *dns_list)
{
int i;
if (!dns_list)
return;
for (i = 0; i < dns_list->num_servers; i++) {
free(dns_list->dns_server[i].ip);
}
free(dns_list->dns_server);
free(dns_list);
}
struct dns_entry *get_dns_entry(struct dns_lists *dns_list, int idx)
{
if (!dns_list || idx > (dns_list->num_servers - 1))
return NULL;
return &dns_list->dns_server[idx];
}
struct dns_lists *get_dns_list(int v6)
{
struct dns_lists *dns_list = NULL;
int altdns_index = 1;
dns_list = (struct dns_lists *)malloc(sizeof(struct dns_lists));
bzero(dns_list, sizeof(struct dns_lists));
char *sv_localdns = nvram_safe_get("sv_localdns");
char *wan_dns = nvram_safe_get("wan_dns");
char *wan_get_dns = nvram_safe_get("wan_get_dns");
/*
* if < 3 DNS servers found, try to insert alternates
*/
while (altdns_index <= 3) {
char altdnsvar[32] = {
0
};
snprintf(altdnsvar, 31, "altdns%d", altdns_index);
if (*(nvram_safe_get(altdnsvar))) {
add_dnslist(dns_list, nvram_safe_get(altdnsvar), 1, 0);
}
altdns_index++;
}
if (*sv_localdns)
add_dnslist(dns_list, sv_localdns, 0, 0);
if (nvram_invmatch("wan_ignore_dns", "1")) {
if (*wan_dns) {
add_dnslist(dns_list, wan_dns, 0, 0);
}
if (*wan_get_dns) {
add_dnslist(dns_list, wan_get_dns, 0, 0);
}
}
#ifdef HAVE_IPV6
if (v6 && nvram_matchi("ipv6_enable", 1)) {
char *a1 = nvram_safe_get("ipv6_dns1");
char *a2 = nvram_safe_get("ipv6_dns2");
if (*a1)
add_dnslist(dns_list, a1, 1, 1);
if (*a2)
add_dnslist(dns_list, a2, 1, 1);
if (nvram_invmatch("wan_ignore_dns", "1")) {
char *next, *wordlist = nvram_safe_get("ipv6_get_dns");
char word[64];
foreach(word, wordlist, next) {
add_dnslist(dns_list, word, 0, 1);
}
}
}
#endif
return dns_list;
}
int dns_to_resolv(void)
{
FILE *fp_w;
struct dns_lists *dns_list = NULL;
int i = 0;
/*
* Save DNS to resolv.conf
*/
if (!(fp_w = fopen(RESOLV_FILE, "w"))) {
perror(RESOLV_FILE);
return errno;
}
if (nvram_invmatch("wan_get_domain", "")) {
fprintf(fp_w, "search %s\n", nvram_safe_get("wan_get_domain"));
} else if (nvram_invmatch("wan_domain", "")) {
fprintf(fp_w, "search %s\n", nvram_safe_get("wan_domain"));
}
if (nvram_invmatch("lan_domain", "")) {
fprintf(fp_w, "search %s\n", nvram_safe_get("lan_domain"));
}
if (nvram_matchi("dnsmasq_enable", 1)) {
fprintf(fp_w, "nameserver %s\n", nvram_safe_get("lan_ipaddr"));
fclose(fp_w);
if (!(fp_w = fopen(RESOLV_FORW, "w"))) {
perror(RESOLV_FORW);
return errno;
}
}
dns_list = get_dns_list(0);
for (i = 0; i < dns_list->num_servers; i++)
fprintf(fp_w, "nameserver %s\n", dns_list->dns_server[i].ip);
/*
* Put a pseudo DNS IP to trigger Connect On Demand
*/
if (dns_list->num_servers == 0 && (nvram_match("wan_proto", "pppoe")
|| nvram_match("wan_proto", "pptp")
#ifdef HAVE_PPPOEDUAL
|| nvram_match("wan_proto", "pppoe_dual")
#endif
#ifdef HAVE_PPPOATM
|| nvram_match("wan_proto", "pppoa")
#endif
#ifdef HAVE_L2TP
|| nvram_match("wan_proto", "l2tp")
#endif
#ifdef HAVE_3G
|| nvram_match("wan_proto", "3g")
#endif
) && nvram_matchi("ppp_demand", 1))
fprintf(fp_w, "nameserver 1.1.1.1\n");
fclose(fp_w);
free_dns_list(dns_list);
eval("touch", "/tmp/hosts");
return 1;
}
/*
* Example: lan_ipaddr = 192.168.1.1 get_dns_ip("lan_ipaddr", 1); produces
* "168"
*/
int get_single_ip(char *ipaddr, int which)
{
static int ip[4] = {
0, 0, 0, 0
};
int ret;
ret = sscanf(ipaddr, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
return ip[which];
}
char *get_complete_ip(char *from, char *to)
{
static char ipaddr[20];
int i[4];
if (!from || !to)
return "0.0.0.0";
if (sscanf(from, "%d.%d.%d.%d", &i[0], &i[1], &i[2], &i[3]) != 4)
return "0.0.0.0";
snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%s", i[0], i[1], i[2], to);
return ipaddr;
}
char *get_complete_lan_ip(char *ip)
{
static char ipaddr[20];
int i[4];
if (sscanf(nvram_safe_get("lan_ipaddr"), "%d.%d.%d.%d", &i[0], &i[1], &i[2], &i[3]) != 4)
return "0.0.0.0";
snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%s", i[0], i[1], i[2], ip);
return ipaddr;
}
/*
* =====================================================================================
* by tallest
* =====================================================================================
*/
int get_int_len(int num)
{
char buf[80];
snprintf(buf, sizeof(buf), "%d", num);
return strlen(buf);
}
#define READ_BUF_SIZE 254
/*
* from busybox find_pid_by_name.c
*/
pid_t *find_pid_by_name(char *pidName)
{
DIR *dir;
struct dirent *next;
pid_t *pidList = NULL;
int i = 0;
dir = opendir("/proc");
while ((next = readdir(dir)) != NULL) {
FILE *status;
char filename[READ_BUF_SIZE];
char buffer[READ_BUF_SIZE];
char name[READ_BUF_SIZE];
/*
* Must skip ".." since that is outside /proc
*/
if (strcmp(next->d_name, "..") == 0)
continue;
/*
* If it isn't a number, we don't want it
*/
if (!isdigit(*next->d_name))
continue;
sprintf(filename, "/proc/%s/status", next->d_name);
if (!(status = fopen(filename, "r"))) {
continue;
}
if (fgets(buffer, READ_BUF_SIZE - 1, status) == NULL) {
fclose(status);
continue;
}
fclose(status);
/*
* Buffer should contain a string like "Name: binary_name"
*/
sscanf(buffer, "%*s %s", name);
// printf("buffer=[%s] name=[%s]\n",buffer,name);
if (strcmp(name, pidName) == 0) {
pidList = realloc(pidList, sizeof(pid_t) * (i + 2));
if (pidList)
pidList[i++] = strtol(next->d_name, NULL, 0);
}
}
if (pidList)
pidList[i] = 0;
else {
pidList = realloc(pidList, sizeof(pid_t));
if (pidList)
pidList[0] = -1;
}
return pidList;
}
/*
* Find first process pid with same name from ps command
*/
int find_pid_by_ps(char *pidName)
{
FILE *fp;
int pid = -1;
char line[254];
if ((fp = popen("ps", "r"))) {
while (fgets(line, sizeof(line), fp) != NULL) {
if (strstr(line, pidName)) {
sscanf(line, "%d", &pid);
printf("%s pid is %d\n", pidName, pid);
break;
}
}
pclose(fp);
}
return pid;
}
/*
* Find all process pid with same name from ps command
*/
int *find_all_pid_by_ps(char *pidName)
{
FILE *fp;
int pid = -1;
char line[254];
int *pidList = NULL;
int i = 0;
cprintf("Search for %s\n", pidName);
if ((fp = popen("ps", "r"))) {
while (fgets(line, sizeof(line), fp) != NULL) {
if (strstr(line, pidName)) {
sscanf(line, "%d", &pid);
cprintf("%s pid is %d\n", pidName, pid);
pidList = realloc(pidList, sizeof(int) * (i + 2));
pidList[i++] = pid;
}
}
pclose(fp);
}
if (pidList)
pidList[i] = 0;
else {
pidList = realloc(pidList, sizeof(int));
pidList[0] = -1;
}
cprintf("Search done...\n");
return pidList;
}
void encode(char *buf, int len)
{
int i;
char ch;
for (i = 0; i < len; i++) {
ch = (buf[i] & 0x03) << 6;
buf[i] = (buf[i] >> 2);
buf[i] &= 0x3f;
buf[i] |= ch;
buf[i] = ~buf[i];
}
}
void decode(char *buf, int len)
{
int i;
char ch;
for (i = 0; i < len; i++) {
ch = (buf[i] & 0xC0) >> 6;
buf[i] = (buf[i] << 2) | ch;
buf[i] = ~buf[i];
}
}
/*
* v1.41.7 => 014107 v1.2 => 0102
*/
int convert_ver(char *ver)
{
char buf[10];
int v[3];
int ret;
ret = sscanf(ver, "v%d.%d.%d", &v[0], &v[1], &v[2]);
if (ret == 2) {
snprintf(buf, sizeof(buf), "%02d%02d", v[0], v[1]);
return atoi(buf);
} else if (ret == 3) {
snprintf(buf, sizeof(buf), "%02d%02d%02d", v[0], v[1], v[2]);
return atoi(buf);
} else
return -1;
}
int check_now_boot(void)
{
char *ver = nvram_safe_get("pmon_ver");
char *cfe = nvram_safe_get("CFEver");
// for 4712
// The boot_ver value is lower v2.0 (no included)
if (!strncmp(ver, "PMON", 4)) {
cprintf("The boot is PMON\n");
return PMON_BOOT;
}
// for 4712
// The boot_ver value is higher v2.0 (included)
else if (!strncmp(ver, "CFE", 3)) {
cprintf("The boot is CFE\n");
return CFE_BOOT;
} else if (!strncmp(ver, "2", 1)) {
cprintf("The boot is CFE %s\n", ver);
return CFE_BOOT;
} else if (!strncmp(cfe, "MotoWR", 6)) {
cprintf("The boot is Motorola CFE\n");
return CFE_BOOT;
} else {
cprintf("The boot is UNKNOWN\n");
return UNKNOWN_BOOT;
}
}
int check_hw_type(void)
{
char *boardtype = nvram_safe_get("boardtype");
uint boardflags = strtoul(nvram_safe_get("boardflags"), NULL, 0);
uint btype = strtoul(boardtype, NULL, 0);
char *vlan0 = nvram_safe_get("vlan0ports");
char *vlan1 = nvram_safe_get("vlan1ports");
if (!strncmp(boardtype, "bcm94710", 8))
return BCM4702_CHIP;
else if (btype == 0x0708 && !(boardflags & BFL_ENETADM))
return BCM5325E_CHIP;
else if (btype == 0x456 && getRouterBrand() == ROUTER_BELKIN_F5D7231) // stupid
// Belkin!
return BCM5352E_CHIP;
else if (btype == 0x456)
return BCM5350_CHIP;
else if (!strncmp(boardtype, "bcm95365", 8))
return BCM5365_CHIP;
else if (btype == 0x048e)
return BCM5354G_CHIP;
else if (btype == 0x042f && !(boardflags & BFL_ENETADM))
return BCM4704_BCM5325F_CHIP;
else if (btype == 0x478 && strstr(vlan0, "5*")) /* WRT300NV1.1 */
return BCM4705L_BCM5325E_EWC_CHIP;
else if ((btype == 0x478 && (strstr(vlan0, "8*") || strstr(vlan1, "8*"))) || nvram_match("boot_hw_model", "WRT350N") || nvram_match("boot_hw_model", "WRT610N")) /* WRT350N
*/
return BCM4705_BCM5397_EWC_CHIP;
else if (btype == 0x489 || nvram_match("boot_hw_model", "WRT310N")) /* WRT310N,
* temporal
* boardtype
* 0x478,
* wait
* for
* braodcom's
* txt
* file
*/
return BCM4705G_BCM5395S_EWC_CHIP;
else if (btype == 0x0467)
return BCM5352E_CHIP;
else if (btype == 0x0101 && !(boardflags & BFL_ENETADM))
return BCM4712_BCM5325E_CHIP;
else if ((btype == 0x0101 || btype == 0x0446) && (boardflags & BFL_ENETADM)) // 0x446
// is
// wap54g
// v2
return BCM4712_CHIP;
else if (btype == 0x0472 && !(boardflags & BFL_ENETADM))
return BCM4704_BCM5325F_EWC_CHIP;
else
return NO_DEFINE_CHIP;
}
static char *device_name[] = {
"eth0", "qos0"
};
char *get_device_name(void)
{
int i;
switch (check_hw_type()) {
case BCM5325E_CHIP:
case BCM5350_CHIP:
case BCM5365_CHIP:
case BCM4704_BCM5325F_CHIP:
case BCM4704_BCM5325F_EWC_CHIP:
case BCM5352E_CHIP:
case BCM5354G_CHIP:
case BCM4712_BCM5325E_CHIP:
case BCM4705L_BCM5325E_EWC_CHIP:
i = 0;
break;
case BCM4702_CHIP:
case BCM4712_CHIP:
default:
i = 1;
break;
}
return device_name[i];
}
char *strncpyz(char *dest, char const *src, size_t size)
{
if (!size--)
return dest;
strncpy(dest, src, size);
dest[size] = 0; /* Make sure the string is null terminated */
return dest;
}
static int sockets_open(int domain, int type, int protocol)
{
int fd = socket(domain, type, protocol);
if (fd < 0)
cprintf("sockets_open: no usable address was found.\n");
return fd;
}
int sys_netdev_ioctl(int family, int socket, char *if_name, int cmd, struct ifreq *ifr)
{
int rc, s;
if ((s = socket) < 0) {
if ((s = sockets_open(family, family == AF_PACKET ? SOCK_PACKET : SOCK_DGRAM, family == AF_PACKET ? htons(ETH_P_ALL) : 0)) < 0) {
cprintf("sys_netdev_ioctl: failed\n");
return -1;
}
}
strncpyz(ifr->ifr_name, if_name, IFNAMSIZ);
rc = ioctl(s, cmd, ifr);
if (socket < 0)
close(s);
return rc;
}
int set_register_value(unsigned short port_addr, unsigned short option_content)
{