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

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

wrt610n: enable this back, makes no harm...

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 (startswith(nvram_safe_get("pci/1/1/macaddr"), "00:90:4C")
923                     || startswith(nvram_safe_get("pci/1/2/macaddr"),
924                                   "00:90:4C")) {
925                         unsigned char mac[20];
926                         strcpy(mac, nvram_safe_get("et0macaddr"));
927                         MAC_ADD(mac);
928                         MAC_ADD(mac);
929                         nvram_set("pci/1/1/macaddr", mac);
930                         MAC_ADD(mac);
931                         nvram_set("pci/1/2/macaddr", mac);
932                         need_reboot = 1;
933                 }
934                 break;
935
936         case ROUTER_WRT300NV11:
937         case ROUTER_BUFFALO_WZRG144NH:
938                 nvram_set("wan_ifname", "vlan1");
939                 break;
940
941         case ROUTER_ASUS_WL500G_PRE:
942                 nvram_set("sdram_init", "0x0009");
943                 // nvram_set ("sdram_ncdl", "0x208");
944                 nvram_set("lan_ifnames", "vlan0 eth2");
945                 nvram_set("wl0_ifname", "eth2");
946                 nvram_set("wan_ifname", "vlan1");       // fix for Asus WL500gPremium
947                 //
948                 // WAN problem.
949                 if (nvram_match("vlan1ports", "0 5u"))
950                         nvram_set("vlan1ports", "0 5");
951                 break;
952
953         case ROUTER_ASUS_WL500GD:
954         case ROUTER_ASUS_WL550GE:
955                 nvram_set("wl0_ifname", "eth1");
956                 break;
957
958         case ROUTER_BUFFALO_WLA2G54C:
959         case ROUTER_WAP54G_V2:
960         case ROUTER_VIEWSONIC_WAPBR_100:
961         case ROUTER_USR_5430:
962         case ROUTER_BUFFALO_WLI_TX4_G54HP:
963         case ROUTER_BELKIN_F5D7230_V2000:
964         case ROUTER_NETGEAR_WG602_V3:
965         case ROUTER_NETGEAR_WG602_V4:
966         case ROUTER_ASUS_330GE:
967                 nvram_set("lan_ifnames", "eth0 eth1");
968                 nvram_set("wl0_ifname", "eth1");
969                 nvram_set("wan_ifname", "eth2");        // map WAN port to
970                 // nonexistant interface
971                 if (nvram_match("wan_to_lan", "yes") && nvram_invmatch("wan_proto", "disabled"))        // =
972                         //
973                         // no
974                         // lan
975                 {
976                         nvram_set("lan_ifnames", "eth1");
977                         nvram_set("wan_ifname", "eth0");
978                 }
979                 break;
980
981         case ROUTER_BELKIN_F5D7230_V3000:
982                 if (nvram_match("vlan1ports", "4 5u"))
983                         nvram_set("vlan1ports", "4 5");
984                 break;
985
986         case ROUTER_DELL_TRUEMOBILE_2300_V2:    // we must fix cfe defaults
987                 // with CR added
988                 nvram_set("vlan0hwname", "et0");
989                 nvram_set("vlan1hwname", "et0");
990                 nvram_set("et0mdcport", "0");
991                 nvram_set("et0phyaddr", "30");
992                 nvram_set("gpio2", "adm_eecs");
993                 nvram_set("gpio3", "adm_eesk");
994                 nvram_set("gpio4", "adm_eedi");
995                 nvram_set("gpio5", "adm_rc");
996                 nvram_unset("gpio6");
997                 break;
998
999         case ROUTER_WRT54G_V8:
1000                 nvram_set("reset_gpio", "7");
1001                 break;
1002
1003         case ROUTER_ASUS_WL520G:
1004         case ROUTER_ASUS_WL500G_PRE_V2:
1005         case ROUTER_WRT54G_V81:
1006                 if (nvram_match("vlan1ports", "4 5u"))
1007                         nvram_set("vlan1ports", "4 5");
1008                 break;
1009
1010         case ROUTER_ASUS_WL520GUGC:
1011                 if (nvram_match("vlan1ports", "0 5u"))
1012                         nvram_set("vlan1ports", "0 5");
1013                 if (!nvram_get("Fix_WL520GUGC_clock")) {
1014                         nvram_set("Fix_WL520GUGC_clock", "1");
1015                         need_reboot = 1;
1016                 }
1017                 break;
1018
1019         case ROUTER_NETGEAR_WGR614L:
1020                 if (nvram_match("vlan1ports", "0 5u"))
1021                         nvram_set("vlan1ports", "0 5");
1022                 if (nvram_match("sromrev", "2")
1023                     && nvram_match("boardrev", "0x10")
1024                     && nvram_match("boardtype", "0x48E")) {
1025                         nvram_set("sromrev", "3");      // This is a fix for WGR614L NA - which has a wrong sromrev
1026                         need_reboot = 1;
1027                 }
1028                 break;
1029
1030         case ROUTER_ALLNET01:
1031                 nvram_set("wl0_ifname", "eth1");
1032                 if (nvram_match("vlan1ports", "5u"))    //correct bad parameters
1033                 {
1034                         nvram_set("vlan1ports", "4 5");
1035                         nvram_set("vlan0ports", "0 1 2 3 5*");
1036                 }
1037                 break;
1038
1039         case ROUTER_LINKSYS_WTR54GS:
1040                 eval("gpio", "enable", "3");    // prevent reboot loop on
1041                 // reset
1042                 break;
1043
1044         case ROUTER_WAP54G_V3:
1045                 eval("gpio", "enable", "0");    // reset gpio 0 for reset
1046                 // button
1047                 // nvram_set ("vlan0ports", "1 5*");
1048                 // nvram_set ("vlan1ports", "4 5");
1049                 // if (nvram_match ("wan_to_lan", "yes") && nvram_invmatch
1050                 // ("wan_proto", "disabled")) // = no lan
1051                 // {
1052                 // nvram_set ("vlan0ports", "4 5*");
1053                 // nvram_set ("vlan1ports", "1 5");
1054                 // }
1055                 break;
1056
1057         }
1058 #if 0
1059         /*
1060          * fix il0macaddr to be lanmac+2
1061          */
1062         if (nvram_get("il0macaddr") == NULL)
1063                 need_reboot = 1;
1064
1065         unsigned char mac[20];
1066
1067         if (nvram_match("port_swap", "1"))
1068                 strcpy(mac, nvram_safe_get("et1macaddr"));
1069         else
1070                 strcpy(mac, nvram_safe_get("et0macaddr"));
1071         MAC_ADD(mac);
1072         MAC_ADD(mac);
1073         nvram_set("il0macaddr", mac);
1074 #endif
1075
1076         /*
1077          * set router's basic parameters
1078          */
1079         while (basic_params && basic_params->name) {
1080                 nvram_set(basic_params->name, basic_params->value);
1081                 basic_params++;
1082         }
1083
1084         /*
1085          * ifnames
1086          */
1087         strcpy(wanifname, nvram_safe_get("wan_ifname"));
1088         strcpy(wlifname, nvram_safe_get("wl0_ifname"));
1089
1090         /*
1091          * set wan_ifnames, pppoe_wan_ifname and pppoe_ifname
1092          */
1093         nvram_set("wan_ifname", wanifname);
1094         nvram_set("wan_ifnames", wanifname);
1095         nvram_set("wan_default", wanifname);
1096         nvram_set("pppoe_wan_ifname", wanifname);
1097         nvram_set("pppoe_ifname", wanifname);
1098
1099         /*
1100          * MAC address sdjustments
1101          */
1102         switch (brand) {
1103         case ROUTER_ALLNET01:
1104         case ROUTER_BELKIN_F5D7231_V2000:
1105
1106                 if (!nvram_match("no_sercom", "1")) {
1107                         //fix mac
1108                         unsigned char mac[6];
1109                         FILE *in = fopen("/dev/mtdblock/0", "rb");
1110
1111                         if (in != NULL) //special sercom mac address handling
1112                         {
1113                                 fseek(in, 0x1ffa0, SEEK_SET);
1114                                 fread(mac, 6, 1, in);
1115                                 fclose(in);
1116                                 char macstr[32];
1117
1118                                 sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X",
1119                                         (int)mac[0] & 0xff, (int)mac[1] & 0xff,
1120                                         (int)mac[2] & 0xff, (int)mac[3] & 0xff,
1121                                         (int)mac[4] & 0xff, (int)mac[5] & 0xff);
1122                                 nvram_set("et0macaddr", macstr);
1123                                 eval("ifconfig", "eth0", "hw", "ether", macstr);
1124                         }
1125                 }
1126                 break;
1127         }
1128
1129         /*
1130          * additional boardflags adjustment
1131          */
1132         switch (brand) {
1133         case ROUTER_BELKIN_F5D7231:
1134                 if (nvram_match("boardflags", "0x388")
1135                     || nvram_match("boardflags", "0x0388")) {
1136                         nvram_set("boardflags", "0x0f58");
1137                         need_reboot = 1;
1138                 }
1139                 break;
1140
1141         case ROUTER_ASKEY_RT220XD:
1142                 if (nvram_match("boardflags", "0x388")
1143                     || nvram_match("boardflags", "0x0388")) {
1144                         nvram_set("boardflags", "0x0208");
1145                         need_reboot = 1;
1146                 }
1147                 break;
1148
1149         case ROUTER_BUFFALO_WLI_TX4_G54HP:
1150                 if (!nvram_match("buffalo_hp", "1")
1151                     && (nvram_match("boardflags", "0x1658")
1152                         || nvram_match("boardflags", "0x2658"))) {
1153                         nvram_set("buffalo_hp", "1");
1154 #ifndef HAVE_BUFFALO            // if HAVE_BUFFALO not used to be FCC/CE
1155                         // valid
1156                         nvram_set("boardflags", "0x3658");      // enable high gain
1157                         // PA
1158                         need_reboot = 1;
1159 #endif
1160                 }
1161                 break;
1162
1163         case ROUTER_BUFFALO_WHRG54S:    // for HP only
1164                 if (!nvram_match("buffalo_hp", "1")
1165                     && nvram_match("boardflags", "0x1758")) {
1166                         nvram_set("buffalo_hp", "1");
1167 #ifndef HAVE_BUFFALO            // if HAVE_BUFFALO not used to be FCC/CE
1168                         // valid
1169                         nvram_set("boardflags", "0x3758");      // enable high gain
1170                         // PA
1171                         need_reboot = 1;
1172 #endif
1173                 }
1174                 break;
1175
1176         case ROUTER_WRTSL54GS:
1177                 if (nvram_match("force_vlan_supp", "enabled")
1178                     && nvram_match("boardflags", "0x0018")) {
1179                         nvram_set("boardflags", "0x0118");      //enable lan vlans
1180                         need_reboot = 1;
1181                 } else if (!nvram_match("force_vlan_supp", "enabled")
1182                            && nvram_match("boardflags", "0x0118")) {
1183                         nvram_set("boardflags", "0x0018");      //disable vlans
1184                         need_reboot = 1;
1185                 }
1186                 break;
1187
1188         case ROUTER_WRT160N:
1189                 if (nvram_match("force_vlan_supp", "enabled")
1190                     && nvram_match("boardflags", "0x0010")) {
1191                         nvram_set("boardflags", "0x0110");      //enable lan vlans
1192                         need_reboot = 1;
1193                 } else if (!nvram_match("force_vlan_supp", "enabled")
1194                            && nvram_match("boardflags", "0x0110")) {
1195                         nvram_set("boardflags", "0x0010");      //disable vlans
1196                         need_reboot = 1;
1197                 }
1198                 break;
1199
1200         case ROUTER_NETGEAR_WNR834BV2:
1201                 if (nvram_match("force_vlan_supp", "enabled")
1202                     && nvram_match("boardflags", "0x10")) {
1203                         nvram_set("boardflags", "0x110");       //enable lan vlans
1204                         need_reboot = 1;
1205                 } else if (!nvram_match("force_vlan_supp", "enabled")
1206                            && nvram_match("boardflags", "0x110")) {
1207                         nvram_set("boardflags", "0x10");        //disable vlans
1208                         need_reboot = 1;
1209                 }
1210                 break;
1211
1212         case ROUTER_NETGEAR_WG602_V4:
1213                 if (nvram_match("boardflags", "0x650")) {
1214                         nvram_set("boardflags", "0x0458");
1215                         need_reboot = 1;
1216                 }
1217                 break;
1218         }
1219
1220         if (need_reboot) {
1221                 nvram_commit();
1222                 cprintf("Need reboot now .....\n");
1223                 sys_reboot();
1224         }
1225
1226         /*
1227          * Modules
1228          */
1229         uname(&name);
1230
1231         snprintf(buf, sizeof(buf), "/lib/modules/%s", name.release);
1232         if (stat("/proc/modules", &tmp_stat) == 0 && stat(buf, &tmp_stat) == 0) {
1233                 char module[80], *modules, *next;
1234
1235                 // modules="wl switch-core";
1236                 nvram_set("portprio_support", "1");
1237
1238                 if (check_vlan_support() && check_hw_type() != BCM5325E_CHIP) {
1239                         switch (brand) {
1240                         case ROUTER_WRT310N:
1241                         case ROUTER_WRT350N:
1242                         case ROUTER_WRT600N:
1243                         case ROUTER_WRT610N:
1244                         case ROUTER_WRT300NV11:
1245                         case ROUTER_BUFFALO_WZRG144NH:
1246                                 nvram_set("portprio_support", "0");
1247                                 modules = "bcm57xxlsys";
1248                                 break;
1249                         case ROUTER_LINKSYS_WRT55AG:
1250                         case ROUTER_MOTOROLA:
1251                         case ROUTER_BUFFALO_WBR2G54S:
1252                         case ROUTER_DELL_TRUEMOBILE_2300_V2:
1253                                 modules =
1254                                     nvram_invmatch("ct_modules",
1255                                                    "") ?
1256                                     nvram_safe_get("ct_modules")
1257                                     : "switch-core switch-adm";
1258                                 break;
1259
1260                         case ROUTER_WRT54G_V8:
1261                         case ROUTER_WRT54G_V81:
1262                         case ROUTER_LINKSYS_WRH54G:
1263                         case ROUTER_ASUS_WL520G:
1264                         case ROUTER_ASUS_WL520GUGC:
1265                                 modules =
1266                                     nvram_invmatch("ct_modules",
1267                                                    "") ?
1268                                     nvram_safe_get("ct_modules")
1269                                     : "switch-core switch-robo";
1270                                 break;
1271
1272                         case ROUTER_WRT54G1X:
1273                         case ROUTER_WRT54G:
1274                                 insmod("switch-core");
1275                                 if (insmod("switch-robo"))
1276                                         insmod("switch-adm");
1277                                 break;
1278
1279                         case ROUTER_RT480W:
1280                         case ROUTER_BUFFALO_WLI2_TX1_G54:
1281                                 modules =
1282                                     nvram_invmatch("ct_modules",
1283                                                    "") ?
1284                                     nvram_safe_get("ct_modules")
1285                                     : "";
1286                                 insmod("switch-core");
1287                                 if (insmod("switch-robo"))
1288                                         insmod("switch-adm");
1289                                 break;
1290
1291                         case ROUTER_WRT54G3G:
1292                                 modules =
1293                                     nvram_invmatch("ct_modules",
1294                                                    "") ?
1295                                     nvram_safe_get("ct_modules")
1296                                     :
1297                                     "switch-core switch-robo pcmcia_core yenta_socket ds serial_cs usbcore usb-ohci usbserial sierra";
1298                                 break;
1299
1300                         default:
1301
1302                                 modules =
1303                                     nvram_invmatch("ct_modules",
1304                                                    "") ?
1305                                     nvram_safe_get("ct_modules")
1306                                     : "switch-core switch-robo";
1307                                 break;
1308                         }
1309                 } else {
1310                         switch (brand) {
1311                         case ROUTER_WRT310N:
1312                         case ROUTER_WRT350N:
1313                         case ROUTER_WRT600N:
1314                         case ROUTER_WRT610N:
1315                         case ROUTER_BUFFALO_WZRG144NH:
1316                                 nvram_set("portprio_support", "0");
1317                                 modules = "bcm57xxlsys";
1318                                 break;
1319                         case ROUTER_LINKSYS_WRT55AG:
1320                                 modules =
1321                                     nvram_invmatch("ct_modules",
1322                                                    "") ?
1323                                     nvram_safe_get("ct_modules")
1324                                     : "switch-core switch-adm";
1325
1326                                 break;
1327                         case ROUTER_ASUS_WL500GD:
1328                         case ROUTER_ASUS_WL550GE:
1329                                 modules =
1330                                     nvram_invmatch("ct_modules",
1331                                                    "") ?
1332                                     nvram_safe_get("ct_modules")
1333                                     : "switch-core switch-robo";
1334                                 break;
1335                         case ROUTER_BUFFALO_WZRRSG54:
1336                                 nvram_set("portprio_support", "0");
1337                                 modules =
1338                                     nvram_invmatch("ct_modules",
1339                                                    "") ?
1340                                     nvram_safe_get("ct_modules")
1341                                     : "";
1342                                 break;
1343                         case ROUTER_WRT54G3G:
1344                                 if (check_vlan_support())
1345                                         modules =
1346                                             nvram_invmatch("ct_modules",
1347                                                            "") ?
1348                                             nvram_safe_get("ct_modules") :
1349                                             "switch-core switch-robo pcmcia_core yenta_socket ds";
1350                                 else {
1351                                         nvram_set("portprio_support", "0");
1352
1353                                         modules =
1354                                             nvram_invmatch("ct_modules",
1355                                                            "") ?
1356                                             nvram_safe_get("ct_modules") :
1357                                             "pcmcia_core yenta_socket ds";
1358                                 }
1359                                 break;
1360
1361                         default:
1362                                 if (check_vlan_support())
1363                                         modules =
1364                                             nvram_invmatch("ct_modules",
1365                                                            "") ?
1366                                             nvram_safe_get("ct_modules") :
1367                                             "switch-core switch-robo";
1368                                 else {
1369                                         nvram_set("portprio_support", "0");
1370                                         modules =
1371                                             nvram_invmatch("ct_modules",
1372                                                            "") ?
1373                                             nvram_safe_get("ct_modules") : "";
1374                                 }
1375                                 break;
1376                         }
1377                 }
1378 //      fprintf( "insmod %s\n", modules );
1379
1380                 foreach(module, modules, next) {
1381 #ifdef HAVE_MACBIND
1382                         if (nvram_match("et0macaddr", MACBRAND))
1383                                 insmod(module);
1384 #else
1385
1386                         fprintf(stderr, "loading %s\n", module);
1387                         insmod(module);
1388                         cprintf("done\n");
1389 #endif
1390                 }
1391
1392                 if (check_hw_type() == BCM4702_CHIP)
1393                         insmod("diag");
1394
1395                 loadWlModule();
1396
1397         }
1398         /*
1399          * Set a sane date
1400          */
1401         stime(&tm);
1402
1403         led_control(LED_POWER, LED_ON);
1404         led_control(LED_SES, LED_OFF);
1405         led_control(LED_BRIDGE, LED_OFF);
1406         led_control(LED_WLAN, LED_OFF);
1407         led_control(LED_CONNECTED, LED_OFF);
1408
1409         if (brand == ROUTER_WRT54G3G) {
1410                 eval("cardmgr");
1411         }
1412
1413         cprintf("done\n");
1414         return;
1415
1416 }
1417
1418 static int check_nv(char *name, char *value)
1419 {
1420         int ret = 0;
1421
1422         if (nvram_match("manual_boot_nv", "1"))
1423                 return 0;
1424
1425         if (!nvram_get(name)) {
1426                 cprintf("ERR: Cann't find %s !.......................\n", name);
1427                 nvram_set(name, value);
1428                 ret++;
1429         } else if (nvram_invmatch(name, value)) {
1430                 cprintf("ERR: The %s is %s, not %s !.................\n", name,
1431                         nvram_safe_get(name), value);
1432                 nvram_set(name, value);
1433                 ret++;
1434         }
1435
1436         return ret;
1437 }
1438
1439 int check_cfe_nv(void)
1440 {
1441         int ret = 0;
1442
1443         switch (getRouterBrand()) {
1444         case ROUTER_BUFFALO_WZRRSG54:
1445                 ret += check_nv("lan_hwnames", "et0 wl0");
1446                 ret += check_nv("wan_hwname", "et1");
1447                 ret += check_nv("vlans", "0");
1448                 break;
1449         case ROUTER_BUFFALO_WBR2G54S:
1450                 ret += check_nv("aa0", "3");
1451
1452                 ret += check_nv("pa0itssit", "62");
1453                 ret += check_nv("pa0b0", "0x1136");
1454                 ret += check_nv("pa0b1", "0xfb93");
1455                 ret += check_nv("pa0b2", "0xfea5");
1456                 ret += check_nv("wl0gpio2", "0");
1457                 ret += check_nv("wl0gpio3", "0");
1458                 ret += check_nv("cctl", "0");
1459                 ret += check_nv("ccode", "0");
1460                 break;
1461 #ifndef HAVE_BUFFALO
1462
1463         case ROUTER_WRT54G:
1464         case ROUTER_WRT54G_V8:
1465         case ROUTER_WRT54G_V81:
1466                 ret += check_nv("aa0", "3");
1467                 /*
1468                  * if (check_hw_type () == BCM5352E_CHIP || check_hw_type () ==
1469                  * BCM5354G_CHIP) ret += check_nv ("ag0", "0x02"); else ret +=
1470                  * check_nv ("ag0", "255");
1471                  */
1472                 if (check_hw_type() == BCM5325E_CHIP) {
1473                         /*
1474                          * Lower the DDR ram drive strength , the value will be
1475                          * stable for all boards Latency 3 is more stable for all ddr
1476                          * 20050420 by honor
1477                          */
1478
1479                         ret += check_nv("sdram_init", "0x010b");
1480                         ret += check_nv("sdram_config", "0x0062");
1481
1482                         if (nvram_match("clkfreq", "200")
1483                             && nvram_match("overclocking", "200")) {
1484                                 ret += check_nv("clkfreq", "216");
1485                                 nvram_set("overclocking", "216");
1486                         }
1487
1488                         if (ret) {
1489                                 nvram_set("sdram_ncdl", "0x0");
1490
1491                         }
1492                         ret += check_nv("pa0itssit", "62");
1493                         ret += check_nv("pa0b0", "0x15eb");
1494                         ret += check_nv("pa0b1", "0xfa82");
1495                         ret += check_nv("pa0b2", "0xfe66");
1496                 } else if (check_hw_type() == BCM5354G_CHIP) {
1497                         ret += check_nv("pa0itssit", "62");
1498                         ret += check_nv("pa0b0", "0x1326");
1499                         ret += check_nv("pa0b1", "0xFB51");
1500                         ret += check_nv("pa0b2", "0xFE87");
1501                         ret += check_nv("reset_gpio", "7");
1502                 } else if (check_hw_type() == BCM4705_BCM5397_EWC_CHIP) {
1503                         // nothing to do
1504                 } else if (check_hw_type() == BCM4704_BCM5325F_CHIP) {
1505                         // nothing to do
1506                 } else {
1507                         ret += check_nv("pa0itssit", "62");
1508                         ret += check_nv("pa0b0", "0x170c");
1509                         ret += check_nv("pa0b1", "0xfa24");
1510                         ret += check_nv("pa0b2", "0xfe70");
1511                 }
1512
1513                 // ret += check_nv("gpio2", "adm_eecs");
1514                 // ret += check_nv("gpio3", "adm_eesk");
1515                 // ret += check_nv("gpio5", "adm_eedi");
1516                 // ret += check_nv("gpio6", "adm_rc");
1517
1518                 ret += check_nv("wl0gpio2", "0");
1519                 ret += check_nv("wl0gpio3", "0");
1520
1521                 ret += check_nv("cctl", "0");
1522                 ret += check_nv("ccode", "0");
1523                 break;
1524 #endif
1525         }
1526         if (ret) {
1527                 cprintf
1528                     ("Some error found, we want to reboot!.....................\n");
1529                 nvram_commit();
1530                 sys_reboot();
1531         }
1532
1533         return ret;
1534 }
1535
1536 int check_pmon_nv(void)
1537 {
1538         return 0;
1539 }
1540
1541 void start_overclocking(void)
1542 {
1543 #ifdef HAVE_OVERCLOCKING
1544         cprintf("Overclocking started\n");
1545
1546         int rev = cpu_plltype();
1547
1548         if (rev == 0)
1549                 return;         // unsupported
1550
1551         char *ov = nvram_get("overclocking");
1552
1553         if (ov == NULL)
1554                 return;
1555         int clk = atoi(ov);
1556
1557         if (nvram_get("clkfreq") == NULL)
1558                 return;         // unsupported
1559
1560         char *pclk = nvram_safe_get("clkfreq");
1561         char dup[64];
1562
1563         strcpy(dup, pclk);
1564         int i;
1565
1566         for (i = 0; i < strlen(dup); i++)
1567                 if (dup[i] == ',')
1568                         dup[i] = 0;
1569         int cclk = atoi(dup);
1570
1571         if ((cclk < 150 && rev == 3) || (cclk < 192 && rev == 4)
1572             || (cclk < 183 && rev == 7)) {
1573                 cprintf("clkfreq is %d (%s), this is unsupported\n", cclk, dup);
1574                 return;         // unsupported
1575         }
1576
1577         if (clk == cclk) {
1578                 cprintf("clkfreq identical with new setting\n");
1579                 return;         // clock already set
1580         }
1581
1582         int set = 1;
1583         int clk2 = 0;
1584         char clkfr[16];
1585
1586         switch (clk) {
1587         case 150:
1588                 clk2 = 75;
1589                 // nvram_set ("clkfreq", "150,75");
1590                 break;
1591         case 183:
1592                 clk2 = 92;
1593                 // nvram_set ("clkfreq", "183,92");
1594                 break;
1595         case 187:
1596                 clk2 = 94;
1597                 // nvram_set ("clkfreq", "187,94");
1598                 break;
1599         case 192:
1600                 clk2 = 96;
1601                 // nvram_set ("clkfreq", "192,96");
1602                 break;
1603         case 198:
1604                 clk2 = 98;
1605                 // nvram_set ("clkfreq", "198,98");
1606                 break;
1607         case 200:
1608                 clk2 = 100;
1609                 // nvram_set ("clkfreq", "200,100");
1610                 break;
1611         case 216:
1612                 clk2 = 108;
1613                 // nvram_set ("clkfreq", "216,108");
1614                 break;
1615         case 225:
1616                 clk2 = 113;
1617                 // nvram_set ("clkfreq", "225,113");
1618                 break;
1619         case 228:
1620                 clk2 = 114;
1621                 // nvram_set ("clkfreq", "228,114");
1622                 break;
1623         case 233:
1624                 clk2 = 116;
1625                 // nvram_set ("clkfreq", "233,116");
1626                 break;
1627         case 237:
1628                 clk2 = 119;
1629                 // nvram_set ("clkfreq", "237,119");
1630                 break;
1631         case 240:
1632                 clk2 = 120;
1633                 // nvram_set ("clkfreq", "240,120");
1634                 break;
1635         case 250:
1636                 clk2 = 125;
1637                 // nvram_set ("clkfreq", "250,125");
1638                 break;
1639         case 252:
1640                 clk2 = 126;
1641                 // nvram_set ("clkfreq", "252,126");
1642                 break;
1643         case 264:
1644                 clk2 = 132;
1645                 // nvram_set ("clkfreq", "264,132");
1646                 break;
1647         case 280:
1648                 clk2 = 120;
1649                 // nvram_set ("clkfreq", "280,120");
1650                 break;
1651         case 300:
1652                 clk2 = 120;
1653                 // nvram_set ("clkfreq", "300,120");
1654                 break;
1655         default:
1656                 set = 0;
1657                 break;
1658         }
1659
1660         if (set) {
1661                 cprintf
1662                     ("clock frequency adjusted from %d to %d, reboot needed\n",
1663                      cclk, clk);
1664                 sprintf(clkfr, "%d,%d", clk, clk2);
1665                 nvram_set("clkfreq", clkfr);
1666                 nvram_commit();
1667                 cprintf("Overclocking done, rebooting...\n");
1668                 sys_reboot();
1669         }
1670 #endif
1671 }
1672
1673 char *enable_dtag_vlan(int enable)
1674 {
1675         int donothing = 0;
1676
1677         nvram_set("fromvdsl", "1");
1678         if (nvram_match("vdsl_state", "1") && enable)
1679                 donothing = 1;
1680         if ((nvram_match("vdsl_state", "0")
1681              || nvram_match("vdsl_state", "")) && !enable)
1682                 donothing = 1;
1683         if (enable)
1684                 nvram_set("vdsl_state", "1");
1685         else
1686                 nvram_set("vdsl_state", "0");
1687
1688         char *vlan7ports = NULL;
1689
1690         vlan7ports = "4t 5";
1691         if (nvram_match("vlan1ports", "4 5")) {
1692                 vlan7ports = "4t 5";
1693         }
1694         if (nvram_match("vlan1ports", "0 5")) {
1695                 vlan7ports = "0t 5";
1696         }
1697         if (nvram_match("vlan1ports", "1 5")) {
1698                 vlan7ports = "1t 5";
1699         }
1700         if (nvram_match("vlan2ports", "0 8") || nvram_match("vlan2ports", "0 8*") || nvram_match("vlan2ports", "0 8t") || nvram_match("vlan1ports", "4 8"))     // special
1701                 //
1702                 // condition
1703                 // for
1704                 // Broadcom
1705                 // Gigabit
1706                 // Phy
1707                 // routers
1708                 //
1709         {
1710 #ifdef HAVE_MADWIFI
1711                 char *eth = "eth0";
1712 #else
1713                 char *eth = "eth1";
1714 #endif
1715                 vlan7ports = "0t 8";
1716                 int vlanswap = 0;
1717
1718                 if (nvram_match("vlan1ports", "4 8")) {
1719                         vlanswap = 1;
1720                         vlan7ports = "4t 8";
1721                 }
1722                 char *save_ports2 = nvram_safe_get("vlan2ports");
1723                 char *save_ports1 = nvram_safe_get("vlan1ports");
1724 #ifndef HAVE_MADWIFI
1725                 if (getRouterBrand() == ROUTER_WRT600N
1726                     || getRouterBrand() == ROUTER_WRT610N)
1727                         eth = "eth2";
1728 #endif
1729                 if (donothing) {
1730                         nvram_set("fromvdsl", "0");
1731                         return eth;
1732                 }
1733                 if (enable) {
1734                         if (vlanswap)
1735                                 nvram_set("vlan1ports", "");
1736                         else
1737                                 nvram_set("vlan2ports", "");
1738                         nvram_set("vlan7ports", vlan7ports);
1739                         if (nvram_match("dtag_vlan8", "1")) {
1740                                 nvram_set("vlan8ports", vlan7ports);
1741                         }
1742                 }
1743                 stop_lan();
1744                 eval("ifconfig", eth, "down");
1745                 rmmod("bcm57xxlsys");
1746                 insmod("bcm57xxlsys");
1747                 eval("ifconfig", eth, "up");
1748                 start_config_vlan();
1749                 start_lan();
1750                 if (enable) {
1751                         nvram_set("vlan1ports", save_ports1);
1752                         nvram_set("vlan2ports", save_ports2);
1753                         nvram_set("vlan7ports", "");
1754                         nvram_set("vlan8ports", "");
1755                 }
1756                 nvram_set("fromvdsl", "0");
1757                 return eth;
1758         }
1759
1760         if (nvram_match("switch_type", "BCM5325"))      // special condition
1761                 // for Broadcom
1762                 // Gigabit Phy
1763                 // routers
1764         {
1765 #ifdef HAVE_MADWIFI
1766                 char *eth = "eth0";
1767 #else
1768                 char *eth = "eth1";
1769 #endif
1770                 vlan7ports = "0t 5";
1771                 char *save_ports2 = nvram_safe_get("vlan1ports");
1772                 char *save_ports1 = nvram_safe_get("vlan0ports");
1773
1774                 if (donothing) {
1775                         nvram_set("fromvdsl", "0");
1776                         return eth;
1777                 }
1778                 if (enable) {
1779                         nvram_set("vlan1ports", "");
1780                         nvram_set("vlan7ports", vlan7ports);
1781                         if (nvram_match("dtag_vlan8", "1")) {
1782                                 nvram_set("vlan8ports", vlan7ports);
1783                         }
1784                 }
1785                 stop_lan();
1786                 eval("ifconfig", eth, "down");
1787                 rmmod("bcm57xxlsys");
1788                 insmod("bcm57xxlsys");
1789                 eval("ifconfig", eth, "up");
1790                 start_config_vlan();
1791                 start_lan();
1792                 if (enable) {
1793                         nvram_set("vlan0ports", save_ports1);
1794                         nvram_set("vlan1ports", save_ports2);
1795                         nvram_set("vlan7ports", "");
1796                         nvram_set("vlan8ports", "");
1797                 }
1798                 nvram_set("fromvdsl", "0");
1799                 return eth;
1800         }
1801
1802         char *eth = "eth0";
1803
1804         FILE *in = fopen("/proc/switch/eth1/reset", "rb");      // this
1805
1806         // condition
1807         // fails
1808         // almost.
1809         // just one
1810         // router
1811         // (DLINK
1812         // DIR-330)
1813         // requires
1814         // it
1815         if (in) {
1816                 eth = "eth1";
1817                 fclose(in);
1818         } else {
1819                 FILE *in = fopen("/proc/switch/eth2/reset", "rb");      // this
1820
1821                 // condition
1822                 // fails
1823                 // almost.
1824                 // just one
1825                 // router
1826                 // (DLINK
1827                 // DIR-330)
1828                 // requires
1829                 // it
1830                 if (in) {
1831                         eth = "eth2";
1832                         fclose(in);
1833                 } else
1834                         eth = "eth0";
1835         }
1836
1837         if (!donothing) {
1838                 sysprintf("echo 1 > /proc/switch/%s/reset", eth);
1839                 if (enable) {
1840                         fprintf(stderr, "enable vlan port mapping %s/%s\n",
1841                                 nvram_safe_get("vlan0ports"), vlan7ports);
1842                         if (!nvram_match("dtag_vlan8", "1")) {
1843                                 sysprintf
1844                                     ("echo \"%s\" > /proc/switch/%s/vlan/0/ports",
1845                                      nvram_safe_get("vlan0ports"), eth);
1846                                 start_setup_vlans();
1847                                 sysprintf
1848                                     ("echo \"%s\" > /proc/switch/%s/vlan/1/ports",
1849                                      "", eth);
1850                                 sysprintf
1851                                     ("echo \"%s\" > /proc/switch/%s/vlan/7/ports",
1852                                      vlan7ports, eth);
1853                         } else {
1854                                 sysprintf
1855                                     ("echo \"%s\" > /proc/switch/%s/vlan/0/ports",
1856                                      nvram_safe_get("vlan0ports"), eth);
1857                                 start_setup_vlans();
1858                                 sysprintf
1859                                     ("echo \"%s\" > /proc/switch/%s/vlan/1/ports",
1860                                      "", eth);
1861                                 sysprintf
1862                                     ("echo \"%s\" > /proc/switch/%s/vlan/7/ports",
1863                                      vlan7ports, eth);
1864                                 sysprintf
1865                                     ("echo \"%s\" > /proc/switch/%s/vlan/8/ports",
1866                                      vlan7ports, eth);
1867                         }
1868                 } else {
1869                         fprintf(stderr, "disable vlan port mapping %s/%s\n",
1870                                 nvram_safe_get("vlan0ports"),
1871                                 nvram_safe_get("vlan1ports"));
1872                         sysprintf("echo \"%s\" > /proc/switch/%s/vlan/8/ports",
1873                                   "", eth);
1874                         sysprintf("echo \"%s\" > /proc/switch/%s/vlan/7/ports",
1875                                   "", eth);
1876                         sysprintf("echo \"%s\" > /proc/switch/%s/vlan/0/ports",
1877                                   nvram_safe_get("vlan0ports"), eth);
1878                         sysprintf("echo \"%s\" > /proc/switch/%s/vlan/1/ports",
1879                                   nvram_safe_get("vlan1ports"), eth);
1880                         start_setup_vlans();
1881                 }
1882         }
1883         nvram_set("fromvdsl", "0");
1884         return eth;
1885 }
1886
1887 void start_dtag(void)
1888 {
1889         enable_dtag_vlan(1);
1890 }
Note: See TracBrowser for help on using the browser.