source: src/router/services/sysinit/sysinit.c @ 14606

Last change on this file since 14606 was 14606, checked in by BrainSlayer, 3 years ago

better

File size: 53.4 KB
Line 
1/*
2 * sysinit.c
3 *
4 * Copyright (C) 2007 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.
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
34#include <sys/klog.h>
35#include <sys/types.h>
36#include <sys/mount.h>
37#include <sys/reboot.h>
38#include <sys/stat.h>
39#include <sys/sysmacros.h>
40#include <sys/time.h>
41#include <sys/utsname.h>
42#include <sys/wait.h>
43#include <dirent.h>
44
45#include <epivers.h>
46#include <bcmnvram.h>
47#include <mtd.h>
48#include <shutils.h>
49#include <rc.h>
50#include <netconf.h>
51#include <nvparse.h>
52#include <bcmdevs.h>
53
54#include <wlutils.h>
55#include <utils.h>
56#include <cyutils.h>
57#include <code_pattern.h>
58#include <cy_conf.h>
59// #include <mkfiles.h>
60#include <typedefs.h>
61#include <bcmnvram.h>
62#include <bcmutils.h>
63#include <shutils.h>
64#include <wlutils.h>
65#include <cy_conf.h>
66#include <cymac.h>
67// #include <ledcontrol.h>
68
69#define WL_IOCTL(name, cmd, buf, len) (ret = wl_ioctl((name), (cmd), (buf), (len)))
70
71#define TXPWR_MAX 251
72#define TXPWR_DEFAULT 28
73
74void start_restore_defaults(void);
75static void rc_signal(int sig);
76extern void start_overclocking(void);
77extern int check_cfe_nv(void);
78extern int check_pmon_nv(void);
79static void unset_nvram(void);
80void start_nvram(void);
81
82extern struct nvram_tuple *srouter_defaults;
83extern void load_defaults(void);
84extern void free_defaults(void);
85
86int endswith(char *str, char *cmp)
87{
88        int cmp_len, str_len, i;
89
90        cmp_len = strlen(cmp);
91        str_len = strlen(str);
92        if (cmp_len > str_len)
93                return (0);
94        for (i = 0; i < cmp_len; i++) {
95                if (str[(str_len - 1) - i] != cmp[(cmp_len - 1) - i])
96                        return (0);
97        }
98        return (1);
99}
100
101#ifdef HAVE_MACBIND
102#include "../../../opt/mac.h"
103#endif
104
105void runStartup(char *folder, char *extension)
106{
107        struct dirent **entry;
108        struct stat filestat;
109        DIR *directory;
110        int num, n = 0;
111        char fullname[128];
112
113        directory = opendir(folder);
114        if (directory == NULL) {
115                return;
116        }
117        closedir(directory);
118
119        num = scandir(folder, &entry, 0, alphasort);
120        if (num < 0)
121                return;
122        // list all files in this directory
123        while (n < num) {
124                if (!strcmp(extension, "K**") && strlen(entry[n]->d_name) > 3 && startswith(entry[n]->d_name, "K") && strspn(entry[n]->d_name, "K1234567890") == 3) {   // K* scripts
125                        sprintf(fullname, "%s/%s", folder, entry[n]->d_name);
126                        if (!stat(fullname, &filestat)
127                            && (filestat.st_mode & S_IXUSR))
128                                sysprintf("%s 2>&1 > /dev/null", fullname);
129                        free(entry[n]);
130                        n++;
131                        continue;
132                }
133                if (!strcmp(extension, "S**") && strlen(entry[n]->d_name) > 3 && startswith(entry[n]->d_name, "S") && strspn(entry[n]->d_name, "S1234567890") == 3) {   // S* scripts
134                        sprintf(fullname, "%s/%s", folder, entry[n]->d_name);
135                        if (!stat(fullname, &filestat)
136                            && (filestat.st_mode & S_IXUSR))
137                                sysprintf("%s 2>&1 > /dev/null", fullname);
138                        free(entry[n]);
139                        n++;
140                        continue;
141                }
142                if (endswith(entry[n]->d_name, extension)) {
143#ifdef HAVE_REGISTER
144                        if (!isregistered_real()) {
145                                if (endswith
146                                    (entry[n]->d_name, "wdswatchdog.startup")) {
147                                        free(entry[n]);
148                                        n++;
149                                        continue;
150                                }
151                                if (endswith
152                                    (entry[n]->d_name, "schedulerb.startup")) {
153                                        free(entry[n]);
154                                        n++;
155                                        continue;
156                                }
157                                if (endswith
158                                    (entry[n]->d_name,
159                                     "proxywatchdog.startup")) {
160                                        free(entry[n]);
161                                        n++;
162                                        continue;
163                                }
164                        }
165#endif
166                        sysprintf("%s/%s 2>&1 > /dev/null", folder,
167                                  entry[n]->d_name);
168                        // execute script
169                }
170                free(entry[n]);
171                n++;
172        }
173        free(entry);
174        return;
175}
176
177#ifdef HAVE_BUFFALO
178
179extern void *getUEnv(char *name);
180
181static void buffalo_defaults(int force)
182{
183        if (nvram_get("ath0_akm") == NULL || force) {
184                char *mode_ex = getUEnv("DEF-p_wireless_ath0_11bg-authmode_ex");
185                if (mode_ex && !strcmp(mode_ex, "mixed-psk")) {
186                        char *mode =
187                            getUEnv("DEF-p_wireless_ath0_11bg-authmode");
188                        if (!mode)
189                                return;
190                        if (!strcmp(mode, "psk")) {
191                                nvram_set("ath0_akm", "psk psk2");
192                                nvram_set("ath0_security_mode", "psk psk2");
193                        }
194                        if (!strcmp(mode, "psk2")) {
195                                nvram_set("ath0_akm", "psk psk2");
196                                nvram_set("ath0_security_mode", "psk psk2");
197                        }
198                } else {
199                        char *mode =
200                            getUEnv("DEF-p_wireless_ath0_11bg-authmode");
201                        if (mode) {
202                                nvram_set("ath0_akm", mode);
203                                nvram_set("ath0_security_mode", mode);
204                        } else
205                                return;
206                }
207
208                char *crypto = getUEnv("DEF-p_wireless_ath0_11bg-crypto");
209                if (crypto)
210                        nvram_set("ath0_crypto", crypto);
211                char *wpapsk = getUEnv("DEF-p_wireless_ath0_11bg-wpapsk");
212                if (wpapsk)
213                        nvram_set("ath0_wpa_psk", wpapsk);
214                struct ifreq ifr;
215                int s;
216
217                if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
218                        char eabuf[32];
219
220                        strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
221                        ioctl(s, SIOCGIFHWADDR, &ifr);
222                        close(s);
223                        unsigned char *edata =
224                            (unsigned char *)ifr.ifr_hwaddr.sa_data;
225                        sprintf(eabuf, "%02X%02X%02X%02X%02X%02X",
226                                edata[0] & 0xff, edata[1] & 0xff,
227                                edata[2] & 0xff, edata[3] & 0xff,
228                                edata[4] & 0xff, edata[5] & 0xff);
229                        nvram_set("ath0_ssid", eabuf);
230                }
231
232                char *region = getUEnv("region");
233                if (region == NULL) {
234                        region = "US";
235                }
236                if (!strcmp(region, "US")) {
237                        nvram_set("ath0_regdomain", "UNITED_STATES");
238                } else if (!strcmp(region, "EU")) {
239                        nvram_set("ath0_regdomain", "GERMANY");
240                } else if (!strcmp(region, "JP")) {
241                        nvram_set("ath0_regdomain", "JAPAN");
242                } else if (!strcmp(region, "AP")) {
243                        nvram_set("ath0_regdomain", "TAIWAN");
244                }
245
246        }
247}
248#endif
249/*
250 * SeG dd-wrt addition for module startup scripts
251 */
252void start_modules(void)
253{
254        runStartup("/etc/config", ".startup");
255
256#ifdef HAVE_RB500
257        runStartup("/usr/local/etc/config", ".startup");        // if available
258#elif HAVE_X86
259        runStartup("/usr/local/etc/config", ".startup");        // if available
260#else
261        runStartup("/jffs/etc/config", ".startup");     // if available
262        runStartup("/mmc/etc/config", ".startup");      // if available
263#endif
264        return;
265}
266
267void start_wanup(void)
268{
269        runStartup("/etc/config", ".wanup");
270#ifdef HAVE_RB500
271        runStartup("/usr/local/etc/config", ".wanup");  // if available
272#elif HAVE_X86
273        runStartup("/usr/local/etc/config", ".wanup");  // if available
274#else
275        runStartup("/jffs/etc/config", ".wanup");       // if available
276        runStartup("/mmc/etc/config", ".wanup");        // if available
277        runStartup("/tmp/etc/config", ".wanup");        // if available
278#endif
279        return;
280}
281
282void start_run_rc_startup(void)
283{
284        DIR *directory;
285        int count = 36;         // 36 * 5 s = 180s
286
287        create_rc_file(RC_STARTUP);
288
289        if (f_exists("/tmp/.rc_startup"))
290                system("/tmp/.rc_startup");
291
292        while (count > 0) {
293                directory = opendir("/opt/etc/init.d");
294                if (directory == NULL) {
295                        sleep(5);
296                        count--;
297                } else {
298                        closedir(directory);
299                        runStartup("/opt/etc/init.d", "S**");   // if available; run S** startup scripts
300                        return;
301                }
302        }
303}
304
305void start_run_rc_shutdown(void)
306{
307        runStartup("/opt/etc/init.d", "K**");   // if available; run K** shutdown scripts
308        create_rc_file(RC_SHUTDOWN);
309        if (f_exists("/tmp/.rc_shutdown"))
310                system("/tmp/.rc_shutdown");
311        return;
312}
313
314int create_rc_file(char *name)
315{
316        FILE *fp;
317        char *p = nvram_safe_get(name);
318        char tmp_file[100] = { 0 };
319
320        if ((void *)0 == name || 0 == p[0])
321                return -1;
322
323        snprintf(tmp_file, 100, "/tmp/.%s", name);
324        unlink(tmp_file);
325
326        fp = fopen(tmp_file, "w");
327        if (fp) {
328                // filter Windows <cr>ud
329                while (*p) {
330                        if (*p != 0x0d)
331                                fprintf(fp, "%c", *p);
332                        p++;
333                }
334        }
335        fclose(fp);
336        chmod(tmp_file, 0700);
337
338        return 0;
339}
340
341static void ses_cleanup(void)
342{
343        /*
344         * well known event to cleanly initialize state machine
345         */
346        nvram_set("ses_event", "2");
347
348        /*
349         * Delete lethal dynamically generated variables
350         */
351        nvram_unset("ses_bridge_disable");
352}
353
354static void ses_restore_defaults(void)
355{
356        char tmp[100], prefix[] = "wlXXXXXXXXXX_ses_";
357        int i;
358
359        /*
360         * Delete dynamically generated variables
361         */
362        for (i = 0; i < MAX_NVPARSE; i++) {
363                sprintf(prefix, "wl%d_ses_", i);
364                nvram_unset(strcat_r(prefix, "ssid", tmp));
365                nvram_unset(strcat_r(prefix, "closed", tmp));
366                nvram_unset(strcat_r(prefix, "wpa_psk", tmp));
367                nvram_unset(strcat_r(prefix, "auth", tmp));
368                nvram_unset(strcat_r(prefix, "wep", tmp));
369                nvram_unset(strcat_r(prefix, "auth_mode", tmp));
370                nvram_unset(strcat_r(prefix, "crypto", tmp));
371                nvram_unset(strcat_r(prefix, "akm", tmp));
372        }
373}
374
375void start_restore_defaults(void)
376{
377
378#ifdef HAVE_RB500
379        struct nvram_tuple generic[] = {
380                {"lan_ifname", "br0", 0},
381                {"lan_ifnames",
382                 "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 ath0 ath1 ath2 ath3 ath4 ath5",
383                 0},
384                {"wan_ifname", "eth0", 0},
385                {"wan_ifnames", "eth0", 0},
386                {0, 0, 0}
387        };
388#elif HAVE_GEMTEK
389        struct nvram_tuple generic[] = {
390                {"lan_ifname", "br0", 0},
391                {"lan_ifnames", "eth1 ath0", 0},
392                {"wan_ifname", "eth0", 0},
393                {"wan_ifnames", "eth0", 0},
394                {0, 0, 0}
395        };
396#elif HAVE_EAP9550
397        struct nvram_tuple generic[] = {
398                {"lan_ifname", "br0", 0},
399                {"lan_ifnames", "eth2 ra0",
400                 0},
401                {"wan_ifname2", "eth2", 0},
402                {"wan_ifname", "eth2", 0},
403                {"wan_default", "eth2", 0},
404                {"wan_ifnames", "eth2", 0},
405                {0, 0, 0}
406        };
407#elif HAVE_RT2880
408        struct nvram_tuple generic[] = {
409                {"lan_ifname", "br0", 0},
410                {"lan_ifnames", "vlan1 vlan2 ra0",
411                 0},
412                {"wan_ifname2", "vlan2", 0},
413                {"wan_ifname", "vlan2", 0},
414                {"wan_default", "vlan2", 0},
415                {"wan_ifnames", "vlan2", 0},
416                {0, 0, 0}
417        };
418#elif HAVE_GATEWORX
419#if defined(HAVE_XIOCOM) || defined(HAVE_MI424WR)
420        struct nvram_tuple generic[] = {
421                {"lan_ifname", "br0", 0},
422                {"lan_ifnames", "ixp1 ath0 ath1 ath2 ath3",
423                 0},
424                {"wan_ifname2", "ixp0", 0},
425                {"wan_ifname", "ixp0", 0},
426                {"wan_default", "ixp0", 0},
427                {"wan_ifnames", "ixp0", 0},
428                {0, 0, 0}
429        };
430#else
431        struct nvram_tuple generic[] = {
432                {"lan_ifname", "br0", 0},
433                {"lan_ifnames", "ixp0 ath0 ath1 ath2 ath3",
434                 0},
435                {"wan_ifname2", "ixp1", 0},
436                {"wan_ifname", "ixp1", 0},
437                {"wan_default", "ixp1", 0},
438                {"wan_ifnames", "ixp1", 0},
439                {0, 0, 0}
440        };
441#endif
442#elif HAVE_X86
443        struct nvram_tuple generic[] = {
444                {"lan_ifname", "br0", 0},
445#ifdef HAVE_NOWIFI
446                {"lan_ifnames",
447                 "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10",
448                 0},
449#else
450#ifdef HAVE_GW700
451                {"lan_ifnames",
452                 "eth0 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath5 ath6 ath7 ath8",
453                 0},
454#else
455                {"lan_ifnames",
456                 "eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 ath0 ath1 ath2 ath3 ath5 ath6 ath7 ath8",
457                 0},
458#endif
459#endif
460#ifdef HAVE_GW700
461                {"wan_ifname", "eth1", 0},
462                {"wan_ifname2", "eth1", 0},
463                {"wan_ifnames", "eth1", 0},
464#else
465                {"wan_ifname", "eth0", 0},
466                {"wan_ifname2", "eth0", 0},
467                {"wan_ifnames", "eth0", 0},
468#endif
469                {0, 0, 0}
470        };
471#elif HAVE_XSCALE
472        struct nvram_tuple generic[] = {
473                {"lan_ifname", "br0", 0},
474                {"lan_ifnames",
475                 "ixp0.1 ixp0.2 ath0 ath1",
476                 0},
477                {"wan_ifname", "ixp1", 0},
478                {"wan_ifname2", "ixp1", 0},
479                {"wan_ifnames", "ixp1", 0},
480                {"wan_default", "ixp1", 0},
481                {0, 0, 0}
482        };
483#elif HAVE_MAGICBOX
484        struct nvram_tuple generic[] = {
485                {"lan_ifname", "br0", 0},
486                {"lan_ifnames", "eth1 ath0",
487                 0},
488                {"wan_ifname", "eth0", 0},
489                {"wan_ifname2", "eth0", 0},
490                {"wan_ifnames", "eth0", 0},
491                {"wan_default", "eth0", 0},
492                {0, 0, 0}
493        };
494#elif HAVE_RB600
495        struct nvram_tuple generic[] = {
496                {"lan_ifname", "br0", 0},
497                {"lan_ifnames",
498                 "eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 ath0 ath1 ath2 ath3 ath4 ath5 ath6 ath7",
499                 0},
500                {"wan_ifname", "eth0", 0},
501                {"wan_ifname2", "eth0", 0},
502                {"wan_ifnames", "eth0", 0},
503                {"wan_default", "eth0", 0},
504                {0, 0, 0}
505        };
506#elif HAVE_FONERA
507        struct nvram_tuple generic[] = {
508                {"lan_ifname", "br0", 0},
509                {"lan_ifnames", "vlan0 ath0", 0},
510                {"wan_ifname", "", 0},
511                {"wan_ifname2", "", 0},
512                {"wan_default", "", 0},
513                {"wan_ifnames", "eth0 vlan1", 0},
514                {0, 0, 0}
515        };
516#elif HAVE_BWRG1000
517        struct nvram_tuple generic[] = {
518                {"lan_ifname", "br0", 0},
519                {"lan_ifnames", "vlan0 vlan2 ath0", 0},
520                {"wan_ifname", "vlan2", 0},
521                {"wan_ifname2", "vlan2", 0},
522                {"wan_ifnames", "vlan2", 0},
523                {"wan_default", "vlan2", 0},
524                {0, 0, 0}
525        };
526#elif HAVE_SOLO51
527        struct nvram_tuple generic[] = {
528                {"lan_ifname", "br0", 0},
529                {"lan_ifnames", "vlan0 vlan2 ath0", 0},
530                {"wan_ifname", "vlan0", 0},
531                {"wan_ifname2", "vlan0", 0},
532                {"wan_ifnames", "vlan0", 0},
533                {"wan_default", "vlan0", 0},
534                {0, 0, 0}
535        };
536#elif HAVE_BS2
537        struct nvram_tuple generic[] = {
538                {"lan_ifname", "br0", 0},
539                {"lan_ifnames", "eth0 ath0 ath1", 0},
540                {"wan_ifname", "eth0", 0},
541                {"wan_ifname2", "eth0", 0},
542                {"wan_ifnames", "eth0", 0},
543                {"wan_default", "eth0", 0},
544                {0, 0, 0}
545        };
546#elif HAVE_NS2
547        struct nvram_tuple generic[] = {
548                {"lan_ifname", "br0", 0},
549                {"lan_ifnames", "eth0 ath0 ath1", 0},
550                {"wan_ifname", "eth0", 0},
551                {"wan_ifname2", "eth0", 0},
552                {"wan_ifnames", "eth0", 0},
553                {"wan_default", "eth0", 0},
554                {0, 0, 0}
555        };
556#elif HAVE_LC2
557        struct nvram_tuple generic[] = {
558                {"lan_ifname", "br0", 0},
559                {"lan_ifnames", "eth0 ath0 ath1", 0},
560                {"wan_ifname", "eth0", 0},
561                {"wan_ifname2", "eth0", 0},
562                {"wan_ifnames", "eth0", 0},
563                {"wan_default", "eth0", 0},
564                {0, 0, 0}
565        };
566#elif HAVE_PICO2
567        struct nvram_tuple generic[] = {
568                {"lan_ifname", "br0", 0},
569                {"lan_ifnames", "eth0 ath0 ath1", 0},
570                {"wan_ifname", "eth0", 0},
571                {"wan_ifname2", "eth0", 0},
572                {"wan_ifnames", "eth0", 0},
573                {"wan_default", "eth0", 0},
574                {0, 0, 0}
575        };
576#elif HAVE_PICO2HP
577        struct nvram_tuple generic[] = {
578                {"lan_ifname", "br0", 0},
579                {"lan_ifnames", "eth0 ath0 ath1", 0},
580                {"wan_ifname", "eth0", 0},
581                {"wan_ifname2", "eth0", 0},
582                {"wan_ifnames", "eth0", 0},
583                {"wan_default", "eth0", 0},
584                {0, 0, 0}
585        };
586#elif HAVE_MS2
587        struct nvram_tuple generic[] = {
588                {"lan_ifname", "br0", 0},
589                {"lan_ifnames", "eth0 ath0 ath1", 0},
590                {"wan_ifname", "eth0", 0},
591                {"wan_ifname2", "eth0", 0},
592                {"wan_ifnames", "eth0", 0},
593                {"wan_default", "eth0", 0},
594                {0, 0, 0}
595        };
596#elif HAVE_BS2HP
597        struct nvram_tuple generic[] = {
598                {"lan_ifname", "br0", 0},
599                {"lan_ifnames", "eth0 ath0 ath1", 0},
600                {"wan_ifname", "eth0", 0},
601                {"wan_ifname2", "eth0", 0},
602                {"wan_ifnames", "eth0", 0},
603                {"wan_default", "eth0", 0},
604                {0, 0, 0}
605        };
606#elif HAVE_LS2
607        struct nvram_tuple generic[] = {
608                {"lan_ifname", "br0", 0},
609                {"lan_ifnames", "vlan0 vlan2 ath0", 0},
610                {"wan_ifname", "vlan0", 0},
611                {"wan_ifname2", "vlan0", 0},
612                {"wan_ifnames", "vlan0", 0},
613                {"wan_default", "vlan0", 0},
614                {0, 0, 0}
615        };
616#elif HAVE_RS
617        struct nvram_tuple generic[] = {
618                {"lan_ifname", "br0", 0},
619                {"lan_ifnames", "eth0 eth1 ath0 ath1 ath2", 0},
620                {"wan_ifname", "eth0", 0},
621                {"wan_ifname2", "eth0", 0},
622                {"wan_ifnames", "eth0", 0},
623                {"wan_default", "eth0", 0},
624                {0, 0, 0}
625        };
626#elif HAVE_WR941
627        struct nvram_tuple generic[] = {
628                {"lan_ifname", "br0", 0},
629                {"lan_ifnames", "vlan0 vlan1 ath0", 0},
630                {"wan_ifname", "vlan1", 0},
631                {"wan_ifname2", "vlan1", 0},
632                {"wan_ifnames", "vlan1", 0},
633                {"wan_default", "vlan1", 0},
634                {0, 0, 0}
635        };
636#elif HAVE_WR1043
637        struct nvram_tuple generic[] = {
638                {"lan_ifname", "br0", 0},
639                {"lan_ifnames", "vlan1 vlan2 ath0", 0},
640                {"wan_ifname", "vlan2", 0},
641                {"wan_ifname2", "vlan2", 0},
642                {"wan_ifnames", "vlan2", 0},
643                {"wan_default", "vlan2", 0},
644                {0, 0, 0}
645        };
646#elif HAVE_AP83
647        struct nvram_tuple generic[] = {
648                {"lan_ifname", "br0", 0},
649                {"lan_ifnames", "eth0 eth1 ath0", 0},
650                {"wan_ifname", "eth1", 0},
651                {"wan_ifname2", "eth1", 0},
652                {"wan_ifnames", "eth1", 0},
653                {"wan_default", "eth1", 0},
654                {0, 0, 0}
655        };
656#elif HAVE_AP94
657        struct nvram_tuple generic[] = {
658                {"lan_ifname", "br0", 0},
659                {"lan_ifnames", "eth0 eth1 ath0", 0},
660                {"wan_ifname", "eth1", 0},
661                {"wan_ifname2", "eth1", 0},
662                {"wan_ifnames", "eth1", 0},
663                {"wan_default", "eth1", 0},
664                {0, 0, 0}
665        };
666#elif HAVE_UBNTM
667        struct nvram_tuple generic[] = {
668                {"lan_ifname", "br0", 0},
669                {"lan_ifnames", "eth0 eth1 ath0", 0},
670                {"wan_ifname", "", 0},
671                {"wan_ifname2", "", 0},
672                {"wan_ifnames", "", 0},
673                {"wan_default", "", 0},
674                {0, 0, 0}
675        };
676#elif HAVE_WHRHPGN
677        struct nvram_tuple generic[] = {
678                {"lan_ifname", "br0", 0},
679                {"lan_ifnames", "eth0 eth1 ath0", 0},
680                {"wan_ifname", "", 0},
681                {"wan_ifname2", "", 0},
682                {"wan_ifnames", "", 0},
683                {"wan_default", "", 0},
684                {0, 0, 0}
685        };
686#elif HAVE_DIR615E
687        struct nvram_tuple generic[] = {
688                {"lan_ifname", "br0", 0},
689                {"lan_ifnames", "eth0 eth1 ath0", 0},
690                {"wan_ifname", "", 0},
691                {"wan_ifname2", "", 0},
692                {"wan_ifnames", "", 0},
693                {"wan_default", "", 0},
694                {0, 0, 0}
695        };
696#elif HAVE_JA76PF
697        struct nvram_tuple generic[] = {
698                {"lan_ifname", "br0", 0},
699                {"lan_ifnames", "eth0 eth1 ath0", 0},
700                {"wan_ifname", "eth1", 0},
701                {"wan_ifname2", "eth1", 0},
702                {"wan_ifnames", "eth1", 0},
703                {"wan_default", "eth1", 0},
704                {0, 0, 0}
705        };
706#elif HAVE_JWAP003
707        struct nvram_tuple generic[] = {
708                {"lan_ifname", "br0", 0},
709                {"lan_ifnames", "eth0 eth1 ath0", 0},
710                {"wan_ifname", "eth1", 0},
711                {"wan_ifname2", "eth1", 0},
712                {"wan_ifnames", "eth1", 0},
713                {"wan_default", "eth1", 0},
714                {0, 0, 0}
715        };
716#elif HAVE_LSX
717        struct nvram_tuple generic[] = {
718                {"lan_ifname", "br0", 0},
719                {"lan_ifnames", "eth0 ath0", 0},
720                {"wan_ifname", "", 0},
721                {"wan_ifname2", "", 0},
722                {"wan_ifnames", "", 0},
723                {"wan_default", "", 0},
724                {0, 0, 0}
725        };
726#elif HAVE_DANUBE
727        struct nvram_tuple generic[] = {
728                {"lan_ifname", "br0", 0},
729                {"lan_ifnames", "eth0 ath0", 0},
730                {"wan_ifname", "", 0},
731                {"wan_ifname2", "", 0},
732                {"wan_ifnames", "", 0},
733                {"wan_default", "", 0},
734                {0, 0, 0}
735        };
736#elif HAVE_WBD222
737        struct nvram_tuple generic[] = {
738                {"lan_ifname", "br0", 0},
739                {"lan_ifnames", "eth0 eth1 eth2 ath0 ath1 ath2", 0},
740                {"wan_ifname", "eth0", 0},
741                {"wan_ifname2", "eth0", 0},
742                {"wan_ifnames", "eth0", 0},
743                {"wan_default", "eth0", 0},
744                {0, 0, 0}
745        };
746#elif HAVE_STORM
747        struct nvram_tuple generic[] = {
748                {"lan_ifname", "br0", 0},
749                {"lan_ifnames", "eth0 ath0", 0},
750                {"wan_ifname", "", 0},
751                {"wan_ifname2", "", 0},
752                {"wan_ifnames", "", 0},
753                {"wan_default", "", 0},
754                {0, 0, 0}
755        };
756#elif HAVE_OPENRISC
757        struct nvram_tuple generic[] = {
758                {"lan_ifname", "br0", 0},
759                {"lan_ifnames", "eth0 eth1 eth2 eth3 ath0", 0},
760                {"wan_ifname", "", 0},
761                {"wan_ifname2", "", 0},
762                {"wan_ifnames", "", 0},
763                {"wan_default", "", 0},
764                {0, 0, 0}
765        };
766#elif HAVE_WP54G
767        struct nvram_tuple generic[] = {
768                {"lan_ifname", "br0", 0},
769                {"lan_ifnames", "eth0 ath0", 0},
770                {"wan_ifname", "eth1", 0},
771                {"wan_ifname2", "eth1", 0},
772                {"wan_ifnames", "eth1", 0},
773                {"wan_default", "eth1", 0},
774                {0, 0, 0}
775        };
776#elif HAVE_NP28G
777        struct nvram_tuple generic[] = {
778                {"lan_ifname", "br0", 0},
779                {"lan_ifnames", "eth0 ath0", 0},
780                {"wan_ifname", "eth1", 0},
781                {"wan_ifname2", "eth1", 0},
782                {"wan_ifnames", "eth1", 0},
783                {"wan_default", "eth1", 0},
784                {0, 0, 0}
785        };
786#elif HAVE_ADM5120
787        struct nvram_tuple generic[] = {
788                {"lan_ifname", "br0", 0},
789                {"lan_ifnames", "eth0 ath0", 0},
790                {"wan_ifname", "", 0},
791                {"wan_ifname2", "", 0},
792                {"wan_ifnames", "", 0},
793                {"wan_default", "", 0},
794                {0, 0, 0}
795        };
796#elif HAVE_LS5
797        struct nvram_tuple generic[] = {
798                {"lan_ifname", "br0", 0},
799                {"lan_ifnames", "ath0", 0},
800                {"wan_ifname", "eth0", 0},
801                {"wan_ifname2", "eth0", 0},
802                {"wan_ifnames", "eth0", 0},
803                {"wan_default", "eth0", 0},
804                {0, 0, 0}
805        };
806#elif HAVE_WHRAG108
807        struct nvram_tuple generic[] = {
808                {"lan_ifname", "br0", 0},
809                {"lan_ifnames", "eth0 ath0 ath1", 0},
810                {"wan_ifname2", "eth1", 0},
811                {"wan_ifname", "eth1", 0},
812                {"wan_ifnames", "eth1", 0},
813                {"wan_default", "eth1", 0},
814                {0, 0, 0}
815        };
816#elif HAVE_PB42
817        struct nvram_tuple generic[] = {
818                {"lan_ifname", "br0", 0},
819                {"lan_ifnames", "eth1 ath0 ath1", 0},
820                {"wan_ifname2", "eth0", 0},
821                {"wan_ifname", "eth0", 0},
822                {"wan_ifnames", "eth0", 0},
823                {"wan_default", "eth0", 0},
824                {0, 0, 0}
825        };
826#elif HAVE_TW6600
827        struct nvram_tuple generic[] = {
828                {"lan_ifname", "br0", 0},
829                {"lan_ifnames", "ath0 ath1", 0},
830                {"wan_ifname2", "eth0", 0},
831                {"wan_ifname", "eth0", 0},
832                {"wan_ifnames", "eth0", 0},
833                {"wan_default", "eth0", 0},
834                {0, 0, 0}
835        };
836#elif HAVE_CA8PRO
837        struct nvram_tuple generic[] = {
838                {"lan_ifname", "br0", 0},
839                {"lan_ifnames", "vlan0 ath0", 0},
840                {"wan_ifname", "vlan1", 0},
841                {"wan_ifname2", "vlan1", 0},
842                {"wan_ifnames", "vlan1", 0},
843                {"wan_default", "vlan1", 0},
844                {0, 0, 0}
845        };
846#elif HAVE_CA8
847        struct nvram_tuple generic[] = {
848                {"lan_ifname", "br0", 0},
849                {"lan_ifnames", "ath0", 0},
850                {"wan_ifname", "eth0", 0},
851                {"wan_ifname2", "eth0", 0},
852                {"wan_ifnames", "eth0", 0},
853                {"wan_default", "eth0", 0},
854                {0, 0, 0}
855        };
856#else
857        struct nvram_tuple generic[] = {
858                {"lan_ifname", "br0", 0},
859                {"lan_ifnames", "eth0 eth2 eth3 eth4", 0},
860                {"wan_ifname", "eth1", 0},
861                {"wan_ifname2", "eth1", 0},
862                {"wan_ifnames", "eth1", 0},
863                {"wan_default", "eth1", 0},
864                {0, 0, 0}
865        };
866        struct nvram_tuple vlan[] = {
867                {"lan_ifname", "br0", 0},
868                {"lan_ifnames", "vlan0 eth1 eth2 eth3", 0},
869                {"wan_ifname", "vlan1", 0},
870                {"wan_ifname2", "vlan1", 0},
871                {"wan_ifnames", "vlan1", 0},
872                {"wan_default", "vlan1", 0},
873                {0, 0, 0}
874        };
875
876        struct nvram_tuple wrt350vlan[] = {
877                {"lan_ifname", "br0", 0},
878                {"lan_ifnames", "vlan1 eth0", 0},
879                {"wan_ifname", "vlan2", 0},
880                {"wan_ifname2", "vlan2", 0},
881                {"wan_ifnames", "vlan2", 0},
882                {"wan_default", "vlan2", 0},
883                {0, 0, 0}
884        };
885
886        struct nvram_tuple wnr3500vlan[] = {
887                {"lan_ifname", "br0", 0},
888                {"lan_ifnames", "vlan1 eth1", 0},
889                {"wan_ifname", "vlan2", 0},
890                {"wan_ifname2", "vlan2", 0},
891                {"wan_ifnames", "vlan2", 0},
892                {"wan_default", "vlan2", 0},
893                {0, 0, 0}
894        };
895
896        struct nvram_tuple wrt320vlan[] = {
897                {"lan_ifname", "br0", 0},
898                {"lan_ifnames", "vlan1 eth1", 0},
899                {"wan_ifname", "vlan2", 0},
900                {"wan_ifname2", "vlan2", 0},
901                {"wan_ifnames", "vlan2", 0},
902                {"wan_default", "vlan2", 0},
903                {0, 0, 0}
904        };
905
906        struct nvram_tuple wrt30011vlan[] = {
907                {"lan_ifname", "br0", 0},
908                {"lan_ifnames", "vlan0 eth0", 0},
909                {"wan_ifname", "vlan1", 0},
910                {"wan_ifname2", "vlan1", 0},
911                {"wan_ifnames", "vlan1", 0},
912                {"wan_default", "vlan1", 0},
913                {0, 0, 0}
914        };
915
916        struct nvram_tuple wrt600vlan[] = {
917                {"lan_ifname", "br0", 0},
918                {"lan_ifnames", "vlan0 eth0 eth1", 0},
919                {"wan_ifname", "vlan2", 0},
920                {"wan_ifname2", "vlan2", 0},
921                {"wan_ifnames", "vlan2", 0},
922                {"wan_default", "vlan2", 0},
923                {0, 0, 0}
924        };
925
926        struct nvram_tuple wrt60011vlan[] = {
927                {"lan_ifname", "br0", 0},
928                {"lan_ifnames", "vlan1 eth0 eth1", 0},
929                {"wan_ifname", "vlan2", 0},
930                {"wan_ifname2", "vlan2", 0},
931                {"wan_ifnames", "vlan2", 0},
932                {"wan_default", "vlan2", 0},
933                {0, 0, 0}
934        };
935
936        struct nvram_tuple wrt6102vlan[] = {
937                {"lan_ifname", "br0", 0},
938                {"lan_ifnames", "vlan1 eth1 eth2", 0},
939                {"wan_ifname", "vlan2", 0},
940                {"wan_ifname2", "vlan2", 0},
941                {"wan_ifnames", "vlan2", 0},
942                {"wan_default", "vlan2", 0},
943                {0, 0, 0}
944        };
945
946        struct nvram_tuple wzr144nhvlan[] = {
947                {"lan_ifname", "br0", 0},
948                {"lan_ifnames", "vlan2 eth0", 0},
949                {"wan_ifname", "vlan1", 0},
950                {"wan_ifname2", "vlan1", 0},
951                {"wan_ifnames", "vlan1", 0},
952                {"wan_default", "vlan1", 0},
953                {0, 0, 0}
954        };
955
956        struct nvram_tuple generic_2[] = {
957                {"lan_ifname", "br0", 0},
958                {"lan_ifnames", "eth1 eth2", 0},
959                {"wan_ifname", "eth0", 0},
960                {"wan_ifname2", "eth0", 0},
961                {"wan_ifnames", "eth0", 0},
962                {"wan_default", "eth0", 0},
963                {0, 0, 0}
964        };
965
966        struct nvram_tuple generic_3[] = {
967                {"lan_ifname", "br0", 0},
968                {"lan_ifnames", "eth0 eth1", 0},
969                {"wan_ifname", "eth2", 0},
970                {"wan_ifname2", "eth2", 0},
971                {"wan_ifnames", "eth2", 0},
972                {"wan_default", "eth2", 0},
973                {0, 0, 0}
974        };
975#endif
976
977        struct nvram_tuple *linux_overrides;
978        struct nvram_tuple *t, *u;
979        int restore_defaults = 0;
980
981        // uint boardflags;
982
983        /*
984         * Restore defaults if told to.
985         *
986         * Note: an intentional side effect is that when upgrading from a
987         * firmware without the sv_restore_defaults var, defaults will also be
988         * restored.
989         */
990        char *et0mac = nvram_safe_get("et0macaddr");
991        char *et1mac = nvram_safe_get("et1macaddr");
992
993        // unsigned char mac[20];
994        // if (getRouterBrand () == ROUTER_BUFFALO_WZRG144NH)
995        // {
996        // if (nvram_get ("il0macaddr") == NULL)
997        // {
998        // strcpy (mac, et0mac);
999        // MAC_ADD (mac);
1000        // nvram_set ("il0macaddr", mac);
1001        // }
1002        // }
1003        load_defaults();
1004#ifdef HAVE_RB500
1005        linux_overrides = generic;
1006        int brand = getRouterBrand();
1007#elif HAVE_XSCALE
1008        linux_overrides = generic;
1009        int brand = getRouterBrand();
1010
1011        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1012                // nvram_invmatch("os_name",
1013                // "linux"))
1014        {
1015                restore_defaults = 1;
1016        }
1017#elif HAVE_X86
1018        linux_overrides = generic;
1019        int brand = getRouterBrand();
1020
1021        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1022                // nvram_invmatch("os_name",
1023                // "linux"))
1024        {
1025                restore_defaults = 1;
1026        }
1027#elif HAVE_MAGICBOX
1028        linux_overrides = generic;
1029        int brand = getRouterBrand();
1030
1031        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1032                // nvram_invmatch("os_name",
1033                // "linux"))
1034        {
1035                restore_defaults = 1;
1036        }
1037#elif HAVE_RB600
1038        linux_overrides = generic;
1039        int brand = getRouterBrand();
1040
1041        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1042                // nvram_invmatch("os_name",
1043                // "linux"))
1044        {
1045                restore_defaults = 1;
1046        }
1047#elif HAVE_GATEWORX
1048        linux_overrides = generic;
1049        int brand = getRouterBrand();
1050
1051        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1052                // nvram_invmatch("os_name",
1053                // "linux"))
1054        {
1055                restore_defaults = 1;
1056        }
1057#elif HAVE_FONERA
1058        linux_overrides = generic;
1059        int brand = getRouterBrand();
1060
1061        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1062                // nvram_invmatch("os_name",
1063                // "linux"))
1064        {
1065                restore_defaults = 1;
1066        }
1067#elif HAVE_SOLO51
1068        linux_overrides = generic;
1069        int brand = getRouterBrand();
1070
1071        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1072                // nvram_invmatch("os_name",
1073                // "linux"))
1074        {
1075                restore_defaults = 1;
1076        }
1077#elif HAVE_RT2880
1078        linux_overrides = generic;
1079        int brand = getRouterBrand();
1080
1081        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1082                // nvram_invmatch("os_name",
1083                // "linux"))
1084        {
1085                restore_defaults = 1;
1086        }
1087#elif HAVE_LS2
1088        linux_overrides = generic;
1089        int brand = getRouterBrand();
1090
1091        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1092                // nvram_invmatch("os_name",
1093                // "linux"))
1094        {
1095                restore_defaults = 1;
1096        }
1097#elif HAVE_LS5
1098        linux_overrides = generic;
1099        int brand = getRouterBrand();
1100
1101        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1102                // nvram_invmatch("os_name",
1103                // "linux"))
1104        {
1105                restore_defaults = 1;
1106        }
1107#elif HAVE_WHRAG108
1108        linux_overrides = generic;
1109        int brand = getRouterBrand();
1110
1111        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1112                // nvram_invmatch("os_name",
1113                // "linux"))
1114        {
1115                restore_defaults = 1;
1116        }
1117#elif HAVE_TW6600
1118        linux_overrides = generic;
1119        int brand = getRouterBrand();
1120
1121        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1122                // nvram_invmatch("os_name",
1123                // "linux"))
1124        {
1125                restore_defaults = 1;
1126        }
1127#elif HAVE_PB42
1128        linux_overrides = generic;
1129        int brand = getRouterBrand();
1130
1131        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1132                // nvram_invmatch("os_name",
1133                // "linux"))
1134        {
1135                restore_defaults = 1;
1136        }
1137#elif HAVE_LSX
1138        linux_overrides = generic;
1139        int brand = getRouterBrand();
1140
1141        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1142                // nvram_invmatch("os_name",
1143                // "linux"))
1144        {
1145                restore_defaults = 1;
1146        }
1147#elif HAVE_DANUBE
1148        linux_overrides = generic;
1149        int brand = getRouterBrand();
1150
1151        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1152                // nvram_invmatch("os_name",
1153                // "linux"))
1154        {
1155                restore_defaults = 1;
1156        }
1157#elif HAVE_OPENRISC
1158        linux_overrides = generic;
1159        int brand = getRouterBrand();
1160
1161        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1162                // nvram_invmatch("os_name",
1163                // "linux"))
1164        {
1165                restore_defaults = 1;
1166        }
1167#elif HAVE_STORM
1168        linux_overrides = generic;
1169        int brand = getRouterBrand();
1170
1171        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1172                // nvram_invmatch("os_name",
1173                // "linux"))
1174        {
1175                restore_defaults = 1;
1176        }
1177#elif HAVE_ADM5120
1178        linux_overrides = generic;
1179        int brand = getRouterBrand();
1180
1181        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1182                // nvram_invmatch("os_name",
1183                // "linux"))
1184        {
1185                restore_defaults = 1;
1186        }
1187#elif HAVE_CA8
1188        linux_overrides = generic;
1189        int brand = getRouterBrand();
1190
1191        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1192                // nvram_invmatch("os_name",
1193                // "linux"))
1194        {
1195                restore_defaults = 1;
1196        }
1197#elif HAVE_GEMTEK
1198        linux_overrides = generic;
1199        int brand = getRouterBrand();
1200#else
1201        int brand = getRouterBrand();
1202
1203        if (nvram_invmatch("sv_restore_defaults", "0")) // ||
1204                // nvram_invmatch("os_name",
1205                // "linux"))
1206        {
1207                // nvram_unset("sv_restore_defaults");
1208                restore_defaults = 1;
1209        }
1210        if (nvram_match("product_name", "INSPECTION")) {
1211                nvram_unset("product_name");
1212                restore_defaults = 1;
1213        }
1214        if (nvram_get("router_name") == NULL)
1215                restore_defaults = 1;
1216
1217        if (restore_defaults) {
1218                cprintf("Restoring defaults...\n");
1219#ifdef HAVE_MICRO
1220                /*
1221                 * adjust ip_conntrack_max based on available memory size
1222                 * some routers that can run micro only have 16MB memory
1223                 */
1224                FILE *fmem = fopen("/proc/meminfo", "r");
1225                char line[128];
1226                unsigned long msize = 0;
1227
1228                if (fmem != NULL) {
1229                        fgets(line, sizeof(line), fmem);        //eat first line
1230                        fgets(line, sizeof(line), fmem);
1231                        if (sscanf(line, "%*s %lu", &msize) == 1) {
1232                                if (msize > (8 * 1024 * 1024)) {
1233                                        nvram_set("ip_conntrack_max", "4096");
1234                                        nvram_set("ip_conntrack_tcp_timeouts",
1235                                                  "3600");
1236                                }
1237                        }
1238                        fclose(fmem);
1239                }
1240#endif
1241                /*
1242                 * these unsets are important for routers where we can't erase nvram
1243                 * and only software restore defaults
1244                 */
1245                nvram_unset("wan_to_lan");
1246                nvram_unset("wl_vifs");
1247                nvram_unset("wl0_vifs");
1248        }
1249        // }
1250
1251        /*
1252         * Delete dynamically generated variables
1253         */
1254        /*
1255         * Choose default lan/wan i/f list.
1256         */
1257        char *ds;
1258
1259        switch (brand) {
1260#ifndef HAVE_BUFFALO
1261        case ROUTER_WRTSL54GS:
1262        case ROUTER_WRT150N:
1263        case ROUTER_WRT160N:
1264        case ROUTER_WRT300N:
1265        case ROUTER_NETGEAR_WNR834B:
1266        case ROUTER_NETGEAR_WNR834BV2:
1267        case ROUTER_NETGEAR_WNDR3300:
1268        case ROUTER_ASUS_WL500G:
1269        case ROUTER_ASUS_WL500W:
1270#endif
1271        case ROUTER_BUFFALO_WZRG300N:
1272        case ROUTER_BUFFALO_WLAH_G54:
1273        case ROUTER_BUFFALO_WAPM_HP_AM54G54:
1274        case ROUTER_BUFFALO_WZRRSG54:
1275        case ROUTER_ASKEY_RT220XD:
1276                linux_overrides = generic;
1277                break;
1278#ifndef HAVE_BUFFALO
1279        case ROUTER_ASUS_WL500GD:
1280        case ROUTER_ASUS_WL550GE:
1281        case ROUTER_BELKIN_F5D7230_V3000:
1282                linux_overrides = vlan;
1283                break;
1284        case ROUTER_NETGEAR_WNR3500L:
1285                linux_overrides = wnr3500vlan;
1286                break;
1287        case ROUTER_WRT160NV3:
1288        case ROUTER_WRT320N:
1289        case ROUTER_WRT310NV2:
1290                linux_overrides = wrt320vlan;
1291                break;
1292        case ROUTER_WRT350N:
1293                linux_overrides = wrt350vlan;
1294                break;
1295        case ROUTER_WRT310N:
1296                linux_overrides = wrt350vlan;
1297                break;
1298        case ROUTER_WRT300NV11:
1299                linux_overrides = wrt30011vlan;
1300                break;
1301        case ROUTER_WRT600N:
1302                if (nvram_match("switch_type", "BCM5395"))
1303                        linux_overrides = wrt60011vlan;
1304                else
1305                        linux_overrides = wrt600vlan;
1306                break;
1307        case ROUTER_WRT610N:
1308                linux_overrides = wrt60011vlan;
1309                break;
1310        case ROUTER_WRT610NV2:
1311                linux_overrides = wrt6102vlan;
1312                break;
1313#endif
1314        case ROUTER_BUFFALO_WZRG144NH:
1315                linux_overrides = wzr144nhvlan;
1316                break;
1317#ifndef HAVE_BUFFALO
1318        case ROUTER_MOTOROLA_WE800G:
1319        case ROUTER_WAP54G_V1:
1320        case ROUTER_SITECOM_WL105B:
1321#endif
1322        case ROUTER_BUFFALO_WLI2_TX1_G54:
1323        case ROUTER_BUFFALO_WLAG54C:
1324                linux_overrides = generic_2;
1325                break;
1326#ifndef HAVE_BUFFALO
1327        case ROUTER_WAP54G_V2:
1328        case ROUTER_VIEWSONIC_WAPBR_100:
1329        case ROUTER_USR_5430:
1330        case ROUTER_BELKIN_F5D7230_V2000:
1331        case ROUTER_NETGEAR_WG602_V3:
1332        case ROUTER_NETGEAR_WG602_V4:
1333#endif
1334        case ROUTER_BUFFALO_WLA2G54C:
1335        case ROUTER_BUFFALO_WLI_TX4_G54HP:
1336                linux_overrides = generic_3;
1337                break;
1338#ifndef HAVE_BUFFALO
1339        case ROUTER_RT480W:
1340        case ROUTER_RT210W:
1341#endif
1342        case ROUTER_BRCM4702_GENERIC:
1343                ds = nvram_safe_get("dhcp_start");
1344                if (ds != NULL && strlen(ds) > 3) {
1345                        fprintf(stderr, "cleaning nvram variables\n");
1346                        for (t = srouter_defaults; t->name; t++) {
1347                                nvram_unset(t->name);
1348                        }
1349                        restore_defaults = 1;
1350                }
1351
1352                /*
1353                 * ds = nvram_safe_get ("http_passwd"); if (ds == NULL || strlen
1354                 * (ds) == 0) //fix for empty default password { nvram_set
1355                 * ("http_passwd", "admin"); } ds = nvram_safe_get ("language");
1356                 * if (ds != NULL && strlen (ds) < 3) { nvram_set ("language",
1357                 * "english"); }
1358                 */
1359                // fall through
1360        default:
1361                if (check_vlan_support())
1362                        linux_overrides = vlan;
1363                else
1364                        linux_overrides = generic;
1365                break;
1366        }
1367#endif
1368        /*
1369         * int i; for (i=0;i<4;i++)
1370         * nvram_set(linux_overrides[i].name,linux_overrides[i].value);
1371         */
1372
1373        /*
1374         * Restore defaults
1375         */
1376#ifdef HAVE_FON
1377        int reset = 0;
1378        char *rev = nvram_safe_get("fon_revision");
1379
1380        if (rev == NULL || strlen(rev) == 0)
1381                reset = 1;
1382        if (strlen(rev) > 0) {
1383                int n = atoi(rev);
1384
1385                if (atoi(srouter_defaults[0].value) != n)
1386                        reset = 1;
1387        }
1388        if (reset) {
1389                for (t = srouter_defaults; t->name; t++) {
1390                        for (u = linux_overrides; u && u->name; u++) {
1391                                if (!strcmp(t->name, u->name)) {
1392                                        nvram_set(u->name, u->value);
1393                                        break;
1394                                }
1395                        }
1396                        if (!u || !u->name)
1397                                nvram_set(t->name, t->value);
1398                }
1399        }
1400#endif
1401#ifdef HAVE_GATEWORX
1402        if (restore_defaults) {
1403                eval("erase", "nvram");
1404        }
1405#elif HAVE_XSCALE
1406        if (restore_defaults)
1407                eval("rm", "-f", "/etc/nvram/*");       // delete nvram database
1408#endif
1409#ifdef HAVE_MAGICBOX
1410        if (restore_defaults) {
1411                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1412                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1413                eval("erase", "nvram");
1414        }
1415#endif
1416#ifdef HAVE_RB600
1417        if (restore_defaults) {
1418                eval("rm", "-f", "/tmp/nvram/*");       // delete nvram database
1419                eval("rm", "-f", "/tmp/nvram/.lock");   // delete nvram database
1420                eval("erase", "nvram");
1421        }
1422#endif
1423#ifdef HAVE_RT2880
1424        if (restore_defaults) {
1425                eval("erase", "nvram");
1426        }
1427#endif
1428#ifdef HAVE_FONERA
1429        if (restore_defaults) {
1430                eval("erase", "nvram");
1431        }
1432#endif
1433#ifdef HAVE_SOLO51
1434        if (restore_defaults) {
1435                eval("erase", "nvram");
1436        }
1437#endif
1438#ifdef HAVE_LS2
1439        if (restore_defaults) {
1440                eval("erase", "nvram");
1441        }
1442#endif
1443#ifdef HAVE_LS5
1444        if (restore_defaults) {
1445                eval("erase", "nvram");
1446        }
1447#endif
1448#ifdef HAVE_WHRAG108
1449        if (restore_defaults) {
1450                eval("erase", "nvram");
1451        }
1452#endif
1453#ifdef HAVE_TW6600
1454        if (restore_defaults) {
1455                eval("erase", "nvram");
1456        }
1457#endif
1458        int nvcnt = 0;
1459
1460#ifndef HAVE_MADWIFI
1461        int icnt = get_wl_instances();
1462#else
1463        int icnt = getdevicecount();
1464#endif
1465#ifdef HAVE_BUFFALO
1466        buffalo_defaults(restore_defaults);
1467#endif
1468        // if (!nvram_match("default_init","1"))
1469        {
1470                for (t = srouter_defaults; t->name; t++) {
1471                        if (restore_defaults || !nvram_get(t->name)) {
1472                                for (u = linux_overrides; u && u->name; u++) {
1473                                        if (!strcmp(t->name, u->name)) {
1474                                                nvcnt++;
1475                                                nvram_set(u->name, u->value);
1476                                                break;
1477                                        }
1478                                }
1479                                if (!u || !u->name) {
1480                                        nvcnt++;
1481                                        nvram_set(t->name, t->value);
1482                                        if (icnt == 1 && startswith(t->name, "wl1_"))   //unset wl1_xx if we have single radio only
1483                                                nvram_unset(t->name);
1484                                }
1485                        }
1486                }
1487        }
1488        free_defaults();
1489        if (strlen(nvram_safe_get("http_username")) == 0) {
1490                nvram_set("http_username", zencrypt("root"));
1491                nvram_set("http_passwd", zencrypt("admin"));
1492        }
1493#ifndef HAVE_FON
1494        if (restore_defaults) {
1495                switch (brand) {
1496                case ROUTER_ASUS_WL520G:
1497                case ROUTER_ASUS_WL500G_PRE_V2:
1498                case ROUTER_BELKIN_F5D7230_V3000:
1499                case ROUTER_USR_5465:
1500                        nvram_set("vlan0ports", "0 1 2 3 5*");
1501                        nvram_set("vlan1ports", "4 5");
1502                        break;
1503                case ROUTER_LINKSYS_WTR54GS:
1504                        nvram_set("vlan0ports", "0 5*");
1505                        nvram_set("vlan1ports", "1 5");
1506                        break;
1507                case ROUTER_ASUS_WL550GE:
1508                        nvram_set("vlan0ports", "1 2 3 4 5*");
1509                        nvram_set("vlan1ports", "0 5");
1510                        break;
1511                case ROUTER_MOTOROLA:
1512                case ROUTER_WRT54G_V8:
1513                case ROUTER_ASUS_RTN12:
1514                        nvram_set("vlan0ports", "3 2 1 0 5*");
1515                        nvram_set("vlan1ports", "4 5");
1516                        break;
1517                case ROUTER_LINKSYS_WRH54G:
1518                case ROUTER_ASUS_WL500GD:
1519                case ROUTER_BUFFALO_WBR2G54S:
1520                        nvram_set("vlan0ports", "4 3 2 1 5*");
1521                        nvram_set("vlan1ports", "0 5");
1522                        break;
1523                case ROUTER_USR_5461:
1524                        nvram_set("vlan0ports", "4 1 2 3 5*");
1525                        nvram_set("vlan1ports", "0 5");
1526                        break;
1527                default:
1528                        if (nvram_match("boardnum", "WAP54GV3_8M_0614")) {
1529                                nvram_set("vlan0ports", "3 2 1 0 5*");
1530                                nvram_set("vlan1ports", "4 5");
1531                        }
1532                        break;
1533                }
1534#ifdef HAVE_SPUTNIK
1535                nvram_set("lan_ipaddr", "192.168.180.1");
1536#elif HAVE_BUFFALO
1537                nvram_set("lan_ipaddr", "192.168.11.1");
1538#else
1539                nvram_set("lan_ipaddr", "192.168.1.1");
1540#endif
1541        }
1542#else
1543        if (restore_defaults) {
1544                nvram_set("lan_ipaddr", "192.168.10.1");
1545        }
1546#endif
1547#ifdef HAVE_SKYTRON
1548        if (restore_defaults) {
1549                nvram_set("lan_ipaddr", "192.168.0.1");
1550        }
1551#endif
1552        if (brand == ROUTER_WRT600N) {
1553                if (nvram_match("switch_type", "BCM5395"))      // fix for WRT600N
1554                        // v1.1 (BCM5395 does
1555                        // not suppport vid
1556                        // 0, so gemtek
1557                        // internally
1558                        // configured vid 1
1559                        // as lan)
1560                {
1561                        nvram_set("vlan0ports", " ");
1562                        nvram_set("vlan1ports", "1 2 3 4 8*");
1563                        nvram_set("vlan2ports", "0 8*");
1564                        nvram_set("vlan0hwname", " ");
1565                        nvram_set("vlan1hwname", "et0");
1566                        nvram_set("landevs", "vlan1 wl0 wl1");
1567                        nvram_set("lan_ifnames", "vlan1 eth0 eth1");
1568                } else {
1569                        if (!nvram_get("vlan0ports")
1570                            || nvram_match("vlan0ports", "")) {
1571                                nvram_set("vlan0ports", "1 2 3 4 8*");
1572                                nvram_set("vlan2ports", "0 8*");
1573                        }
1574                        if (!nvram_get("vlan2ports")
1575                            || nvram_match("vlan2ports", "")) {
1576                                nvram_set("vlan0ports", "1 2 3 4 8*");
1577                                nvram_set("vlan2ports", "0 8*");
1578                        }
1579                }
1580        } else if (brand == ROUTER_WRT610N || brand == ROUTER_WRT610NV2) {
1581                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1582                        nvram_set("vlan1ports", "1 2 3 4 8*");
1583                        nvram_set("vlan2ports", "0 8");
1584                }
1585                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1586                        nvram_set("vlan1ports", "1 2 3 4 8*");
1587                        nvram_set("vlan2ports", "0 8");
1588                }
1589        } else if (brand == ROUTER_WRT350N) {
1590
1591                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1592                        nvram_set("vlan1ports", "1 2 3 4 8*");
1593                        nvram_set("vlan2ports", "0 8");
1594                }
1595                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1596                        nvram_set("vlan1ports", "1 2 3 4 8*");
1597                        nvram_set("vlan2ports", "0 8");
1598                }
1599
1600        } else if (brand == ROUTER_WRT310N) {
1601
1602                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1603                        nvram_set("vlan1ports", "1 2 3 4 8*");
1604                        nvram_set("vlan2ports", "0 8");
1605                }
1606                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1607                        nvram_set("vlan1ports", "1 2 3 4 8*");
1608                        nvram_set("vlan2ports", "0 8");
1609                }
1610
1611        } else if (brand == ROUTER_WRT300NV11) {
1612
1613                if (!nvram_get("vlan0ports") || nvram_match("vlan0ports", "")) {
1614                        nvram_set("vlan0ports", "1 2 3 4 5*");
1615                        nvram_set("vlan1ports", "0 5");
1616                }
1617                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1618                        nvram_set("vlan0ports", "1 2 3 4 5*");
1619                        nvram_set("vlan1ports", "0 5");
1620                }
1621
1622        } else if (brand == ROUTER_BUFFALO_WZRG144NH) {
1623                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1624                        nvram_set("vlan1ports", "4 8");
1625                        nvram_set("vlan2ports", "0 1 2 3 8*");
1626                }
1627                if (!nvram_get("vlan2ports") || nvram_match("vlan2ports", "")) {
1628                        nvram_set("vlan1ports", "4 8");
1629                        nvram_set("vlan2ports", "0 1 2 3 8*");
1630                }
1631
1632        } else {
1633                if (!nvram_get("vlan0hwname") || nvram_match("vlan0hwname", ""))
1634                        nvram_set("vlan0hwname", "et0");
1635                if (!nvram_get("vlan1hwname") || nvram_match("vlan1hwname", ""))
1636                        nvram_set("vlan1hwname", "et0");
1637
1638                switch (brand) {
1639                case ROUTER_MOTOROLA:
1640                case ROUTER_MOTOROLA_V1:
1641                case ROUTER_MOTOROLA_WE800G:
1642                case ROUTER_RT210W:
1643                        if (et0mac != NULL)
1644                                nvram_set("et0macaddr", et0mac);
1645                        if (et1mac != NULL)
1646                                nvram_set("et1macaddr", et1mac);
1647                        break;
1648                }
1649
1650                if (!nvram_get("vlan0ports") || nvram_match("vlan0ports", "")) {
1651                        switch (brand) {
1652                        case ROUTER_NETGEAR_WNR3500L:
1653                        case ROUTER_WRT320N:
1654                                nvram_unset("vlan0hwname");
1655                                break;
1656                        case ROUTER_LINKSYS_WTR54GS:
1657                                nvram_set("vlan0ports", "0 5*");
1658                                break;
1659                        case ROUTER_ASUS_WL500G_PRE:
1660                                nvram_set("vlan0ports", "1 2 3 4 5*");
1661                                break;
1662                        case ROUTER_MOTOROLA:
1663                        case ROUTER_WRT54G_V8:
1664                        case ROUTER_BELKIN_F5D7231_V2000:
1665                                nvram_set("vlan0ports", "3 2 1 0 5*");
1666                                break;
1667                        case ROUTER_LINKSYS_WRT55AG:
1668                        case ROUTER_RT480W:
1669                        case ROUTER_DELL_TRUEMOBILE_2300_V2:
1670                        case ROUTER_ASUS_WL520G:
1671                        case ROUTER_ASUS_WL500G_PRE_V2:
1672                        case ROUTER_WRT54G_V81:
1673                        case ROUTER_BELKIN_F5D7230_V3000:
1674                        case ROUTER_USR_5465:
1675                                nvram_set("vlan0ports", "0 1 2 3 5*");
1676                                break;
1677                        case ROUTER_LINKSYS_WRH54G:
1678                        case ROUTER_ASUS_WL500GD:
1679                        case ROUTER_BUFFALO_WBR2G54S:
1680                                nvram_set("vlan0ports", "4 3 2 1 5*");
1681                                break;
1682                        case ROUTER_USR_5461:
1683                                nvram_set("vlan0ports", "4 1 2 3 5*");
1684                                break;
1685                        default:
1686                                if (nvram_match("bootnv_ver", "4")
1687                                    || nvram_match("boardnum",
1688                                                   "WAP54GV3_8M_0614"))
1689                                        nvram_set("vlan0ports", "3 2 1 0 5*");
1690                                else
1691                                        nvram_set("vlan0ports", "1 2 3 4 5*");
1692                                break;
1693                        }
1694                }
1695
1696                if (!nvram_get("vlan1ports") || nvram_match("vlan1ports", "")) {
1697                        switch (brand) {
1698                        case ROUTER_NETGEAR_WNR3500L:
1699                        case ROUTER_WRT320N:
1700                                nvram_set("vlan2ports", "0 8");
1701                                break;
1702                        case ROUTER_LINKSYS_WTR54GS:
1703                                nvram_set("vlan1ports", "1 5");
1704                                break;
1705                        case ROUTER_ASUS_WL500G_PRE:
1706                        case ROUTER_LINKSYS_WRH54G:
1707                        case ROUTER_USR_5461:
1708                                nvram_set("vlan1ports", "0 5");
1709                                break;
1710                        case ROUTER_MOTOROLA:
1711                        case ROUTER_WRT54G_V8:
1712                        case ROUTER_WRT54G_V81:
1713                        case ROUTER_LINKSYS_WRT55AG:
1714                        case ROUTER_RT480W:
1715                        case ROUTER_DELL_TRUEMOBILE_2300_V2:
1716                        case ROUTER_ASUS_WL520G:
1717                        case ROUTER_ASUS_WL500G_PRE_V2:
1718                        case ROUTER_BELKIN_F5D7230_V3000:
1719                        case ROUTER_BELKIN_F5D7231_V2000:
1720                        case ROUTER_USR_5465:
1721                                nvram_set("vlan1ports", "4 5");
1722                                break;
1723                        default:
1724                                if (nvram_match("bootnv_ver", "4")
1725                                    || nvram_match("boardnum",
1726                                                   "WAP54GV3_8M_0614"))
1727                                        nvram_set("vlan1ports", "4 5");
1728                                else
1729                                        nvram_set("vlan1ports", "0 5");
1730                                break;
1731                        }
1732                }
1733
1734        }
1735
1736        if (restore_defaults) { //hack for VLAN page display for some routers: lan is on vlan1, wan is on vlan2
1737                if (strlen(nvram_safe_get("vlan1ports")) == 10) {
1738                        nvram_set("port0vlans", "2");
1739                        nvram_set("port1vlans", "1");
1740                        nvram_set("port2vlans", "1");
1741                        nvram_set("port3vlans", "1");
1742                        nvram_set("port4vlans", "1");
1743                        nvram_set("port5vlans", "1 2 16");
1744                }
1745        }
1746
1747        if (brand == ROUTER_WRT54G || brand == ROUTER_WRT54G1X
1748            || brand == ROUTER_LINKSYS_WRT55AG) {
1749                if (!nvram_get("aa0"))
1750                        nvram_set("aa0", "3");
1751                if (!nvram_get("ag0"))
1752                        nvram_set("ag0", "255");
1753                if (!nvram_get("gpio2"))
1754                        nvram_set("gpio2", "adm_eecs");
1755                if (!nvram_get("gpio3"))
1756                        nvram_set("gpio3", "adm_eesk");
1757                if (!nvram_get("gpio5"))
1758                        nvram_set("gpio5", "adm_eedi");
1759                if (!nvram_get("gpio6"))
1760                        nvram_set("gpio6", "adm_rc");
1761                if (!nvram_get("boardrev") || nvram_match("boardrev", ""))
1762                        nvram_set("boardrev", "0x10");
1763                if (!nvram_get("boardflags") || nvram_match("boardflags", ""))
1764                        nvram_set("boardflags", "0x0388");
1765                if (!nvram_get("boardflags2"))
1766                        nvram_set("boardflags2", "0");
1767        }
1768
1769        if (restore_defaults &&
1770            (brand == ROUTER_ASUS_RTN10
1771             || brand == ROUTER_ASUS_RTN12 || brand == ROUTER_ASUS_RTN16)) {
1772                nvram_set("wl0_txpwr", "17");
1773        }
1774#ifndef HAVE_BUFFALO
1775        if (restore_defaults && brand == ROUTER_BUFFALO_WHRG54S
1776            && nvram_match("DD_BOARD", "Buffalo WHR-HP-G54")) {
1777                nvram_set("wl0_txpwr", "28");
1778        }
1779#endif
1780        if (restore_defaults && brand == ROUTER_BUFFALO_WLI_TX4_G54HP) {
1781                nvram_set("wl0_txpwr", "28");
1782        }
1783
1784        /*
1785         * Always set OS defaults
1786         */
1787        nvram_set("os_name", "linux");
1788        nvram_set("os_version", EPI_VERSION_STR);
1789
1790#ifdef HAVE_SPUTNIK_APD
1791        /*
1792         * Added for Sputnik Agent
1793         */
1794        nvram_unset("sputnik_mjid");
1795        nvram_unset("sputnik_rereg");
1796#endif
1797#ifdef HAVE_FREERADIUS
1798        nvram_unset("cert_running");
1799#endif
1800        nvram_unset("probe_blacklist");
1801
1802        if (nvram_get("overclocking") == NULL) {
1803                char *clk = nvram_safe_get("clkfreq");
1804                char dup[64];
1805
1806                strcpy(dup, clk);
1807                int j;
1808                for (j = 0; j < strlen(dup); j++)
1809                        if (dup[j] == ',')
1810                                dup[j] = 0;
1811
1812                nvram_set("overclocking", dup);
1813        }
1814        cprintf("start overclocking\n");
1815        start_overclocking();
1816        cprintf("done()");
1817        if (nvram_get("http_username") != NULL) {
1818                if (nvram_match("http_username", "")) {
1819#ifdef HAVE_POWERNOC
1820                        nvram_set("http_username", "bJz7PcC1rCRJQ");    // admin
1821#else
1822                        nvram_set("http_username", "bJ/GddyoJuiU2");    // root
1823#endif
1824                }
1825        }
1826        if (atoi(nvram_safe_get("nvram_ver")) < 3) {
1827                nvram_set("nvram_ver", "3");
1828                nvram_set("block_multicast", "1");
1829        }
1830        nvram_set("gozila_action", "0");
1831        nvram_set("generate_key", "0");
1832        nvram_set("clone_wan_mac", "0");
1833        nvram_unset("flash_active");
1834
1835        cprintf("check CFE nv\n");
1836        if (check_now_boot() == PMON_BOOT)
1837                check_pmon_nv();
1838        else
1839                check_cfe_nv();
1840        cprintf("restore defaults\n");
1841
1842        /*
1843         * Commit values
1844         */
1845        if (restore_defaults) {
1846                int i;
1847
1848                unset_nvram();
1849                nvram_commit();
1850                cprintf("done\n");
1851                for (i = 0; i < MAX_NVPARSE; i++) {
1852                        del_wds_wsec(0, i);
1853                        del_wds_wsec(1, i);
1854                }
1855        }
1856}
1857
1858void start_drivers(void)
1859{
1860        /*
1861         * #ifdef HAVE_USB //load usb driver. we will add samba server, ftp
1862         * server and ctorrent support in future modules = "usbcore usb-ohci
1863         * usb-uhci ehci-hcd scsi_mod usb-storage ide-core ide-detect
1864         * ide-disk ide-scsi cdrom ide-cd printer sd_mod sr_mod" foreach
1865         * (module, modules, next) { cprintf ("loading %s\n", module);
1866         * insmod(module); } #endif
1867         */
1868
1869#ifdef HAVE_USB
1870
1871        if (nvram_match("usb_enable", "1")) {
1872                led_control(LED_USB, LED_ON);
1873
1874                cprintf("loading usbcore\n");
1875                insmod("usbcore");
1876
1877                if (nvram_match("usb_uhci", "1")) {
1878                        cprintf("loading usb-uhci\n");
1879                        insmod("usb-uhci");
1880                        insmod("uhci-hcd");
1881                }
1882
1883                if (nvram_match("usb_ohci", "1")) {
1884                        cprintf("loading usb-ohci\n");
1885                        insmod("usb-ohci");
1886                        insmod("ohci-hcd");
1887                }
1888
1889                if (nvram_match("usb_usb2", "1")) {
1890                        cprintf("loading usb2 module\n");
1891                        insmod("ehci-hcd");
1892                }
1893
1894                if (nvram_match("usb_storage", "1")) {
1895                        cprintf("loading scsi_mod\n");
1896                        insmod("scsi_mod");
1897                        insmod("scsi_wait_scan");
1898                        cprintf("loading sd_mod\n");
1899                        insmod("sd_mod");
1900                        cprintf("loading usb-storage\n");
1901                        insmod("usb-storage");
1902
1903                        if (nvram_match("usb_fs_ext3", "1")) {
1904                                cprintf("loading jbd\n");
1905                                insmod("mbcache");
1906                                cprintf("loading ext2\n");
1907                                insmod("ext2");
1908#ifdef HAVE_USB_ADVANCED
1909                                cprintf("loading jbd\n");
1910                                insmod("jbd");
1911                                cprintf("loading ext3\n");
1912                                insmod("ext3");
1913#endif
1914                        }
1915
1916                        if (nvram_match("usb_fs_fat", "1")) {
1917                                cprintf("loading usb_fs_fat\n");
1918                                insmod("nls_base");
1919                                insmod("nls_cp437");
1920                                insmod("nls_iso8859-1");
1921                                insmod("nls_iso8859-2");
1922                                insmod("nls_utf8");
1923                                insmod("fat");
1924                                cprintf("loading usb_fs_vfat\n");
1925                                insmod("vfat");
1926                                cprintf("loading fs_msdos\n");
1927                                insmod("msdos");
1928                        }
1929
1930                        if (nvram_match("usb_fs_xfs", "1")) {
1931                                cprintf("loading usb_fs_xfs\n");
1932                                insmod("xfs");
1933                        }
1934                        // if (nvram_match ("usb_fs_xfs", "1"))
1935                        // {
1936                        // cprintf ("loading usb_fs_xfs\n");
1937                        // insmod("xfs");
1938                        // }
1939                }
1940
1941                if (nvram_match("usb_printer", "1")) {
1942                        cprintf("loading printer\n");
1943                        insmod("printer");
1944                        insmod("usblp");
1945#ifdef HAVE_P910ND
1946                        sleep(2);       // wait for printers to show up
1947                        FILE *test = fopen("/dev/usb/lp0", "rb");
1948                        if (!test) {
1949                                eval("mknod", "/dev/lp0", "c", "180", "0");
1950                                eval("mknod", "/dev/lp1", "c", "180", "1");
1951                                eval("mknod", "/dev/lp2", "c", "180", "2");
1952                                eval("p910nd", "-f", "/dev/lp0", "0");
1953                        } else {
1954                                fclose(test);
1955                                eval("p910nd", "-f", "/dev/usb/lp0", "0");
1956                        }
1957#endif
1958                }
1959                mount("devpts", "/proc/bus/usb", "usbfs", MS_MGC_VAL, NULL);
1960        } else {
1961                led_control(LED_USB, LED_OFF);
1962        }
1963#endif
1964
1965}
1966
1967/*
1968 * States
1969 */
1970enum {
1971        RESTART,
1972        STOP,
1973        START,
1974        TIMER,
1975        USER,
1976        IDLE,
1977};
1978static int state = START;
1979static int signalled = -1;
1980
1981/*
1982 * Signal handling
1983 */
1984static void rc_signal(int sig)
1985{
1986        if (state == IDLE) {
1987                if (sig == SIGHUP) {
1988                        printf("signalling RESTART\n");
1989                        signalled = RESTART;
1990                } else if (sig == SIGUSR2) {
1991                        printf("signalling START\n");
1992                        signalled = START;
1993                } else if (sig == SIGINT) {
1994                        printf("signalling STOP\n");
1995                        signalled = STOP;
1996                } else if (sig == SIGALRM) {
1997                        printf("signalling TIMER\n");
1998                        signalled = TIMER;
1999                } else if (sig == SIGUSR1) {    // Receive from WEB
2000                        printf("signalling USER1\n");
2001                        signalled = USER;
2002                }
2003
2004        }
2005}
2006
2007/*
2008 * Timer procedure
2009 */
2010int do_timer(void)
2011{
2012        // do_ntp();
2013        return 0;
2014}
2015
2016#define CONVERT_NV(old, new) \
2017        if(nvram_get(old)) \
2018                nvram_set(new, nvram_safe_get(old));
2019
2020void start_nvram(void)
2021{
2022        int i = 0;
2023
2024        /*
2025         * broadcom 3.11.48.7 change some nvram name
2026         */
2027
2028        nvram_unset("wl0_hwaddr");      // When disbale wireless, we must get
2029        //
2030        // null wireless mac */
2031
2032        nvram_set("wan_get_dns", "");
2033        nvram_set("filter_id", "1");
2034        nvram_set("wl_active_add_mac", "0");
2035        nvram_set("ddns_change", "");
2036        nvram_unset("action_service");
2037        nvram_set("wan_get_domain", "");
2038
2039        // if(!nvram_get("wl_macmode1")){
2040        // if(nvram_match("wl_macmode","disabled"))
2041        // nvram_set("wl_macmode1","disabled");
2042        // else
2043        // nvram_set("wl_macmode1","other");
2044        // }
2045        if (nvram_match("wl_gmode", "5"))       // Mixed mode had been
2046                // changed to 5
2047                nvram_set("wl_gmode", "1");
2048
2049        if (nvram_match("wl_gmode", "4"))       // G-ONLY mode had been
2050                // changed to 2, after 1.40.1
2051                // for WiFi G certication
2052                nvram_set("wl_gmode", "2");
2053
2054        // nvram_set("wl_country","Worldwide"); // The country always Worldwide
2055
2056        nvram_set("ping_ip", "");
2057        nvram_set("ping_times", "");
2058        // nvram_set ("traceroute_ip", "");
2059
2060        nvram_set("filter_port", "");   // The name have been disbaled from
2061        // 1.41.3
2062
2063#ifdef HAVE_UPNP
2064        if ((nvram_match("restore_defaults", "1"))
2065            || (nvram_match("upnpcas", "1"))) {
2066                nvram_set("upnp_clear", "1");
2067        } else {
2068                char s[32];
2069                char *nv;
2070
2071                for (i = 0; i < MAX_NVPARSE; ++i) {
2072                        sprintf(s, "forward_port%d", i);
2073                        if ((nv = nvram_get(s)) != NULL) {
2074                                if (strstr(nv, "msmsgs"))
2075                                        nvram_unset(s);
2076                        }
2077                }
2078        }
2079        nvram_set("upnp_wan_proto", "");
2080#endif
2081
2082        /*
2083         * The tkip and aes already are changed to wl_crypto from v3.63.3.0
2084         */
2085        if (nvram_match("wl_wep", "tkip")) {
2086                nvram_set("wl_crypto", "tkip");
2087        } else if (nvram_match("wl_wep", "aes")) {
2088                nvram_set("wl_crypto", "aes");
2089        } else if (nvram_match("wl_wep", "tkip+aes")) {
2090                nvram_set("wl_crypto", "tkip+aes");
2091        }
2092
2093        if (nvram_match("wl_wep", "restricted"))
2094                nvram_set("wl_wep", "enabled"); // the nas need this value,
2095        // the "restricted" is no
2096        // longer need. (20040624 by
2097        // honor)
2098
2099#ifdef HAVE_SET_BOOT
2100        if (!nvram_match("boot_wait_web", "0"))
2101                nvram_set("boot_wait_web", "1");
2102#endif
2103#ifndef HAVE_BUFFALO
2104        if (check_hw_type() == BCM5352E_CHIP) {
2105                nvram_set("opo", "0");  // OFDM power reducement in quarter
2106                // dbm (2 dbm in this case)
2107                nvram_set("ag0", "0");  // Antenna Gain definition in dbm
2108        }
2109#endif
2110
2111        if (nvram_match("svqos_port1bw", "full"))
2112                nvram_set("svqos_port1bw", "FULL");
2113        if (nvram_match("svqos_port2bw", "full"))
2114                nvram_set("svqos_port2bw", "FULL");
2115        if (nvram_match("svqos_port3bw", "full"))
2116                nvram_set("svqos_port3bw", "FULL");
2117        if (nvram_match("svqos_port4bw", "full"))
2118                nvram_set("svqos_port4bw", "FULL");
2119        // dirty fix for WBR2 units
2120
2121        // clean old filter_servicesX to free nvram
2122        nvram_unset("filter_services0");
2123        nvram_unset("filter_services1");
2124        nvram_unset("filter_services2");
2125        nvram_unset("filter_services3");
2126        nvram_unset("filter_services4");
2127        nvram_unset("filter_services5");
2128        nvram_unset("filter_services6");
2129        nvram_unset("filter_services7");
2130
2131        nvram_unset("vdsl_state");      // important (this value should never
2132        //
2133        // be commited, but if this will fix
2134        // the vlan7 issue)
2135        nvram_unset("fromvdsl");        // important (this value should never be
2136        // commited, but if this will fix the vlan7
2137        // issue)
2138
2139        nvram_unset("do_reboot");       //for GUI, see broadcom.c
2140
2141#ifdef DIST
2142        nvram_set("dist_type", DIST);
2143#endif
2144
2145        {
2146
2147#ifdef DIST
2148#ifndef HAVE_TW6600
2149#ifdef HAVE_MICRO
2150                // if dist_type micro, check styles, and force to elegant if needed
2151
2152#ifdef HAVE_ROUTERSTYLE
2153                char *style = nvram_safe_get("router_style");
2154
2155                if (!strstr("blue cyan elegant green orange red yellow", style))
2156#endif
2157                {
2158                        nvram_set("router_style", "elegant");
2159                }
2160#endif
2161#endif
2162#endif
2163        }
2164
2165#ifdef HAVE_WIVIZ
2166        if (!strlen(nvram_safe_get("hopseq"))
2167            || !strlen(nvram_safe_get("hopdwell"))) {
2168                nvram_set("hopdwell", "1000");
2169                nvram_set("hopseq", "0");
2170        }
2171#endif
2172        nvram_unset("lasthour");
2173#ifdef HAVE_AQOS
2174        //filter hostapd shaping rules
2175        char *qos_mac = nvram_safe_get("svqos_macs");
2176
2177        if (strlen(qos_mac) > 0) {
2178                char *newqos = malloc(strlen(qos_mac));
2179
2180                memset(newqos, 0, strlen(qos_mac));
2181                char level[32], level2[32], data[32], type[32];
2182
2183                do {
2184                        if (sscanf
2185                            (qos_mac, "%31s %31s %31s %31s |", data, level,
2186                             level2, type) < 4)
2187                                break;
2188                        if (strcmp(type, "hostapd") && strcmp(type, "pppd")) {
2189                                if (strlen(newqos) > 0)
2190                                        sprintf(newqos, "%s %s %s %s %s |",
2191                                                newqos, data, level, level2,
2192                                                type);
2193                                else
2194                                        sprintf(newqos, "%s %s %s %s |", data,
2195                                                level, level2, type);
2196
2197                        }
2198                }
2199                while ((qos_mac = strpbrk(++qos_mac, "|")) && qos_mac++);
2200                nvram_set("svqos_macs", newqos);
2201                free(newqos);
2202        }
2203#endif
2204        return;
2205}
2206
2207static void unset_nvram(void)
2208{
2209#ifndef MPPPOE_SUPPORT
2210        nvram_safe_unset("ppp_username_1");
2211        nvram_safe_unset("ppp_passwd_1");
2212        nvram_safe_unset("ppp_idletime_1");
2213        nvram_safe_unset("ppp_demand_1");
2214        nvram_safe_unset("ppp_redialperiod_1");
2215        nvram_safe_unset("ppp_service_1");
2216        nvram_safe_unset("mpppoe_enable");
2217        nvram_safe_unset("mpppoe_dname");
2218#endif
2219#ifndef HAVE_HTTPS
2220        nvram_safe_unset("remote_mgt_https");
2221#endif
2222#ifndef HSIAB_SUPPORT
2223        nvram_safe_unset("hsiab_mode");
2224        nvram_safe_unset("hsiab_provider");
2225        nvram_safe_unset("hsiab_device_id");
2226        nvram_safe_unset("hsiab_device_password");
2227        nvram_safe_unset("hsiab_admin_url");
2228        nvram_safe_unset("hsiab_registered");
2229        nvram_safe_unset("hsiab_configured");
2230        nvram_safe_unset("hsiab_register_ops");
2231        nvram_safe_unset("hsiab_session_ops");
2232        nvram_safe_unset("hsiab_config_ops");
2233        nvram_safe_unset("hsiab_manual_reg_ops");
2234        nvram_safe_unset("hsiab_proxy_host");
2235        nvram_safe_unset("hsiab_proxy_port");
2236        nvram_safe_unset("hsiab_conf_time");
2237        nvram_safe_unset("hsiab_stats_time");
2238        nvram_safe_unset("hsiab_session_time");
2239        nvram_safe_unset("hsiab_sync");
2240        nvram_safe_unset("hsiab_config");
2241#endif
2242
2243#ifndef HEARTBEAT_SUPPORT
2244        nvram_safe_unset("hb_server_ip");
2245        nvram_safe_unset("hb_server_domain");
2246#endif
2247
2248#ifndef PARENTAL_CONTROL_SUPPORT
2249        nvram_safe_unset("artemis_enable");
2250        nvram_safe_unset("artemis_SVCGLOB");
2251        nvram_safe_unset("artemis_HB_DB");
2252        nvram_safe_unset("artemis_provisioned");
2253#endif
2254
2255#ifndef WL_STA_SUPPORT
2256        // nvram_safe_unset("wl_ap_ssid");
2257        // nvram_safe_unset("wl_ap_ip");
2258#endif
2259
2260}
Note: See TracBrowser for help on using the repository browser.