root/src/router/services/sysinit/sysinit-broadcom.c

Revision 12427, 44.7 kB (checked in by eko, 5 months ago)

wrt610n with boot_nv=6 fix: seems that if we change radio MAC it is not find anymore and we get reboot loop.

Line 
1 /*
2  * sysinit-broadcom.c
3  *
4  * Copyright (C) 2006 Sebastian Gottschall <gottschall@dd-wrt.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * $Id:
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <syslog.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <termios.h>
33 #include <sys/klog.h>
34 #include <sys/types.h>
35 #include <sys/mount.h>
36 #include <sys/reboot.h>
37 #include <sys/stat.h>
38 #include <sys/sysmacros.h>
39 #include <sys/time.h>
40 #include <sys/utsname.h>
41 #include <sys/wait.h>
42 #include <linux/if_ether.h>
43 #include <linux/sockios.h>
44 #include <net/if.h>
45
46 #include <arpa/inet.h>
47 #include <sys/socket.h>
48 #include <linux/sockios.h>
49
50 #include <bcmnvram.h>
51 #include <bcmdevs.h>
52 #include <shutils.h>
53 #include <utils.h>
54 #include <wlutils.h>
55 #include <cymac.h>
56 #include <services.h>
57
58 #define sys_restart() eval("event","3","1","1")
59 #define sys_reboot() eval("sync"); eval("event","3","1","15")
60
61 static void check_brcm_cpu_type(void)
62 {
63         FILE *fcpu;
64         char cpu_type[20];
65         char type2[30];
66
67         fcpu = fopen("/proc/cpuinfo", "r");
68
69         if (fcpu == NULL)
70                 cprintf("Open /proc/cpuinfo fail...0\n");
71         else {
72                 char buf[500];
73
74                 fgets(buf, 500, fcpu);
75                 fscanf(fcpu, "%s %s %s %s %s", buf, buf, buf, cpu_type, type2);
76                 if (!strcmp(type2, "BCM4704")) {
77                         nvram_set("cpu_type", cpu_type);
78                         fclose(fcpu);
79                         return;
80                 }
81                 if (!strcmp(type2, "BCM4712")) {
82                         nvram_set("cpu_type", cpu_type);
83                         fclose(fcpu);
84                         return;
85                 }
86                 if (!strcmp(type2, "BCM4702")) {
87                         nvram_set("cpu_type", cpu_type);
88                         fclose(fcpu);
89                         return;
90                 }
91                 if (!strcmp(type2, "BCM3302")) {
92                         nvram_set("cpu_type", cpu_type);
93                         fclose(fcpu);
94                         return;
95                 }
96                 fgets(buf, 500, fcpu);
97                 fscanf(fcpu, "%s %s %s %s %s", buf, buf, buf, cpu_type, type2);
98                 // fprintf(stderr, "cpu_type : %s\n", cpu_type);
99                 fclose(fcpu);
100                 if (!strcmp(cpu_type, "BCM4710")
101                     || !strcmp(cpu_type, "BCM4702")) {
102                         cprintf("We got BCM4702 board...\n");
103                         nvram_set("cpu_type", cpu_type);
104                 } else if (!strcmp(cpu_type, "BCM3302")
105                            || !strcmp(cpu_type, "BCM4712")) {
106                         cprintf("We got BCM4712 board...\n");
107                         nvram_set("cpu_type", cpu_type);
108                 } else {
109                         cprintf("We got unknown board...\n");
110                         nvram_set("cpu_type", cpu_type);
111                 }
112         }
113
114 }
115
116 static void loadWlModule(void)  // set wled params, get boardflags,
117                                         // set afterburner bit, load wl,
118                                         // unset afterburner bit
119 {
120
121         int brand = getRouterBrand();
122         char macbuf[32];
123         char eaddr[32];
124
125 #ifndef HAVE_BUFFALO
126         nvram_set("pa0maxpwr", "251");  // force pa0maxpwr to be 251
127 #endif
128
129         if (check_hw_type() == BCM4702_CHIP)
130                 nvram_unset("wl0_abenable");
131         else {
132                 nvram_set("wl0_abenable", "1");
133                 nvram_set("wl1_abenable", "1");
134         }
135
136         switch (brand) {
137         case ROUTER_LINKSYS_WRH54G:
138                 nvram_set("wl0gpio0", "135");
139                 break;
140         case ROUTER_BUFFALO_WZRRSG54:
141                 nvram_unset("wl0_abenable");
142                 nvram_unset("wl1_abenable");
143                 break;
144         case ROUTER_ASUS_WL550GE:
145                 nvram_set("wl0gpio1", "0");
146                 nvram_set("wl0gpio2", "0");
147                 break;
148         case ROUTER_ASUS_WL500W:
149         case ROUTER_WRT54G:
150         case ROUTER_WRT54G_V8:
151         case ROUTER_MOTOROLA:
152         case ROUTER_BUFFALO_WLAG54C:
153         case ROUTER_NETGEAR_WG602_V3:
154         case ROUTER_RT480W:
155                 nvram_set("wl0gpio0", "136");
156                 break;
157         case ROUTER_WAP54G_V3:
158                 nvram_set("wl0gpio0", "0");
159                 nvram_set("wl0gpio2", "255");
160                 nvram_set("wl0gpio3", "255");
161                 nvram_set("wl0gpio5", "136");
162                 break;
163         case ROUTER_ASUS_WL520GUGC:
164                 nvram_set("wl0gpio0", "0");
165                 nvram_set("wl0gpio1", "136");
166                 nvram_set("wl0gpio2", "0");
167                 nvram_set("wl0gpio3", "0");
168                 break;
169         case ROUTER_LINKSYS_WTR54GS:
170         case ROUTER_WAP54G_V1:
171                 nvram_set("wl0gpio0", "136");
172                 nvram_set("wl0gpio1", "0");
173                 nvram_set("wl0gpio2", "0");
174                 nvram_set("wl0gpio3", "0");
175                 break;
176         case ROUTER_BUFFALO_WBR54G:
177         case ROUTER_BUFFALO_WBR2G54S:
178         case ROUTER_WRT150N:
179         case ROUTER_WRT160N:
180         case ROUTER_WRT300N:
181         case ROUTER_WRT600N:
182         case ROUTER_WRT350N:
183         case ROUTER_WRT310N:
184         case ROUTER_WRT300NV11:
185         case ROUTER_USR_5461:
186                 nvram_set("wl0gpio0", "8");
187                 break;
188         case ROUTER_NETGEAR_WG602_V4:
189                 nvram_set("wl0gpio0", "8");
190                 nvram_set("wl0gpio1", "0");
191                 nvram_set("wl0gpio2", "0");
192                 nvram_set("wl0gpio3", "0");
193                 break;
194         case ROUTER_BUFFALO_WHRG54S:
195         case ROUTER_BUFFALO_WLI_TX4_G54HP:
196                 nvram_set("wl0gpio2", "136");
197                 break;
198         case ROUTER_BUFFALO_WLA2G54C:
199                 nvram_set("wl0gpio0", "0");
200                 nvram_set("wl0gpio5", "136");
201                 break;
202         case ROUTER_ASUS_WL500GD:
203         case ROUTER_ASUS_WL500G_PRE:
204                 nvram_unset("wl0gpio0");
205                 break;
206         case ROUTER_BELKIN_F5D7230_V2000:
207                 // case ROUTER_BELKIN_F5D7230_V3000:
208         case ROUTER_BELKIN_F5D7231:
209                 nvram_set("wl0gpio3", "136");
210                 break;
211         case ROUTER_NETGEAR_WGR614L:
212                 nvram_set("wl0gpio5", "8");
213                 break;
214         case ROUTER_BELKIN_F5D7231_V2000:
215                 nvram_set("wl0gpio0", "2");
216                 nvram_set("wl0gpio1", "0");
217                 break;
218         }
219
220         int boardflags;
221
222         switch (brand) {
223         case ROUTER_WRT150N:
224         case ROUTER_WRT150NV11:
225         case ROUTER_WRT160N:
226         case ROUTER_WRT310N:
227         case ROUTER_WRT300N:
228         case ROUTER_WRT300NV11:
229         case ROUTER_WRT350N:
230         case ROUTER_BUFFALO_WZRG144NH:
231         case ROUTER_BUFFALO_WZRG300N:
232         case ROUTER_NETGEAR_WNR834B:
233         case ROUTER_NETGEAR_WNR834BV2:
234         case ROUTER_NETGEAR_WNDR3300:
235         case ROUTER_ASUS_WL500W:
236                 break;
237         case ROUTER_WRT600N:
238                 fprintf(stderr, "fixing wrt600n\n");
239                 wl_hwaddr("eth0", macbuf);
240                 ether_etoa((uchar *) macbuf, eaddr);
241                 nvram_set("wl0_hwaddr", eaddr);
242                 MAC_SUB(eaddr);
243                 if (!nvram_match("et0macaddr", eaddr)) {
244                         nvram_set("et0macaddr", eaddr);
245                         nvram_commit();
246 //              eval("/sbin/reboot");
247 //              exit( 0 );
248                 }
249                 eval("/sbin/ifconfig", "eth2", "hw", "ether", eaddr);
250                 wl_hwaddr("eth1", macbuf);
251                 ether_etoa((uchar *) macbuf, eaddr);
252                 nvram_set("wl1_hwaddr", eaddr);
253                 break;
254         case ROUTER_WRT610N:
255                 wl_hwaddr("eth0", macbuf);
256                 ether_etoa((uchar *) macbuf, eaddr);
257                 nvram_set("wl0_hwaddr", eaddr);
258                 wl_hwaddr("eth1", macbuf);
259                 ether_etoa((uchar *) macbuf, eaddr);
260                 nvram_set("wl1_hwaddr", eaddr);
261                 break;
262
263         default:
264                 boardflags = strtoul(nvram_safe_get("boardflags"), NULL, 0);
265                 fprintf(stderr, "boardflags are 0x%04X\n", boardflags);
266                 if (boardflags == 0)    // we can try anyway
267                 {
268                         nvram_set("boardflags", "0x0200");
269                         insmod("wl");   // load module
270                         nvram_unset("boardflags");
271                 } else if (boardflags & BFL_AFTERBURNER)        // ab flag already
272                         // set
273                 {
274                         insmod("wl");   // load module
275                 } else          // ab flag not set
276                 {
277                         char bf[16];
278
279                         sprintf(bf, "0x%04X", boardflags);
280                         boardflags |= BFL_AFTERBURNER;
281                         fprintf(stderr,
282                                 "enable Afterburner, boardflags are 0x%04X\n",
283                                 boardflags);
284                         char ab[16];
285
286                         sprintf(ab, "0x%04X", boardflags);
287                         char *oldvalue = nvram_get("boardflags");       // use the
288
289                         // string for
290                         // restoring
291                         // since the
292                         // Buffalo
293                         // WZR-RS-G54
294                         // does await
295                         // a 0x10 in
296                         // the
297                         // bootloader,
298                         // otherwise
299                         // the nvram
300                         // gets
301                         // deleted
302                         nvram_set("boardflags", ab);    // set boardflags with
303                         // AfterBurner bit on
304                         insmod("wl");   // load module
305                         nvram_set("boardflags", oldvalue);      // set back to
306                         // original
307                 }
308
309         }
310 #ifdef HAVE_MADWIFI
311         insmod("ath_hal");
312         if (nvram_get("rate_control") != NULL) {
313                 char rate[64];
314
315                 sprintf(rate, "ratectl=%s", nvram_safe_get("rate_control"));
316                 eval("insmod", "ath_pci", rate);
317         } else {
318                 insmod("ath_pci");
319         }
320 #endif
321         return;
322 }
323
324 char wanifname[8], wlifname[8];
325
326 #define BCM4712_CPUTYPE "0x4712"
327
328 static void setup_4712(void)
329 {
330         uint boardflags = strtoul(nvram_safe_get("boardflags"), NULL, 0);
331
332         if (nvram_match("cpu_type", BCM4712_CPUTYPE) ||
333             nvram_match("cpu_type", "BCM3302") ||
334             nvram_match("cpu_type", "BCM4712")) {
335                 if (boardflags & BFL_ENETVLAN) {
336                         cprintf("setup_4712(): Enable VLAN\n");
337                         // nvram_set("setup_4712","1");
338                         strcpy(wanifname, "vlan1");
339                         strcpy(wlifname, "eth1");
340                         if (!strcmp(nvram_safe_get("wan_ifname"), "eth1")) {
341                                 // nvram_set("setup_4712","1-1");
342                                 nvram_set("wan_ifname", "vlan1");
343                                 nvram_set("wan_ifnames", "vlan1");
344                                 nvram_set("wan_default", "vlan1");
345                         }
346                         if (!strstr(nvram_safe_get("lan_ifnames"), "vlan0")) {
347                                 // nvram_set("setup_4712","1-2");
348                                 nvram_set("lan_ifnames", "vlan0 eth1");
349                                 nvram_set("vlan0hwname", "et0");
350                                 nvram_set("vlan1hwname", "et0");
351                                 nvram_set("wl0_ifname", "eth1");
352                                 // nvram_set ("need_commit","1");
353                         }
354                 }               // VLAN enabled
355                 else {
356                         // nvram_set("setup_4712","2");
357                         cprintf
358                             ("setup_4712(): Disable VLAN, it must be in bridge mode\n");
359                         nvram_set("lan_ifnames", "eth0 eth1");
360                         strcpy(wlifname, "eth1");
361                         nvram_set("wl0_ifname", "eth1");
362                 }
363         } else {                // 4702, 4704
364                 cprintf
365                     ("setup_4712(): It's a 4702 or 4704 hardware, VLAN can't be used in these 2 boards\n");
366                 strcpy(wanifname, "eth1");
367                 strcpy(wlifname, "eth2");
368                 nvram_set("wl0_ifname", "eth2");
369                 if (!strcmp(nvram_safe_get("wan_ifname"), ""))
370                         nvram_set("lan_ifnames",
371                                   "eth0 eth1 eth2 wlanb0 wlana0");
372                 else
373                         nvram_set("lan_ifnames", "eth0 eth2");
374         }
375         // nvram_set ("need_commit","1");
376
377 }
378
379 void start_sysinit(void)
380 {
381         char buf[PATH_MAX];
382         struct utsname name;
383         struct stat tmp_stat;
384         time_t tm = 0;
385
386         cprintf("sysinit() proc\n");
387         /*
388          * /proc
389          */
390         mount("proc", "/proc", "proc", MS_MGC_VAL, NULL);
391         cprintf("sysinit() tmp\n");
392
393         /*
394          * /tmp
395          */
396         mount("ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL);
397         eval("mkdir", "/tmp/www");
398 #ifdef HAVE_MICRO
399         mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
400 #endif
401
402         cprintf("sysinit() var\n");
403
404         /*
405          * /var
406          */
407         mkdir("/tmp/var", 0777);
408         mkdir("/var/lock", 0777);
409         mkdir("/var/lock/subsys", 0777);
410         mkdir("/var/log", 0777);
411         mkdir("/var/run", 0777);
412         mkdir("/var/tmp", 0777);
413         cprintf("sysinit() setup console\n");
414 #ifndef HAVE_MICRO
415         if (!nvram_match("disable_watchdog", "1"))
416                 eval("watchdog");       // system watchdog
417 #endif
418         /*
419          * Setup console
420          */
421
422         cprintf("sysinit() klogctl\n");
423         klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel")));
424         cprintf("sysinit() get router\n");
425
426         int brand = getRouterBrand();
427
428         led_control(LED_DIAG, LED_ON);
429         char *rname = getRouter();
430
431         fprintf(stderr, "Booting device: %s\n", rname);
432
433         nvram_unset("port_swap");
434
435         int need_reboot = 0;
436
437         struct nvram_tuple *basic_params = NULL;
438         struct nvram_tuple *extra_params = NULL;
439
440         struct nvram_tuple generic1[] = {
441                 {"lan_ifnames", "eth0 eth2", 0},
442                 {"wan_ifname", "eth1", 0},
443                 {"wl0_ifname", "eth2", 0},
444                 {0, 0, 0}
445         };
446
447         struct nvram_tuple generic1_wantolan[] = {
448                 {"lan_ifnames", "eth2", 0},
449                 {"wan_ifname", "eth0", 0},
450                 {"wl0_ifname", "eth2", 0},
451                 {0, 0, 0}
452         };
453
454         switch (brand) {
455         case ROUTER_BUFFALO_WZRRSG54:
456                 check_brcm_cpu_type();
457                 setup_4712();
458                 basic_params = generic1;
459                 eval("gpio", "init", "0");      // AOSS button
460                 eval("gpio", "init", "4");      // reset button
461                 break;
462
463         case ROUTER_MOTOROLA:
464                 nvram_set("cpu_type", "BCM4712");
465                 setup_4712();
466                 break;
467
468         case ROUTER_RT480W:
469                 setup_4712();
470                 break;
471
472         case ROUTER_BELKIN_F5D7231_V2000:
473                 nvram_set("lan_ifnames", "vlan0 eth1");
474                 nvram_set("wan_ifname", "vlan1");
475                 nvram_set("wl0_ifname", "eth1");
476                 if (nvram_match("vlan1ports", "0 5u")) {
477                         nvram_set("vlan0ports", "3 2 1 0 5*");
478                         nvram_set("vlan1ports", "4 5");
479                 }
480                 break;
481
482         case ROUTER_BELKIN_F5D7231:
483         case ROUTER_USR_5461:
484                 nvram_set("lan_ifnames", "vlan0 eth1");
485                 nvram_set("wan_ifname", "vlan1");
486                 nvram_set("wl0_ifname", "eth1");
487                 if (nvram_match("vlan1ports", "0 5u"))
488                         nvram_set("vlan1ports", "0 5");
489                 break;
490
491         case ROUTER_RT210W:
492         case ROUTER_ASKEY_RT220XD:
493                 basic_params = generic1;
494
495                 if (nvram_get("et0macaddr") == NULL
496                     || nvram_get("et0macaddr") == "") {
497                         nvram_set("et0macaddr", "00:16:E3:00:00:10");   // fix for
498                         // missing
499                         // cfe
500                         // default =
501                         // dead LAN
502                         // ports.
503                         need_reboot = 1;
504                 }
505                 if (nvram_get("et1macaddr") == NULL
506                     || nvram_get("et1macaddr") == "") {
507                         nvram_set("et1macaddr", "00:16:E3:00:00:11");
508                         need_reboot = 1;
509                 }
510                 break;
511
512         case ROUTER_BRCM4702_GENERIC:
513                 basic_params = generic1;
514
515                 if (nvram_get("et0macaddr") == NULL
516                     || nvram_get("et0macaddr") == "") {
517                         nvram_set("et0macaddr", "00:11:22:00:00:10");   // fix for
518                         // missing
519                         // cfe
520                         // default =
521                         // dead LAN
522                         // ports.
523                         need_reboot = 1;
524                 }
525                 if (nvram_get("et1macaddr") == NULL
526                     || nvram_get("et1macaddr") == "") {
527                         nvram_set("et1macaddr", "00:11:22:00:00:11");
528                         need_reboot = 1;
529                 }
530                 break;
531
532         case ROUTER_ASUS_WL500G:
533                 basic_params = generic1;
534
535                 if (nvram_get("et0macaddr") == NULL
536                     || nvram_get("et0macaddr") == "") {
537                         nvram_set("et0macaddr", "00:0C:6E:00:00:10");   // fix for
538                         // missing
539                         // cfe
540                         // default =
541                         // dead LAN
542                         // ports.
543                         need_reboot = 1;
544                 }
545                 if (nvram_get("et1macaddr") == NULL
546                     || nvram_get("et1macaddr") == "") {
547                         nvram_set("et1macaddr", "00:0C:6E:00:00:10");
548                         need_reboot = 1;
549                 }
550                 break;
551
552         case ROUTER_DELL_TRUEMOBILE_2300:
553                 setup_4712();
554                 nvram_set("wan_ifname", "eth1");        // fix for WAN problem.
555                 break;
556
557         case ROUTER_BUFFALO_WBR54G:     // for WLA-G54
558                 basic_params = generic1;
559                 if (nvram_match("wan_to_lan", "yes") && nvram_invmatch("wan_proto", "disabled"))        // =
560                         //
561                         // no
562                         // lan
563                 {
564                         basic_params = generic1_wantolan;
565                 }
566                 break;
567
568         case ROUTER_BUFFALO_WLI2_TX1_G54:
569         case ROUTER_BUFFALO_WLAG54C:
570         case ROUTER_WAP54G_V1:
571         case ROUTER_SITECOM_WL105B:
572                 nvram_set("lan_ifnames", "eth1 eth2");
573                 nvram_set("wl0_ifname", "eth2");
574                 nvram_set("wan_ifname", "eth0");        // WAN to nonexist. iface.
575                 nvram_set("port_swap", "1");
576                 if (nvram_match("wan_to_lan", "yes") && nvram_invmatch("wan_proto", "disabled"))        // =
577                         //
578                         // no
579                         // lan
580                 {
581                         nvram_set("lan_ifnames", "eth2");
582                         nvram_set("wan_ifname", "eth1");
583                 }
584                 break;
585
586         case ROUTER_SITECOM_WL111:
587                 basic_params = generic1;
588                 break;
589
590         case ROUTER_NETGEAR_WNR834BV2:
591                 if (nvram_match("force_vlan_supp", "enabled")) {
592                         nvram_set("lan_ifnames", "vlan0 eth2");
593                         nvram_set("wan_ifname", "eth1");
594                         nvram_set("vlan0ports", "3 2 1 0 5*");
595                         nvram_set("vlan1ports", "4 5"); //dummy
596                         nvram_set("vlan0hwname", "et0");
597                 } else {
598                         basic_params = generic1;
599                 }
600
601                 if (nvram_get("pci/1/1/macaddr") == NULL) {
602                         nvram_set("pci/1/1/macaddr",
603                                   nvram_safe_get("et0macaddr"));
604                         need_reboot = 1;
605                 }
606                 //params taken from firmware ver. 2.1.13 multi-region
607                 struct nvram_tuple wnr834bv2_pci_1_1_params[] = {
608                         {"pa2gw1a0", "0", 0},
609                         {"stbcpo", "0", 0},
610                         {"pa2gw1a1", "0", 0},
611                         {"ag0", "2", 0},
612                         {"ag1", "2", 0},
613                         {"ag2", "2", 0},
614                         {"ccdpo", "0", 0},
615                         {"txpid2ga0", "55", 0},
616                         {"txpid2ga1", "78", 0},
617                         {"txpt2g", "0x38", 0},
618                         {"pa2gw0a0", "0", 0},
619                         {"pa2gw0a1", "0", 0},
620                         {"boardflags", "0x200", 0},
621                         {"boardvendor", "0x14e4", 0},
622                         {"bw40po", "0", 0},
623                         {"sromrev", "4", 0},
624                         {"venid", "0x14e4", 0},
625                         {"boardrev", "0x4b", 0},
626                         {"itt2ga0", "0", 0},
627                         {"itt2ga1", "0", 0},
628                         {"pa2gw3a0", "0", 0},
629                         {"pa2gw3a1", "0", 0},
630                         {"maxp2ga0", "0", 0},
631                         {"maxp2ga1", "0", 0},
632                         {"boardtype", "0x46d", 0},
633                         {"boardflags2", "3", 0},
634                         {"ofdm2gpo", "0", 0},
635                         {"ledbh0", "0x8", 0},
636                         {"ledbh1", "-1", 0},
637                         {"ledbh2", "-1", 0},
638                         {"ledbh3", "-1", 0},
639                         {"mcs2gpo0", "0", 0},
640                         {"mcs2gpo1", "0", 0},
641                         {"mcs2gpo2", "0", 0},
642                         {"mcs2gpo3", "0", 0},
643                         {"mcs2gpo4", "0", 0},
644                         {"mcs2gpo5", "0", 0},
645                         {"mcs2gpo6", "0", 0},
646                         {"mcs2gpo7", "0", 0},
647                         {"bwduppo", "0", 0},
648                         {"aa2g", "7", 0},
649                         {"pa2gw2a0", "0", 0},
650                         {"pa2gw2a1", "0", 0},
651                         {"ccode", "all", 0},
652                         {"regrev", "0", 0},
653                         {"devid", "0x4329", 0},
654                         {"cck2gpo", "0", 0},
655                         {0, 0, 0}
656                 };
657                 /*
658                  * set router's extra parameters
659                  */
660                 extra_params = wnr834bv2_pci_1_1_params;
661                 while (extra_params->name) {
662                         nvram_nset(extra_params->value, "pci/1/1/%s",
663                                    extra_params->name);
664                         extra_params++;
665                 }
666                 break;
667
668         case ROUTER_NETGEAR_WNDR3300:
669                 nvram_set("lan_ifnames", "eth0 eth2 eth3");     // dual radio
670                 nvram_set("wan_ifname", "eth1");
671                 nvram_set("wl0_ifname", "eth2");
672                 nvram_set("wl1_ifname", "eth3");
673                 eval("gpio", "disable", "7");
674
675                 if (nvram_get("pci/1/1/macaddr") == NULL
676                     || nvram_get("pci/1/3/macaddr") == NULL) {
677                         unsigned char mac[20];
678
679                         strcpy(mac, nvram_safe_get("et0macaddr"));
680                         MAC_ADD(mac);
681                         MAC_ADD(mac);
682                         nvram_set("pci/1/1/macaddr", mac);
683                         MAC_ADD(mac);
684                         nvram_set("pci/1/3/macaddr", mac);
685                         need_reboot = 1;
686                 }
687                 //params taken from firmware ver. 1.0.29 multi-region
688                 struct nvram_tuple wndr3300_pci_1_1_params[] = {
689                         {"stbcpo", "0", 0},
690                         {"mcs5gpo0", "0x4200", 0},
691                         {"pa2gw1a0", "0x14EA", 0},
692                         {"mcs5gpo1", "0x6664", 0},
693                         {"pa2gw1a1", "0x14DA", 0},
694                         {"mcs5gpo2", "0x4200", 0},
695                         {"maxp5gha0", "0x4A", 0},
696                         {"mcs5gpo3", "0x6664", 0},
697                         {"maxp5gha1", "0x4A", 0},
698                         {"mcs5gpo4", "0", 0},
699                         {"mcs5gpo5", "0", 0},
700                         {"mcs5gpo6", "0", 0},
701                         {"aa5g", "7", 0},
702                         {"mcs5gpo7", "0", 0},
703                         {"pa5glw2a0", "0xFBA2", 0},
704                         {"pa5glw2a1", "0xFBDB", 0},
705                         {"ag0", "2", 0},
706                         {"ag1", "2", 0},
707                         {"ag2", "2", 0},
708                         {"pa5gw2a0", "0xFBBA", 0},
709                         {"pa5gw2a1", "0xFC11", 0},
710                         {"pa5ghw2a0", "0xFBB5", 0},
711                         {"pa5ghw2a1", "0xFBD2", 0},
712                         {"ccdpo", "0", 0},
713                         {"txpid2ga0", "52", 0},
714                         {"itt5ga0", "0x3C", 0},
715                         {"rxchain", "3", 0},
716                         {"txpid2ga1", "51", 0},
717                         {"itt5ga1", "0x3C", 0},
718                         {"maxp5ga0", "0x4A", 0},
719                         {"maxp5ga1", "0x4A", 0},
720                         {"txpt2g", "0x48", 0},
721                         {"pa2gw0a0", "0xFEFC", 0},
722                         {"pa2gw0a1", "0xFF03", 0},
723                         {"boardflags", "0x0A00", 0},
724                         {"mcs5glpo0", "0x4200", 0},
725                         {"pa5glw1a0", "0x120E", 0},
726                         {"mcs5glpo1", "0x6664", 0},
727                         {"ofdm5gpo", "0x88888888", 0},
728                         {"pa5glw1a1", "0x12BD", 0},
729                         {"mcs5glpo2", "0x4200", 0},
730                         {"mcs5glpo3", "0x6664", 0},
731                         {"mcs5glpo4", "0", 0},
732                         {"mcs5glpo5", "0", 0},
733                         {"mcs5glpo6", "0", 0},
734                         {"mcs5glpo7", "0", 0},
735                         {"boardvendor", "0x14e4", 0},
736                         {"bw40po", "0", 0},
737                         {"sromrev", "4", 0},
738                         {"venid", "0x14e4", 0},
739                         {"pa5gw1a0", "0x1337", 0},
740                         {"pa5gw1a1", "0x14A4", 0},
741                         {"pa5ghw1a0", "0x11C2", 0},
742                         {"pa5ghw1a1", "0x1275", 0},
743                         {"boardrev", "0x13", 0},
744                         {"itt2ga0", "0x3E", 0},
745                         {"itt2ga1", "0x3E", 0},
746                         {"pa2gw3a0", "0", 0},
747                         {"pa2gw3a1", "0", 0},
748                         {"maxp2ga0", "0x4A", 0},
749                         {"maxp2ga1", "0x4A", 0},
750                         {"boardtype", "0x49C", 0},
751                         {"boardflags2", "0x0014", 0},
752                         {"ofdm2gpo", "0x66666666", 0},
753                         {"ledbh0", "11", 0},
754                         {"ledbh1", "11", 0},
755                         {"pa5glw0a0", "0xFEFB", 0},
756                         {"ledbh2", "11", 0},
757                         {"pa5glw0a1", "0xFF5B", 0},
758                         {"ledbh3", "11", 0},
759                         {"ledbh4", "11", 0},
760                         {"ledbh5", "5", 0},
761                         {"ledbh6", "7", 0},
762                         {"ledbh7", "11", 0},
763                         {"mcs2gpo0", "0x6666", 0},
764                         {"mcs2gpo1", "0x6666", 0},
765                         {"mcs2gpo2", "0x6666", 0},
766                         {"mcs2gpo3", "0x6666", 0},
767                         {"txpid5gla0", "18", 0},
768                         {"mcs2gpo4", "0", 0},
769                         {"txpid5gla1", "14", 0},
770                         {"mcs2gpo5", "0", 0},
771                         {"txpt5g", "0x3C", 0},
772                         {"mcs2gpo6", "0", 0},
773                         {"mcs2gpo7", "0", 0},
774                         {"mcs5ghpo0", "0x4200", 0},
775                         {"mcs5ghpo1", "0x6664", 0},
776                         {"bwduppo", "0", 0},
777                         {"mcs5ghpo2", "0x4200", 0},
778                         {"mcs5ghpo3", "0x6664", 0},
779                         {"txchain", "3", 0},
780                         {"mcs5ghpo4", "0", 0},
781                         {"mcs5ghpo5", "0", 0},
782                         {"txpid5gha0", "28", 0},
783                         {"mcs5ghpo6", "0", 0},
784                         {"ofdm5glpo", "0x88888888", 0},
785                         {"txpid5gha1", "25", 0},
786                         {"mcs5ghpo7", "0", 0},
787                         {"antswitch", "2", 0},
788                         {"aa2g", "7", 0},
789                         {"pa5gw0a0", "0xFF3C", 0},
790                         {"pa5gw0a1", "0xFFEC", 0},
791                         {"ofdm5ghpo", "0x88888888", 0},
792                         {"pa5ghw0a0", "0xFEE8", 0},
793                         {"pa5ghw0a1", "0xFF72", 0},
794                         {"leddc", "0xFFFF", 0},
795                         {"pa2gw2a0", "0xFB44", 0},
796                         {"pa2gw2a1", "0xFB28", 0},
797                         {"pa5glw3a0", "0", 0},
798                         {"pa5glw3a1", "0", 0},
799                         {"ccode", "0", 0},
800                         {"pa5gw3a0", "0", 0},
801                         {"regrev", "0", 0},
802                         {"pa5gw3a1", "0", 0},
803                         {"devid", "0x4328", 0},
804                         {"pa5ghw3a0", "0", 0},
805                         {"pa5ghw3a1", "0", 0},
806                         {"txpt5gh", "0x3C", 0},
807                         {"cck2gpo", "0x0000", 0},
808                         {"txpt5gl", "0x30", 0},
809                         {"maxp5gla0", "0x4A", 0},
810                         {"txpid5ga0", "39", 0},
811                         {"maxp5gla1", "0x4A", 0},
812                         {"txpid5ga1", "39", 0},
813                         {0, 0, 0}
814                 };
815                 /*
816                  * set router's extra parameters
817                  */
818                 extra_params = wndr3300_pci_1_1_params;
819                 while (extra_params->name) {
820                         nvram_nset(extra_params->value, "pci/1/1/%s",
821                                    extra_params->name);
822                         extra_params++;
823                 }
824
825                 struct nvram_tuple wndr3300_pci_1_3_params[] = {
826                         {"ag0", "0x02", 0},
827                         {"boardflags", "0xAA48", 0},
828                         {"ccode", "0", 0},
829                         {"aa0", "0x03", 0},
830                         {"devid", "0x4318", 0},
831                         {"pa0b0", "0x14ed", 0},
832                         {"pa0b1", "0xfac7", 0},
833                         {"pa0b2", "0xfe8a", 0},
834                         {"pa0itssit", "62", 0},
835                         {"pa0maxpwr", "0x0042", 0},
836                         {"opo", "0", 0},
837                         {"wl0gpio0", "11", 0},
838                         {"wl0gpio1", "11", 0},
839                         {"wl0gpio2", "11", 0},
840                         {"wl0gpio3", "130", 0}, //7 is right value, but causes Oops. Set to 130 (act only, no on/off) or 3 (no act - just radio on/off)
841                         {"sromrev", "2", 0},
842                         {0, 0, 0}
843                 };
844                 /*
845                  * set router's extra parameters
846                  */
847                 extra_params = wndr3300_pci_1_3_params;
848                 while (extra_params->name) {
849                         nvram_nset(extra_params->value, "pci/1/3/%s",
850                                    extra_params->name);
851                         extra_params++;
852                 }
853                 break;
854
855         case ROUTER_MOTOROLA_WE800G:
856                 nvram_set("lan_ifnames", "eth1 eth2");
857                 nvram_set("wl0_ifname", "eth2");
858                 nvram_set("wan_ifname", "eth0");        // WAN to nonexist. iface.
859                 nvram_set("port_swap", "1");
860                 eval("gpio", "disable", "7");
861                 if (nvram_match("wan_to_lan", "yes") && nvram_invmatch("wan_proto", "disabled"))        // =
862                         //
863                         // no
864                         // lan
865                 {
866                         nvram_set("lan_ifnames", "eth2");
867                         nvram_set("wan_ifname", "eth1");
868                 }
869                 break;
870
871         case ROUTER_MOTOROLA_V1:
872                 nvram_set("wan_ifname", "eth1");
873                 eval("gpio", "disable", "7");
874                 break;
875
876         case ROUTER_BUFFALO_WZRG300N:
877         case ROUTER_NETGEAR_WNR834B:
878         case ROUTER_WRT150N:
879         case ROUTER_WRT300N:
880         case ROUTER_ASUS_WL500W:
881         case ROUTER_BUFFALO_WLAH_G54:
882         case ROUTER_BUFFALO_WAPM_HP_AM54G54:
883         case ROUTER_MICROSOFT_MN700:
884                 nvram_set("wan_ifname", "eth1");
885                 break;
886
887         case ROUTER_WRTSL54GS:
888         case ROUTER_WRT160N:
889                 nvram_set("wan_ifname", "eth1");
890                 if (nvram_match("force_vlan_supp", "enabled")) {
891                         nvram_set("lan_ifnames", "vlan0 eth2");
892                         nvram_set("vlan0ports", "0 1 2 3 5*");
893                         nvram_set("vlan1ports", "4 5"); //dummy
894                         nvram_set("vlan0hwname", "et0");
895                 } else {
896                         nvram_set("lan_ifnames", "eth0 eth2");
897                 }
898                 break;
899
900         case ROUTER_WRT54G1X:
901                 if (check_vlan_support()) {
902                         nvram_set("lan_ifnames", "vlan0 eth2");
903                         nvram_set("wan_ifname", "vlan1");
904                 }
905                 break;
906
907         case ROUTER_WRT350N:
908         case ROUTER_WRT310N:
909         case ROUTER_WRT600N:
910                 nvram_set("wan_ifname", "vlan2");
911                 break;
912
913         case ROUTER_WRT610N:
914                 nvram_set("wan_ifname", "vlan2");
915                 nvram_set("pci/1/1/ledbh0", "11");
916                 nvram_set("pci/1/1/ledbh1", "135");
917                 nvram_set("pci/1/2/ledbh0", "11");
918                 nvram_set("pci/1/2/ledbh2", "135");
919                 nvram_set("pci/1/1/boardflags2", "0x0400");
920                 nvram_set("pci/1/2/boardflags2", "0x0602");
921
922                 if (!nvram_match ("bootnv_ver", "6")) {
923                 if (startswith(nvram_safe_get("pci/1/1/macaddr"), "00:90:4C")
924                     || startswith(nvram_safe_get("pci/1/2/macaddr"),
925                                   "00:90:4C")) {
926                         unsigned char mac[20];
927                         strcpy(mac, nvram_safe_get("et0macaddr"));
928                         MAC_ADD(mac);
929                         MAC_ADD(mac);
930                         nvram_set("pci/1/1/macaddr", mac);
931                         MAC_ADD(mac);
932                         nvram_set("pci/1/2/macaddr", mac);
933                         need_reboot = 1;
934                 }
935                 }
936                 break;
937
938         case ROUTER_WRT300NV11:
939         case ROUTER_BUFFALO_WZRG144NH:
940                 nvram_set("wan_ifname", "vlan1");
941                 break;
942
943         case ROUTER_ASUS_WL500G_PRE:
944                 nvram_set("sdram_init", "0x0009");
945                 // nvram_set ("sdram_ncdl", "0x208");
946                 nvram_set("lan_ifnames", "vlan0 eth2");
947                 nvram_set("wl0_ifname", "eth2");
948                 nvram_set("wan_ifname", "vlan1");       // fix for Asus WL500gPremium
949                 //
950                 // WAN problem.
951                 if (nvram_match("vlan1ports", "0 5u"))
952                         nvram_set("vlan1ports", "0 5");
953                 break;
954
955         case ROUTER_ASUS_WL500GD:
956         case ROUTER_ASUS_WL550GE:
957                 nvram_set("wl0_ifname", "eth1");
958                 break;
959
960         case ROUTER_BUFFALO_WLA2G54C:
961         case ROUTER_WAP54G_V2:
962         case ROUTER_VIEWSONIC_WAPBR_100:
963         case ROUTER_USR_5430:
964         case ROUTER_BUFFALO_WLI_TX4_G54HP:
965         case ROUTER_BELKIN_F5D7230_V2000:
966         case ROUTER_NETGEAR_WG602_V3:
967         case ROUTER_NETGEAR_WG602_V4:
968         case ROUTER_ASUS_330GE:
969                 nvram_set("lan_ifnames", "eth0 eth1");
970                 nvram_set("wl0_ifname", "eth1");
971                 nvram_set("wan_ifname", "eth2");        // map WAN port to
972                 // nonexistant interface
973                 if (nvram_match("wan_to_lan", "yes") && nvram_invmatch("wan_proto", "disabled"))        // =
974                         //
975                         // no
976                         // lan
977                 {
978                         nvram_set("lan_ifnames", "eth1");
979                         nvram_set("wan_ifname", "eth0");
980                 }
981                 break;
982
983         case ROUTER_BELKIN_F5D7230_V3000:
984                 if (nvram_match("vlan1ports", "4 5u"))
985                         nvram_set("vlan1ports", "4 5");
986                 break;
987
988         case ROUTER_DELL_TRUEMOBILE_2300_V2:    // we must fix cfe defaults
989                 // with CR added
990                 nvram_set("vlan0hwname", "et0");
991                 nvram_set("vlan1hwname", "et0");
992                 nvram_set("et0mdcport", "0");
993                 nvram_set("et0phyaddr", "30");
994                 nvram_set("gpio2", "adm_eecs");
995                 nvram_set("gpio3", "adm_eesk");
996                 nvram_set("gpio4", "adm_eedi");
997                 nvram_set("gpio5", "adm_rc");
998                 nvram_unset("gpio6");
999                 break;
1000
1001         case ROUTER_WRT54G_V8:
1002                 nvram_set("reset_gpio", "7");
1003                 break;
1004
1005         case ROUTER_ASUS_WL520G:
1006         case ROUTER_ASUS_WL500G_PRE_V2:
1007         case ROUTER_WRT54G_V81:
1008                 if (nvram_match("vlan1ports", "4 5u"))
1009                         nvram_set("vlan1ports", "4 5");
1010                 break;
1011
1012         case ROUTER_ASUS_WL520GUGC:
1013                 if (nvram_match("vlan1ports", "0 5u"))
1014                         nvram_set("vlan1ports", "0 5");
1015                 if (!nvram_get("Fix_WL520GUGC_clock")) {
1016                         nvram_set("Fix_WL520GUGC_clock", "1");
1017                         need_reboot = 1;
1018                 }
1019                 break;
1020
1021         case ROUTER_NETGEAR_WGR614L:
1022                 if (nvram_match("vlan1ports", "0 5u"))
1023                         nvram_set("vlan1ports", "0 5");
1024                 if (nvram_match("sromrev", "2")
1025                     && nvram_match("boardrev", "0x10")
1026                     && nvram_match("boardtype", "0x48E")) {
1027                         nvram_set("sromrev", "3");      // This is a fix for WGR614L NA - which has a wrong sromrev
1028                         need_reboot = 1;
1029                 }
1030                 break;
1031
1032         case ROUTER_ALLNET01:
1033                 nvram_set("wl0_ifname", "eth1");
1034                 if (nvram_match("vlan1ports", "5u"))    //correct bad parameters
1035                 {
1036                         nvram_set("vlan1ports", "4 5");
1037                         nvram_set("vlan0ports", "0 1 2 3 5*");
1038                 }
1039                 break;
1040
1041         case ROUTER_LINKSYS_WTR54GS:
1042                 eval("gpio", "enable", "3");    // prevent reboot loop on
1043                 // reset
1044                 break;
1045
1046         case ROUTER_WAP54G_V3:
1047                 eval("gpio", "enable", "0");    // reset gpio 0 for reset
1048                 // button
1049                 // nvram_set ("vlan0ports", "1 5*");
1050                 // nvram_set ("vlan1ports", "4 5");
1051                 // if (nvram_match ("wan_to_lan", "yes") && nvram_invmatch
1052                 // ("wan_proto", "disabled")) // = no lan
1053                 // {
1054                 // nvram_set ("vlan0ports", "4 5*");
1055                 // nvram_set ("vlan1ports", "1 5");
1056                 // }
1057                 break;
1058
1059         }
1060 #if 0
1061         /*
1062          * fix il0macaddr to be lanmac+2
1063          */
1064         if (nvram_get("il0macaddr") == NULL)
1065                 need_reboot = 1;
1066
1067         unsigned char mac[20];
1068
1069         if (nvram_match("port_swap", "1"))
1070                 strcpy(mac, nvram_safe_get("et1macaddr"));
1071         else
1072                 strcpy(mac, nvram_safe_get("et0macaddr"));
1073         MAC_ADD(mac);
1074         MAC_ADD(mac);
1075         nvram_set("il0macaddr", mac);
1076 #endif
1077
1078         /*
1079          * set router's basic parameters
1080          */
1081         while (basic_params && basic_params->name) {
1082                 nvram_set(basic_params->name, basic_params->value);
1083                 basic_params++;
1084         }
1085
1086         /*
1087          * ifnames
1088          */
1089         strcpy(wanifname, nvram_safe_get("wan_ifname"));
1090         strcpy(wlifname, nvram_safe_get("wl0_ifname"));
1091
1092         /*
1093          * set wan_ifnames, pppoe_wan_ifname and pppoe_ifname
1094          */
1095         nvram_set("wan_ifname", wanifname);
1096         nvram_set("wan_ifnames", wanifname);
1097         nvram_set("wan_default", wanifname);
1098         nvram_set("pppoe_wan_ifname", wanifname);
1099         nvram_set("pppoe_ifname", wanifname);
1100
1101         /*
1102          * MAC address sdjustments
1103          */
1104         switch (brand) {
1105         case ROUTER_ALLNET01:
1106         case ROUTER_BELKIN_F5D7231_V2000:
1107
1108                 if (!nvram_match("no_sercom", "1")) {
1109                         //fix mac
1110                         unsigned char mac[6];
1111                         FILE *in = fopen("/dev/mtdblock/0", "rb");
1112
1113                         if (in != NULL) //special sercom mac address handling
1114                         {
1115                                 fseek(in, 0x1ffa0, SEEK_SET);
1116                                 fread(mac, 6, 1, in);
1117                                 fclose(in);
1118                                 char macstr[32];
1119
1120                                 sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
1121                                         (int)mac[0] & 0xff, (int)mac[1] & 0xff,
1122                                         (int)mac[2] & 0xff, (int)mac[3] & 0xff,
1123                                         (int)mac[4] & 0xff, (int)mac[5] & 0xff);
1124                                 nvram_set("et0macaddr", macstr);
1125                                 eval("ifconfig", "eth0", "hw", "ether", macstr);
1126                         }
1127                 }
1128                 break;
1129         }
1130
1131         /*
1132          * additional boardflags adjustment
1133          */
1134         switch (brand) {
1135         case ROUTER_BELKIN_F5D7231:
1136                 if (nvram_match("boardflags", "0x388")
1137                     || nvram_match("boardflags", "0x0388")) {
1138                         nvram_set("boardflags", "0x0f58");
1139                         need_reboot = 1;
1140                 }
1141                 break;
1142
1143         case ROUTER_ASKEY_RT220XD:
1144                 if (nvram_match("boardflags", "0x388")
1145                     || nvram_match("boardflags", "0x0388")) {
1146                         nvram_set("boardflags", "0x0208");
1147                         need_reboot = 1;
1148                 }
1149                 break;
1150
1151         case ROUTER_BUFFALO_WLI_TX4_G54HP:
1152                 if (!nvram_match("buffalo_hp", "1")
1153                     && (nvram_match("boardflags", "0x1658")
1154                         || nvram_match("boardflags", "0x2658"))) {
1155                         nvram_set("buffalo_hp", "1");
1156 #ifndef HAVE_BUFFALO            // if HAVE_BUFFALO not used to be FCC/CE
1157                         // valid
1158                         nvram_set("boardflags", "0x3658");      // enable high gain
1159                         // PA
1160                         need_reboot = 1;
1161 #endif
1162                 }
1163                 break;
1164
1165         case ROUTER_BUFFALO_WHRG54S:    // for HP only
1166                 if (!nvram_match("buffalo_hp", "1")
1167                     && nvram_match("boardflags", "0x1758")) {
1168                         nvram_set("buffalo_hp", "1");
1169 #ifndef HAVE_BUFFALO            // if HAVE_BUFFALO not used to be FCC/CE
1170                         // valid
1171                         nvram_set("boardflags", "0x3758");      // enable high gain
1172                         // PA
1173                         need_reboot = 1;
1174 #endif
1175                 }
1176                 break;
1177
1178         case ROUTER_WRTSL54GS:
1179                 if (nvram_match("force_vlan_supp", "enabled")
1180                     && nvram_match("boardflags", "0x0018")) {
1181                         nvram_set("boardflags", "0x0118");      //enable lan vlans
1182                         need_reboot = 1;
1183                 } else if (!nvram_match("force_vlan_supp", "enabled")
1184                            && nvram_match("boardflags", "0x0118")) {
1185                         nvram_set("boardflags", "0x0018");      //disable vlans
1186                         need_reboot = 1;
1187                 }
1188                 break;
1189
1190         case ROUTER_WRT160N:
1191                 if (nvram_match("force_vlan_supp", "enabled")
1192                     && nvram_match("boardflags", "0x0010")) {
1193                         nvram_set("boardflags", "0x0110");      //enable lan vlans
1194                         need_reboot = 1;
1195                 } else if (!nvram_match("force_vlan_supp", "enabled")
1196                            && nvram_match("boardflags", "0x0110")) {
1197                         nvram_set("boardflags", "0x0010");      //disable vlans
1198                         need_reboot = 1;
1199                 }
1200                 break;
1201
1202         case ROUTER_NETGEAR_WNR834BV2:
1203                 if (nvram_match("force_vlan_supp", "enabled")
1204                     && nvram_match("boardflags", "0x10")) {
1205                         nvram_set("boardflags", "0x110");       //enable lan vlans
1206                         need_reboot = 1;
1207                 } else if (!nvram_match("force_vlan_supp", "enabled")
1208                            && nvram_match("boardflags", "0x110")) {
1209                         nvram_set("boardflags", "0x10");        //disable vlans
1210                         need_reboot = 1;
1211                 }
1212                 break;
1213
1214         case ROUTER_NETGEAR_WG602_V4:
1215                 if (nvram_match("boardflags", "0x650")) {
1216                         nvram_set("boardflags", "0x0458");
1217                         need_reboot = 1;
1218                 }
1219                 break;
1220         }
1221
1222         if (need_reboot) {
1223                 nvram_commit();
1224                 cprintf("Need reboot now .....\n");
1225                 sys_reboot();
1226         }
1227
1228         /*
1229          * Modules
1230          */
1231         uname(&name);
1232
1233         snprintf(buf, sizeof(buf), "/lib/modules/%s", name.release);
1234         if (stat("/proc/modules", &tmp_stat) == 0 && stat(buf, &tmp_stat) == 0) {
1235                 char module[80], *modules, *next;
1236
1237                 // modules="wl switch-core";
1238                 nvram_set("portprio_support", "1");
1239
1240                 if (check_vlan_support() && check_hw_type() != BCM5325E_CHIP) {
1241                         switch (brand) {
1242                         case ROUTER_WRT310N:
1243                         case ROUTER_WRT350N:
1244                         case ROUTER_WRT600N:
1245                         case ROUTER_WRT610N:
1246                         case ROUTER_WRT300NV11:
1247                         case ROUTER_BUFFALO_WZRG144NH:
1248                                 nvram_set("portprio_support", "0");
1249                                 modules = "bcm57xxlsys";
1250                                 break;
1251                         case ROUTER_LINKSYS_WRT55AG:
1252                         case ROUTER_MOTOROLA:
1253                         case ROUTER_BUFFALO_WBR2G54S:
1254                         case ROUTER_DELL_TRUEMOBILE_2300_V2:
1255                                 modules =
1256                                     nvram_invmatch("ct_modules",
1257                                                    "") ?
1258                                     nvram_safe_get("ct_modules")
1259                                     : "switch-core switch-adm";
1260                                 break;
1261
1262                         case ROUTER_WRT54G_V8:
1263                         case ROUTER_WRT54G_V81:
1264                         case ROUTER_LINKSYS_WRH54G:
1265                         case ROUTER_ASUS_WL520G:
1266                         case ROUTER_ASUS_WL520GUGC:
1267                                 modules =
1268                                     nvram_invmatch("ct_modules",
1269                                                    "") ?
1270                                     nvram_safe_get("ct_modules")
1271                                     : "switch-core switch-robo";
1272                                 break;
1273
1274                         case ROUTER_WRT54G1X:
1275                         case ROUTER_WRT54G:
1276                                 insmod("switch-core");
1277                                 if (insmod("switch-robo"))
1278                                         insmod("switch-adm");
1279                                 break;
1280
1281                         case ROUTER_RT480W:
1282                         case ROUTER_BUFFALO_WLI2_TX1_G54:
1283                                 modules =
1284                                     nvram_invmatch("ct_modules",
1285                                                    "") ?
1286                                     nvram_safe_get("ct_modules")
1287                                     : "";
1288                                 insmod("switch-core");
1289                                 if (insmod("switch-robo"))
1290                                         insmod("switch-adm");
1291                                 break;
1292
1293                         case ROUTER_WRT54G3G:
1294                                 modules =
1295                                     nvram_invmatch("ct_modules",
1296                                                    "") ?
1297                                     nvram_safe_get("ct_modules")
1298                                     :
1299                                     "switch-core switch-robo pcmcia_core yenta_socket ds serial_cs usbcore usb-ohci usbserial sierra";
1300                                 break;
1301
1302                         default:
1303
1304                                 modules =
1305                                     nvram_invmatch("ct_modules",
1306                                                    "") ?
1307                                     nvram_safe_get("ct_modules")
1308                                     : "switch-core switch-robo";
1309                                 break;
1310                         }
1311                 } else {
1312                         switch (brand) {
1313                         case ROUTER_WRT310N:
1314                         case ROUTER_WRT350N:
1315                         case ROUTER_WRT600N:
1316                         case ROUTER_WRT610N:
1317                         case ROUTER_BUFFALO_WZRG144NH:
1318                                 nvram_set("portprio_support", "0");
1319                                 modules = "bcm57xxlsys";
1320                                 break;
1321                         case ROUTER_LINKSYS_WRT55AG:
1322                                 modules =
1323                                     nvram_invmatch("ct_modules",
1324                                                    "") ?
1325                                     nvram_safe_get("ct_modules")
1326                                     : "switch-core switch-adm";
1327
1328                                 break;
1329                         case ROUTER_ASUS_WL500GD:
1330                         case ROUTER_ASUS_WL550GE:
1331                                 modules =
1332                                     nvram_invmatch("ct_modules",
1333                                                    "") ?
1334                                     nvram_safe_get("ct_modules")
1335                                     : "switch-core switch-robo";
1336                                 break;
1337                         case ROUTER_BUFFALO_WZRRSG54:
1338                                 nvram_set("portprio_support", "0");
1339                                 modules =
1340                                     nvram_invmatch("ct_modules",
1341                                                    "") ?
1342                                     nvram_safe_get("ct_modules")
1343                                     : "";
1344                                 break;
1345                         case ROUTER_WRT54G3G:
1346                                 if (check_vlan_support())
1347                                         modules =
1348                                             nvram_invmatch("ct_modules",
1349                                                            "") ?
1350                                             nvram_safe_get("ct_modules") :
1351                                             "switch-core switch-robo pcmcia_core yenta_socket ds";
1352                                 else {
1353                                         nvram_set("portprio_support", "0");
1354
1355                                         modules =
1356                                             nvram_invmatch("ct_modules",
1357                                                            "") ?
1358                                             nvram_safe_get("ct_modules") :
1359                                             "pcmcia_core yenta_socket ds";
1360                                 }
1361                                 break;
1362
1363                         default:
1364                                 if (check_vlan_support())
1365                                         modules =
1366                                             nvram_invmatch("ct_modules",
1367                                                            "") ?
1368                                             nvram_safe_get("ct_modules") :
1369                                             "switch-core switch-robo";
1370                                 else {
1371                                         nvram_set("portprio_support", "0");
1372                                         modules =
1373                                             nvram_invmatch("ct_modules",
1374                                                            "") ?
1375                                             nvram_safe_get("ct_modules") : "";
1376                                 }
1377                                 break;
1378                         }
1379                 }
1380 //      fprintf( "insmod %s\n", modules );
1381
1382                 foreach(module, modules, next) {
1383 #ifdef HAVE_MACBIND
1384                         if (nvram_match("et0macaddr", MACBRAND))
1385                                 insmod(module);
1386 #else
1387
1388                         fprintf(stderr, "loading %s\n", module);
1389                         insmod(module);
1390                         cprintf("done\n");
1391 #endif
1392                 }
1393
1394                 if (check_hw_type() == BCM4702_CHIP)
1395                         insmod("diag");
1396
1397                 loadWlModule();
1398
1399         }
1400         /*
1401          * Set a sane date
1402          */
1403         stime(&tm);
1404
1405         led_control(LED_POWER, LED_ON);
1406         led_control(LED_SES, LED_OFF);
1407         led_control(LED_BRIDGE, LED_OFF);
1408         led_control(LED_WLAN, LED_OFF);
1409         led_control(LED_CONNECTED, LED_OFF);
1410
1411         if (brand == ROUTER_WRT54G3G) {
1412                 eval("cardmgr");
1413         }
1414
1415         cprintf("done\n");
1416         return;
1417
1418 }
1419
1420 static int check_nv(char *name, char *value)
1421 {
1422         int ret = 0;
1423
1424         if (nvram_match("manual_boot_nv", "1"))
1425                 return 0;
1426
1427         if (!nvram_get(name)) {
1428                 cprintf("ERR: Cann't find %s !.......................\n", name);
1429                 nvram_set(name, value);
1430                 ret++;
1431         } else if (nvram_invmatch(name, value)) {
1432                 cprintf("ERR: The %s is %s, not %s !.................\n", name,
1433                         nvram_safe_get(name), value);
1434                 nvram_set(name, value);
1435                 ret++;
1436         }
1437
1438         return ret;
1439 }
1440
1441 int check_cfe_nv(void)
1442 {
1443         int ret = 0;
1444
1445         switch (getRouterBrand()) {
1446         case ROUTER_BUFFALO_WZRRSG54:
1447                 ret += check_nv("lan_hwnames", "et0 wl0");
1448                 ret += check_nv("wan_hwname", "et1");
1449                 ret += check_nv("vlans", "0");
1450                 break;
1451         case ROUTER_BUFFALO_WBR2G54S:
1452                 ret += check_nv("aa0", "3");
1453
1454                 ret += check_nv("pa0itssit", "62");
1455                 ret += check_nv("pa0b0", "0x1136");
1456                 ret += check_nv("pa0b1", "0xfb93");
1457                 ret += check_nv("pa0b2", "0xfea5");
1458                 ret += check_nv("wl0gpio2", "0");
1459                 ret += check_nv("wl0gpio3", "0");
1460                 ret += check_nv("cctl", "0");
1461                 ret += check_nv("ccode", "0");
1462                 break;
1463 #ifndef HAVE_BUFFALO
1464
1465         case ROUTER_WRT54G:
1466         case ROUTER_WRT54G_V8:
1467         case ROUTER_WRT54G_V81:
1468                 ret += check_nv("aa0", "3");
1469                 /*
1470                  * if (check_hw_type () == BCM5352E_CHIP || check_hw_type () ==
1471                  * BCM5354G_CHIP) ret += check_nv ("ag0", "0x02"); else ret +=
1472                  * check_nv ("ag0", "255");
1473                  */
1474                 if (check_hw_type() == BCM5325E_CHIP) {
1475                         /*
1476                          * Lower the DDR ram drive strength , the value will be
1477                          * stable for all boards Latency 3 is more stable for all ddr
1478                          * 20050420 by honor
1479                          */
1480
1481                         ret += check_nv("sdram_init", "0x010b");
1482                         ret += check_nv("sdram_config", "0x0062");
1483
1484                         if (nvram_match("clkfreq", "200")
1485                             && nvram_match("overclocking", "200")) {
1486                                 ret += check_nv("clkfreq", "216");
1487                                 nvram_set("overclocking", "216");
1488                         }
1489
1490                         if (ret) {
1491                                 nvram_set("sdram_ncdl", "0x0");
1492
1493                         }
1494                         ret += check_nv("pa0itssit", "62");
1495                         ret += check_nv("pa0b0", "0x15eb");
1496                         ret += check_nv("pa0b1", "0xfa82");
1497                         ret += check_nv("pa0b2", "0xfe66");
1498                 } else if (check_hw_type() == BCM5354G_CHIP) {
1499                         ret += check_nv("pa0itssit", "62");
1500                         ret += check_nv("pa0b0", "0x1326");
1501                         ret += check_nv("pa0b1", "0xFB51");
1502                         ret += check_nv("pa0b2", "0xFE87");
1503                         ret += check_nv("reset_gpio", "7");
1504                 } else if (check_hw_type() == BCM4705_BCM5397_EWC_CHIP) {
1505                         // nothing to do
1506                 } else if (check_hw_type() == BCM4704_BCM5325F_CHIP) {
1507                         // nothing to do
1508                 } else {
1509                         ret += check_nv("pa0itssit", "62");
1510                         ret += check_nv("pa0b0", "0x170c");
1511                         ret += check_nv("pa0b1", "0xfa24");
1512                         ret += check_nv("pa0b2", "0xfe70");
1513                 }
1514
1515                 // ret += check_nv("gpio2", "adm_eecs");
1516                 // ret += check_nv("gpio3", "adm_eesk");
1517                 // ret += check_nv("gpio5", "adm_eedi");
1518                 // ret += check_nv("gpio6", "adm_rc");
1519
1520                 ret += check_nv("wl0gpio2", "0");
1521                 ret += check_nv("wl0gpio3", "0");
1522
1523                 ret += check_nv("cctl", "0");
1524                 ret += check_nv("ccode", "0");
1525                 break;
1526 #endif
1527         }
1528         if (ret) {
1529                 cprintf
1530                     ("Some error found, we want to reboot!.....................\n");
1531                 nvram_commit();
1532                 sys_reboot();
1533         }
1534
1535         return ret;
1536 }
1537
1538 int check_pmon_nv(void)
1539 {
1540         return 0;
1541 }
1542
1543 void start_overclocking(void)
1544 {
1545 #ifdef HAVE_OVERCLOCKING
1546         cprintf("Overclocking started\n");
1547
1548         int rev = cpu_plltype();
1549
1550         if (rev == 0)
1551                 return;         // unsupported
1552
1553         char *ov = nvram_get("overclocking");
1554
1555         if (ov == NULL)
1556                 return;
1557         int clk = atoi(ov);
1558
1559         if (nvram_get("clkfreq") == NULL)
1560                 return;         // unsupported
1561
1562         char *pclk = nvram_safe_get("clkfreq");
1563         char dup[64];
1564
1565         strcpy(dup, pclk);
1566         int i;
1567
1568         for (i = 0; i < strlen(dup); i++)
1569                 if (dup[i] == ',')
1570                         dup[i] = 0;
1571         int cclk = atoi(dup);
1572
1573         if ((cclk < 150 && rev == 3) || (cclk < 192 && rev == 4)
1574             || (cclk < 183 && rev == 7)) {
1575                 cprintf("clkfreq is %d (%s), this is unsupported\n", cclk, dup);
1576                 return;         // unsupported
1577         }
1578
1579         if (clk == cclk) {
1580                 cprintf("clkfreq identical with new setting\n");
1581                 return;         // clock already set
1582         }
1583
1584         int set = 1;
1585         int clk2 = 0;
1586         char clkfr[16];
1587
1588         switch (clk) {
1589         case 150:
1590                 clk2 = 75;
1591                 // nvram_set ("clkfreq", "150,75");
1592                 break;
1593         case 183:
1594                 clk2 = 92;
1595                 // nvram_set ("clkfreq", "183,92");
1596                 break;
1597         case 187:
1598                 clk2 = 94;
1599                 // nvram_set ("clkfreq", "187,94");
1600                 break;
1601         case 192:
1602                 clk2 = 96;
1603                 // nvram_set ("clkfreq", "192,96");
1604                 break;
1605         case 198:
1606                 clk2 = 98;
1607                 // nvram_set ("clkfreq", "198,98");
1608                 break;
1609         case 200:
1610                 clk2 = 100;
1611                 // nvram_set ("clkfreq", "200,100");
1612                 break;
1613         case 216:
1614                 clk2 = 108;
1615                 // nvram_set ("clkfreq", "216,108");
1616                 break;
1617         case 225:
1618                 clk2 = 113;
1619                 // nvram_set ("clkfreq", "225,113");
1620                 break;
1621         case 228:
1622                 clk2 = 114;
1623                 // nvram_set ("clkfreq", "228,114");
1624                 break;
1625         case 233:
1626                 clk2 = 116;
1627                 // nvram_set ("clkfreq", "233,116");
1628                 break;
1629         case 237:
1630                 clk2 = 119;
1631                 // nvram_set ("clkfreq", "237,119");
1632                 break;
1633         case 240:
1634                 clk2 = 120;
1635                 // nvram_set ("clkfreq", "240,120");
1636                 break;
1637         case 250:
1638                 clk2 = 125;
1639                 // nvram_set ("clkfreq", "250,125");
1640                 break;
1641         case 252:
1642                 clk2 = 126;
1643                 // nvram_set ("clkfreq", "252,126");
1644                 break;
1645         case 264:
1646                 clk2 = 132;
1647                 // nvram_set ("clkfreq", "264,132");
1648                 break;
1649         case 280:
1650                 clk2 = 120;
1651                 // nvram_set ("clkfreq", "280,120");
1652                 break;
1653         case 300:
1654                 clk2 = 120;
1655                 // nvram_set ("clkfreq", "300,120");
1656                 break;
1657         default:
1658                 set = 0;
1659                 break;
1660         }
1661
1662         if (set) {
1663                 cprintf
1664                     ("clock frequency adjusted from %d to %d, reboot needed\n",
1665                      cclk, clk);
1666                 sprintf(clkfr, "%d,%d", clk, clk2);
1667                 nvram_set("clkfreq", clkfr);
1668                 nvram_commit();
1669                 cprintf("Overclocking done, rebooting...\n");
1670                 sys_reboot();
1671         }
1672 #endif
1673 }
1674
1675 char *enable_dtag_vlan(int enable)
1676 {
1677         int donothing = 0;
1678
1679         nvram_set("fromvdsl", "1");
1680         if (nvram_match("vdsl_state", "1") && enable)
1681                 donothing = 1;
1682         if ((nvram_match("vdsl_state", "0")
1683              || nvram_match("vdsl_state", "")) && !enable)
1684                 donothing = 1;
1685         if (enable)
1686                 nvram_set("vdsl_state", "1");
1687         else
1688                 nvram_set("vdsl_state", "0");
1689
1690         char *vlan7ports = NULL;
1691
1692         vlan7ports = "4t 5";
1693         if (nvram_match("vlan1ports", "4 5")) {
1694                 vlan7ports = "4t 5";
1695         }
1696         if (nvram_match("vlan1ports", "0 5")) {
1697                 vlan7ports = "0t 5";
1698         }
1699         if (nvram_match("vlan1ports", "1 5")) {
1700                 vlan7ports = "1t 5";
1701         }
1702         if (nvram_match("vlan2ports", "0 8") || nvram_match("vlan2ports", "0 8*") || nvram_match("vlan2ports", "0 8t") || nvram_match("vlan1ports", "4 8"))     // special
1703                 //
1704                 // condition
1705                 // for
1706                 // Broadcom
1707                 // Gigabit
1708                 // Phy
1709                 // routers
1710                 //
1711         {
1712 #ifdef HAVE_MADWIFI
1713                 char *eth = "eth0";
1714 #else
1715                 char *eth = "eth1";
1716 #endif
1717                 vlan7ports = "0t 8";
1718                 int vlanswap = 0;
1719
1720                 if (nvram_match("vlan1ports", "4 8")) {
1721                         vlanswap = 1;
1722                         vlan7ports = "4t 8";
1723                 }
1724                 char *save_ports2 = nvram_safe_get("vlan2ports");
1725                 char *save_ports1 = nvram_safe_get("vlan1ports");
1726 #ifndef HAVE_MADWIFI
1727                 if (getRouterBrand() == ROUTER_WRT600N
1728                     || getRouterBrand() == ROUTER_WRT610N)
1729                         eth = "eth2";
1730 #endif
1731                 if (donothing) {
1732                         nvram_set("fromvdsl", "0");
1733                         return eth;
1734                 }
1735                 if (enable) {
1736                         if (vlanswap)
1737                                 nvram_set("vlan1ports", "");
1738                         else
1739                                 nvram_set("vlan2ports", "");
1740                         nvram_set("vlan7ports", vlan7ports);
1741                         if (nvram_match("dtag_vlan8", "1")) {
1742                                 nvram_set("vlan8ports", vlan7ports);
1743                         }
1744                 }
1745                 stop_lan();
1746                 eval("ifconfig", eth, "down");
1747                 rmmod("bcm57xxlsys");
1748                 insmod("bcm57xxlsys");
1749                 eval("ifconfig", eth, "up");
1750                 start_config_vlan();
1751                 start_lan();
1752                 if (enable) {
1753                         nvram_set("vlan1ports", save_ports1);
1754                         nvram_set("vlan2ports", save_ports2);
1755                         nvram_set("vlan7ports", "");
1756                         nvram_set("vlan8ports", "");
1757                 }
1758                 nvram_set("fromvdsl", "0");
1759                 return eth;
1760         }
1761
1762         if (nvram_match("switch_type", "BCM5325"))      // special condition
1763                 // for Broadcom
1764                 // Gigabit Phy
1765                 // routers
1766         {
1767 #ifdef HAVE_MADWIFI
1768                 char *eth = "eth0";
1769 #else
1770                 char *eth = "eth1";
1771 #endif
1772                 vlan7ports = "0t 5";
1773                 char *save_ports2 = nvram_safe_get("vlan1ports");
1774                 char *save_ports1 = nvram_safe_get("vlan0ports");
1775
1776                 if (donothing) {
1777                         nvram_set("fromvdsl", "0");
1778                         return eth;
1779                 }
1780                 if (enable) {
1781                         nvram_set("vlan1ports", "");
1782                         nvram_set("vlan7ports", vlan7ports);
1783                         if (nvram_match("dtag_vlan8", "1")) {
1784                                 nvram_set("vlan8ports", vlan7ports);
1785                         }
1786                 }
1787                 stop_lan();
1788                 eval("ifconfig", eth, "down");
1789                 rmmod("bcm57xxlsys");
1790                 insmod("bcm57xxlsys");
1791                 eval("ifconfig", eth, "up");
1792                 start_config_vlan();
1793                 start_lan();
1794                 if (enable) {
1795                         nvram_set("vlan0ports", save_ports1);
1796                         nvram_set("vlan1ports", save_ports2);
1797                         nvram_set("vlan7ports", "");
1798                         nvram_set("vlan8ports", "");
1799                 }
1800                 nvram_set("fromvdsl", "0");
1801                 return eth;
1802         }
1803
1804         char *eth = "eth0";
1805
1806         FILE *in = fopen("/proc/switch/eth1/reset", "rb");      // this
1807
1808         // condition
1809         // fails
1810         // almost.
1811         // just one
1812         // router
1813         // (DLINK
1814         // DIR-330)
1815         // requires
1816         // it
1817         if (in) {
1818                 eth = "eth1";
1819                 fclose(in);
1820         } else {
1821                 FILE *in = fopen("/proc/switch/eth2/reset", "rb");      // this
1822
1823                 // condition
1824                 // fails
1825                 // almost.
1826                 // just one
1827                 // router
1828                 // (DLINK
1829                 // DIR-330)
1830                 // requires
1831                 // it
1832                 if (in) {
1833                         eth = "eth2";
1834                         fclose(in);
1835                 } else
1836                         eth = "eth0";
1837         }
1838
1839         if (!donothing) {
1840                 sysprintf("echo 1 > /proc/switch/%s/reset", eth);
1841                 if (enable) {
1842                         fprintf(stderr, "enable vlan port mapping %s/%s\n",
1843                                 nvram_safe_get("vlan0ports"), vlan7ports);
1844                         if (!nvram_match("dtag_vlan8", "1")) {
1845                                 sysprintf
1846                                     ("echo \"%s\" > /proc/switch/%s/vlan/0/ports",
1847                                      nvram_safe_get("vlan0ports"), eth);
1848                                 start_setup_vlans();
1849                                 sysprintf
1850                                     ("echo \"%s\" > /proc/switch/%s/vlan/1/ports",
1851                                      "", eth);
1852                                 sysprintf
1853                                     ("echo \"%s\" > /proc/switch/%s/vlan/7/ports",
1854                                      vlan7ports, eth);
1855                         } else {
1856                                 sysprintf
1857                                     ("echo \"%s\" > /proc/switch/%s/vlan/0/ports",
1858                                      nvram_safe_get("vlan0ports"), eth);
1859                                 start_setup_vlans();
1860                                 sysprintf
1861                                     ("echo \"%s\" > /proc/switch/%s/vlan/1/ports",
1862                                      "", eth);
1863                                 sysprintf
1864                                     ("echo \"%s\" > /proc/switch/%s/vlan/7/ports",
1865                                      vlan7ports, eth);
1866                                 sysprintf
1867                                     ("echo \"%s\" > /proc/switch/%s/vlan/8/ports",
1868                                      vlan7ports, eth);
1869                         }
1870                 } else {
1871                         fprintf(stderr, "disable vlan port mapping %s/%s\n",
1872                                 nvram_safe_get("vlan0ports"),
1873                                 nvram_safe_get("vlan1ports"));
1874                         sysprintf("echo \"%s\" > /proc/switch/%s/vlan/8/ports",
1875                                   "", eth);
1876                         sysprintf("echo \"%s\" > /proc/switch/%s/vlan/7/ports",
1877                                   "", eth);
1878                         sysprintf("echo \"%s\" > /proc/switch/%s/vlan/0/ports",
1879                                   nvram_safe_get("vlan0ports"), eth);
1880                         sysprintf("echo \"%s\" > /proc/switch/%s/vlan/1/ports",
1881                                   nvram_safe_get("vlan1ports"), eth);
1882                         start_setup_vlans();
1883                 }
1884         }
1885         nvram_set("fromvdsl", "0");
1886         return eth;
1887 }
1888
1889 void start_dtag(void)
1890 {
1891         enable_dtag_vlan(1);
1892 }
Note: See TracBrowser for help on using the browser.