New routers supported
[tomato.git] / release / src / router / rc / init.c
blob3b8a281187ce04838f160b5e7c6937a60c0043d7
1 /*
3 Copyright 2005, Broadcom Corporation
4 All Rights Reserved.
6 THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
7 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
8 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
9 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
13 #include "rc.h"
15 #include <termios.h>
16 #include <dirent.h>
17 #include <sys/ioctl.h>
18 #include <sys/mount.h>
19 #include <time.h>
20 #include <errno.h>
21 #include <paths.h>
22 #include <sys/wait.h>
23 #include <sys/reboot.h>
24 #include <sys/klog.h>
25 #ifdef LINUX26
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/sysinfo.h>
29 #endif
30 #include <wlutils.h>
31 #include <bcmdevs.h>
33 #define SHELL "/bin/sh"
35 static int fatalsigs[] = {
36 SIGILL,
37 SIGABRT,
38 SIGFPE,
39 SIGPIPE,
40 SIGBUS,
41 SIGSYS,
42 SIGTRAP,
43 SIGPWR
46 static int initsigs[] = {
47 SIGHUP,
48 SIGUSR1,
49 SIGUSR2,
50 SIGINT,
51 SIGQUIT,
52 SIGALRM,
53 SIGTERM
56 static char *defenv[] = {
57 "TERM=vt100",
58 "HOME=/",
59 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
60 "SHELL=" SHELL,
61 "USER=root",
62 NULL
65 /* Set terminal settings to reasonable defaults */
66 static void set_term(int fd)
68 struct termios tty;
70 tcgetattr(fd, &tty);
72 /* set control chars */
73 tty.c_cc[VINTR] = 3; /* C-c */
74 tty.c_cc[VQUIT] = 28; /* C-\ */
75 tty.c_cc[VERASE] = 127; /* C-? */
76 tty.c_cc[VKILL] = 21; /* C-u */
77 tty.c_cc[VEOF] = 4; /* C-d */
78 tty.c_cc[VSTART] = 17; /* C-q */
79 tty.c_cc[VSTOP] = 19; /* C-s */
80 tty.c_cc[VSUSP] = 26; /* C-z */
82 /* use line dicipline 0 */
83 tty.c_line = 0;
85 /* Make it be sane */
86 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
87 tty.c_cflag |= CREAD|HUPCL|CLOCAL;
90 /* input modes */
91 tty.c_iflag = ICRNL | IXON | IXOFF;
93 /* output modes */
94 tty.c_oflag = OPOST | ONLCR;
96 /* local modes */
97 tty.c_lflag =
98 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
100 tcsetattr(fd, TCSANOW, &tty);
103 static int console_init(void)
105 int fd;
107 /* Clean up */
108 ioctl(0, TIOCNOTTY, 0);
109 close(0);
110 close(1);
111 close(2);
112 setsid();
114 /* Reopen console */
115 if ((fd = open(_PATH_CONSOLE, O_RDWR)) < 0) {
116 /* Avoid debug messages is redirected to socket packet if no exist a UART chip, added by honor, 2003-12-04 */
117 open("/dev/null", O_RDONLY);
118 open("/dev/null", O_WRONLY);
119 open("/dev/null", O_WRONLY);
120 perror(_PATH_CONSOLE);
121 return errno;
123 dup2(fd, 0);
124 dup2(fd, 1);
125 dup2(fd, 2);
127 ioctl(0, TIOCSCTTY, 1);
128 tcsetpgrp(0, getpgrp());
129 set_term(0);
131 return 0;
135 * Waits for a file descriptor to change status or unblocked signal
136 * @param fd file descriptor
137 * @param timeout seconds to wait before timing out or 0 for no timeout
138 * @return 1 if descriptor changed status or 0 if timed out or -1 on error
140 static int waitfor(int fd, int timeout)
142 fd_set rfds;
143 struct timeval tv = { timeout, 0 };
145 FD_ZERO(&rfds);
146 FD_SET(fd, &rfds);
147 return select(fd + 1, &rfds, NULL, NULL, (timeout > 0) ? &tv : NULL);
150 static pid_t run_shell(int timeout, int nowait)
152 pid_t pid;
153 int sig;
155 /* Wait for user input */
156 if (waitfor(STDIN_FILENO, timeout) <= 0) return 0;
158 switch (pid = fork()) {
159 case -1:
160 perror("fork");
161 return 0;
162 case 0:
163 /* Reset signal handlers set for parent process */
164 for (sig = 0; sig < (_NSIG-1); sig++)
165 signal(sig, SIG_DFL);
167 /* Reopen console */
168 console_init();
169 printf("\n\nTomato %s\n\n", tomato_version);
171 /* Now run it. The new program will take over this PID,
172 * so nothing further in init.c should be run. */
173 execve(SHELL, (char *[]) { SHELL, NULL }, defenv);
175 /* We're still here? Some error happened. */
176 perror(SHELL);
177 exit(errno);
178 default:
179 if (nowait) {
180 return pid;
182 else {
183 waitpid(pid, NULL, 0);
184 return 0;
189 int console_main(int argc, char *argv[])
191 for (;;) run_shell(0, 0);
193 return 0;
196 static void shutdn(int rb)
198 int i;
199 int act;
200 sigset_t ss;
202 _dprintf("shutdn rb=%d\n", rb);
204 sigemptyset(&ss);
205 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++)
206 sigaddset(&ss, fatalsigs[i]);
207 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++)
208 sigaddset(&ss, initsigs[i]);
209 sigprocmask(SIG_BLOCK, &ss, NULL);
211 for (i = 30; i > 0; --i) {
212 if (((act = check_action()) == ACT_IDLE) || (act == ACT_REBOOT)) break;
213 _dprintf("Busy with %d. Waiting before shutdown... %d\n", act, i);
214 sleep(1);
216 set_action(ACT_REBOOT);
218 // Disconnect pppd - need this for PPTP/L2TP to finish gracefully
219 stop_pptp();
220 stop_l2tp();
222 _dprintf("TERM\n");
223 kill(-1, SIGTERM);
224 sleep(3);
225 sync();
227 _dprintf("KILL\n");
228 kill(-1, SIGKILL);
229 sleep(1);
230 sync();
232 umount("/jffs"); // may hang if not
233 sleep(1);
235 if (rb != -1) {
236 led(LED_WLAN, 0);
237 if (rb == 0) {
238 for (i = 4; i > 0; --i) {
239 led(LED_DMZ, 1);
240 led(LED_WHITE, 1);
241 usleep(250000);
242 led(LED_DMZ, 0);
243 led(LED_WHITE, 0);
244 usleep(250000);
249 reboot(rb ? RB_AUTOBOOT : RB_HALT_SYSTEM);
251 do {
252 sleep(1);
253 } while (1);
256 static void handle_fatalsigs(int sig)
258 _dprintf("fatal sig=%d\n", sig);
259 shutdn(-1);
262 /* Fixed the race condition & incorrect code by using sigwait()
263 * instead of pause(). But SIGCHLD is a problem, since other
264 * code: 1) messes with it and 2) depends on CHLD being caught so
265 * that the pid gets immediately reaped instead of left a zombie.
266 * Pidof still shows the pid, even though it's in zombie state.
267 * So this SIGCHLD handler reaps and then signals the mainline by
268 * raising ALRM.
270 static void handle_reap(int sig)
272 chld_reap(sig);
273 raise(SIGALRM);
276 static int check_nv(const char *name, const char *value)
278 const char *p;
279 if (!nvram_match("manual_boot_nv", "1")) {
280 if (((p = nvram_get(name)) == NULL) || (strcmp(p, value) != 0)) {
281 _dprintf("Error: Critical variable %s is invalid. Resetting.\n", name);
282 nvram_set(name, value);
283 return 1;
286 return 0;
289 static inline int invalid_mac(const char *mac)
291 return (!mac || !(*mac) || strncasecmp(mac, "00:90:4c", 8) == 0);
294 static int find_sercom_mac_addr(void)
296 FILE *fp;
297 unsigned char m[6], s[18];
299 sprintf(s, MTD_DEV(%dro), 0);
300 if ((fp = fopen(s, "rb"))) {
301 fseek(fp, 0x1ffa0, SEEK_SET);
302 fread(m, sizeof(m), 1, fp);
303 fclose(fp);
304 sprintf(s, "%02X:%02X:%02X:%02X:%02X:%02X",
305 m[0], m[1], m[2], m[3], m[4], m[5]);
306 nvram_set("et0macaddr", s);
307 return !invalid_mac(s);
309 return 0;
312 static int find_dir320_mac_addr(void)
314 FILE *fp;
315 char *buffer, s[18];
316 int i, part, size, found = 0;
318 if (!mtd_getinfo("board_data", &part, &size))
319 goto out;
320 sprintf(s, MTD_DEV(%dro), part);
322 if ((fp = fopen(s, "rb"))) {
323 buffer = malloc(size);
324 memset(buffer, 0, size);
325 fread(buffer, size, 1, fp);
326 if (!memcmp(buffer, "RGCFG1", 6)) {
327 for (i = 6; i < size - 24; i++) {
328 if (!memcmp(buffer + i, "lanmac=", 7)) {
329 memcpy(s, buffer + i + 7, 17);
330 s[17] = 0;
331 nvram_set("et0macaddr", s);
332 found = 1;
334 else if (!memcmp(buffer + i, "wanmac=", 7)) {
335 memcpy(s, buffer + i + 7, 17);
336 s[17] = 0;
337 nvram_set("il0macaddr", s);
338 if (!found) {
339 inc_mac(s, -1);
340 nvram_set("et0macaddr", s);
342 found = 1;
346 free(buffer);
347 fclose(fp);
349 out:
350 if (!found) {
351 strcpy(s, nvram_safe_get("wl0_hwaddr"));
352 inc_mac(s, -2);
353 nvram_set("et0macaddr", s);
355 return 1;
358 static int init_vlan_ports(void)
360 int dirty = 0;
361 int model = get_model();
363 switch (model) {
364 case MODEL_WRT54G:
365 switch (check_hw_type()) {
366 case HW_BCM5352E: // G v4, GS v3, v4
367 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
368 break;
370 break;
371 case MODEL_WTR54GS:
372 dirty |= check_nv("vlan0ports", "0 5*");
373 dirty |= check_nv("vlan1ports", "1 5");
374 dirty |= check_nv("vlan_enable", "1");
375 break;
376 case MODEL_WL500GP:
377 case MODEL_WL500GE:
378 case MODEL_WL500GPv2:
379 case MODEL_WL520GU:
380 case MODEL_WL330GE:
381 if (nvram_match("vlan1ports", "0 5u")) // 520GU or 330GE or WL500GE?
382 dirty |= check_nv("vlan1ports", "0 5");
383 else if (nvram_match("vlan1ports", "4 5u"))
384 dirty |= check_nv("vlan1ports", "4 5");
385 break;
386 case MODEL_WL500GD:
387 dirty |= check_nv("vlan0ports", "1 2 3 4 5*");
388 dirty |= check_nv("vlan1ports", "0 5");
389 break;
390 case MODEL_DIR320:
391 case MODEL_H618B:
392 dirty |= (nvram_get("vlan2ports") != NULL);
393 nvram_unset("vlan2ports");
394 dirty |= check_nv("vlan1ports", "0 5");
395 break;
396 case MODEL_WRT310Nv1:
397 dirty |= check_nv("vlan1ports", "1 2 3 4 8*");
398 dirty |= check_nv("vlan2ports", "0 8");
399 break;
400 case MODEL_WL1600GL:
401 dirty |= check_nv("vlan0ports", "0 1 2 3 5*");
402 dirty |= check_nv("vlan1ports", "4 5");
403 break;
404 #ifdef CONFIG_BCMWL5
405 case MODEL_WNR3500L:
406 case MODEL_WRT320N:
407 case MODEL_WNR3500LV2:
408 case MODEL_RTN16:
409 case MODEL_RTN66U:
410 dirty |= check_nv("vlan1ports", "4 3 2 1 8*");
411 dirty |= check_nv("vlan2ports", "0 8");
412 break;
413 case MODEL_W1800R:
414 dirty |= check_nv("vlan1ports", "1 2 3 4 8*");
415 dirty |= check_nv("vlan2ports", "0 8");
416 break;
417 case MODEL_RTN53:
418 dirty |= check_nv("vlan2ports", "0 1 2 3 5*");
419 dirty |= check_nv("vlan1ports", "4 5");
420 break;
421 case MODEL_RTN53A1:
422 dirty |= check_nv("vlan1ports", "4 5");
423 dirty |= check_nv("vlan2ports", "3 2 1 0 5*");
424 break;
425 case MODEL_WNR2000v2:
426 dirty |= check_nv("vlan1ports", "4 3 2 1 5*");
427 dirty |= check_nv("vlan2ports", "0 5");
428 break;
429 case MODEL_HG320:
430 case MODEL_H218N:
431 case MODEL_RG200E_CA:
432 dirty |= check_nv("vlan1ports", "1 2 3 4 5*");
433 dirty |= check_nv("vlan2ports", "0 5");
434 break;
435 case MODEL_RTN10:
436 dirty |= check_nv("vlan1ports", "4 5");
437 break;
438 case MODEL_RTN10U:
439 case MODEL_CW5358U:
440 dirty |= check_nv("vlan0ports", "1 2 3 4 5*");
441 dirty |= check_nv("vlan1ports", "0 5");
442 break;
443 case MODEL_RTN10P:
444 case MODEL_RTN12:
445 case MODEL_RTN12B1:
446 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
447 dirty |= check_nv("vlan1ports", "4 5");
448 break;
449 case MODEL_WRT610Nv2:
450 case MODEL_F5D8235v3:
451 dirty |= check_nv("vlan1ports", "1 2 3 4 8*");
452 dirty |= check_nv("vlan2ports", "0 8");
453 break;
454 case MODEL_F7D3301:
455 case MODEL_F7D4301:
456 dirty |= check_nv("vlan1ports", "3 2 1 0 8*");
457 dirty |= check_nv("vlan2ports", "4 8");
458 break;
459 case MODEL_E900:
460 case MODEL_E1500:
461 case MODEL_E1550:
462 case MODEL_E2500:
463 case MODEL_F7D3302:
464 case MODEL_F7D4302:
465 case MODEL_DIR620C1:
466 dirty |= check_nv("vlan1ports", "0 1 2 3 5*");
467 dirty |= check_nv("vlan2ports", "4 5");
468 break;
469 case MODEL_E1000v2:
470 case MODEL_L600N:
471 dirty |= check_nv("vlan1ports", "1 2 3 4 5*");
472 dirty |= check_nv("vlan2ports", "0 5");
473 break;
474 case MODEL_RTN15U:
475 case MODEL_E3200:
476 case MODEL_E4200:
477 dirty |= check_nv("vlan1ports", "0 1 2 3 8*");
478 dirty |= check_nv("vlan2ports", "4 8");
479 break;
480 case MODEL_WRT160Nv3:
481 if (nvram_match("vlan1ports", "1 2 3 4 5*")) {
482 // fix lan port numbering on CSE41, CSE51
483 dirty |= check_nv("vlan1ports", "4 3 2 1 5*");
485 else if (nvram_match("vlan1ports", "1 2 3 4 8*")) {
486 // WRT310Nv2 ?
487 dirty |= check_nv("vlan1ports", "4 3 2 1 8*");
489 break;
490 #endif
493 return dirty;
496 static void check_bootnv(void)
498 int dirty;
499 int hardware;
500 int model;
501 char mac[18];
503 model = get_model();
504 dirty = check_nv("wl0_leddc", "0x640000") | check_nv("wl1_leddc", "0x640000");
506 switch (model) {
507 case MODEL_WTR54GS:
508 dirty |= check_nv("vlan0hwname", "et0");
509 dirty |= check_nv("vlan1hwname", "et0");
510 break;
511 case MODEL_WBRG54:
512 dirty |= check_nv("wl0gpio0", "130");
513 break;
514 case MODEL_WR850GV1:
515 case MODEL_WR850GV2:
516 // need to cleanup some variables...
517 if ((nvram_get("t_model") == NULL) && (nvram_get("MyFirmwareVersion") != NULL)) {
518 nvram_unset("MyFirmwareVersion");
519 nvram_set("restore_defaults", "1");
521 break;
522 case MODEL_WL330GE:
523 dirty |= check_nv("wl0gpio1", "0x02");
524 break;
525 case MODEL_WL500W:
526 /* fix WL500W mac adresses for WAN port */
527 if (invalid_mac(nvram_get("et1macaddr"))) {
528 strcpy(mac, nvram_safe_get("et0macaddr"));
529 inc_mac(mac, 1);
530 dirty |= check_nv("et1macaddr", mac);
532 dirty |= check_nv("wl0gpio0", "0x88");
533 break;
534 case MODEL_WL500GP:
535 dirty |= check_nv("sdram_init", "0x0009"); // 32MB; defaults: 0x000b, 0x0009
536 dirty |= check_nv("wl0gpio0", "136");
537 break;
538 case MODEL_WL500GPv2:
539 case MODEL_WL520GU:
540 dirty |= check_nv("wl0gpio1", "136");
541 break;
542 case MODEL_WL500GD:
543 dirty |= check_nv("vlan0hwname", "et0");
544 dirty |= check_nv("vlan1hwname", "et0");
545 dirty |= check_nv("boardflags", "0x00000100"); // set BFL_ENETVLAN
546 nvram_unset("wl0gpio0");
547 break;
548 case MODEL_DIR320:
549 if (strlen(nvram_safe_get("et0macaddr")) == 12 ||
550 strlen(nvram_safe_get("il0macaddr")) == 12) {
551 dirty |= find_dir320_mac_addr();
553 if (nvram_get("vlan2hwname") != NULL) {
554 nvram_unset("vlan2hwname");
555 dirty = 1;
557 dirty |= check_nv("wandevs", "vlan1");
558 dirty |= check_nv("vlan1hwname", "et0");
559 dirty |= check_nv("wl0gpio0", "8");
560 dirty |= check_nv("wl0gpio1", "0");
561 dirty |= check_nv("wl0gpio2", "0");
562 dirty |= check_nv("wl0gpio3", "0");
563 case MODEL_WL1600GL:
564 if (invalid_mac(nvram_get("et0macaddr"))) {
565 dirty |= find_sercom_mac_addr();
567 break;
568 case MODEL_WRT160Nv1:
569 case MODEL_WRT310Nv1:
570 case MODEL_WRT300N:
571 dirty |= check_nv("wl0gpio0", "8");
572 break;
573 #ifdef CONFIG_BCMWL5
574 case MODEL_WNR3500L:
575 dirty |= check_nv("boardflags", "0x00000710"); // needed to enable USB
576 dirty |= check_nv("vlan2hwname", "et0");
577 dirty |= check_nv("ledbh0", "7");
578 break;
579 case MODEL_WNR3500LV2:
580 dirty |= check_nv("vlan2hwname", "et0");
581 break;
582 case MODEL_WNR2000v2:
583 dirty |= check_nv("ledbh5", "8");
584 break;
585 case MODEL_WRT320N:
586 dirty |= check_nv("reset_gpio", "5");
587 dirty |= check_nv("ledbh0", "136");
588 dirty |= check_nv("ledbh1", "11");
589 /* fall through, same as RT-N16 */
590 case MODEL_RTN16:
591 dirty |= check_nv("vlan2hwname", "et0");
592 break;
593 case MODEL_HG320:
594 case MODEL_RG200E_CA:
595 case MODEL_H218N:
596 dirty |= check_nv("vlan1hwname", "et0");
597 dirty |= check_nv("vlan2hwname", "et0");
598 dirty |= check_nv("reset_gpio", "30");
599 break;
600 case MODEL_WRT610Nv2:
601 dirty |= check_nv("vlan2hwname", "et0");
602 dirty |= check_nv("pci/1/1/ledbh2", "8");
603 dirty |= check_nv("sb/1/ledbh1", "8");
604 if (invalid_mac(nvram_get("pci/1/1/macaddr"))) {
605 strcpy(mac, nvram_safe_get("et0macaddr"));
606 inc_mac(mac, 3);
607 dirty |= check_nv("pci/1/1/macaddr", mac);
609 break;
610 case MODEL_F7D3301:
611 case MODEL_F7D3302:
612 case MODEL_F7D4301:
613 case MODEL_F7D4302:
614 case MODEL_F5D8235v3:
615 if (nvram_match("sb/1/macaddr", nvram_safe_get("et0macaddr"))) {
616 strcpy(mac, nvram_safe_get("et0macaddr"));
617 inc_mac(mac, 2);
618 dirty |= check_nv("sb/1/macaddr", mac);
619 inc_mac(mac, 1);
620 dirty |= check_nv("pci/1/1/macaddr", mac);
622 case MODEL_E4200:
623 dirty |= check_nv("vlan2hwname", "et0");
624 if (strncasecmp(nvram_safe_get("pci/1/1/macaddr"), "00:90:4c", 8) == 0 ||
625 strncasecmp(nvram_safe_get("sb/1/macaddr"), "00:90:4c", 8) == 0) {
626 strcpy(mac, nvram_safe_get("et0macaddr"));
627 inc_mac(mac, 2);
628 dirty |= check_nv("sb/1/macaddr", mac);
629 inc_mac(mac, 1);
630 dirty |= check_nv("pci/1/1/macaddr", mac);
632 break;
633 case MODEL_E900:
634 case MODEL_E1000v2:
635 case MODEL_E1500:
636 case MODEL_E1550:
637 case MODEL_E2500:
638 case MODEL_E3200:
639 case MODEL_WRT160Nv3:
640 case MODEL_L600N:
641 case MODEL_DIR620C1:
642 dirty |= check_nv("vlan2hwname", "et0");
643 break;
644 #endif
646 case MODEL_WRT54G:
647 if (strncmp(nvram_safe_get("pmon_ver"), "CFE", 3) != 0) return;
649 hardware = check_hw_type();
650 if (!nvram_get("boardtype") ||
651 !nvram_get("boardnum") ||
652 !nvram_get("boardflags") ||
653 !nvram_get("clkfreq") ||
654 !nvram_get("os_flash_addr") ||
655 !nvram_get("dl_ram_addr") ||
656 !nvram_get("os_ram_addr") ||
657 !nvram_get("scratch") ||
658 !nvram_get("et0macaddr") ||
659 ((hardware != HW_BCM4704_BCM5325F) && (!nvram_get("vlan0ports") || !nvram_get("vlan0hwname")))) {
660 _dprintf("Unable to find critical settings, erasing NVRAM\n");
661 mtd_erase("nvram");
662 goto REBOOT;
665 dirty |= check_nv("aa0", "3");
666 dirty |= check_nv("wl0gpio0", "136");
667 dirty |= check_nv("wl0gpio2", "0");
668 dirty |= check_nv("wl0gpio3", "0");
669 dirty |= check_nv("cctl", "0");
670 dirty |= check_nv("ccode", "0");
672 switch (hardware) {
673 case HW_BCM5325E:
674 /* Lower the DDR ram drive strength , the value will be stable for all boards
675 Latency 3 is more stable for all ddr 20050420 by honor */
676 dirty |= check_nv("sdram_init", "0x010b");
677 dirty |= check_nv("sdram_config", "0x0062");
678 if (!nvram_match("debug_clkfix", "0")) {
679 dirty |= check_nv("clkfreq", "216");
681 if (dirty) {
682 nvram_set("sdram_ncdl", "0x0");
684 dirty |= check_nv("pa0itssit", "62");
685 dirty |= check_nv("pa0b0", "0x15eb");
686 dirty |= check_nv("pa0b1", "0xfa82");
687 dirty |= check_nv("pa0b2", "0xfe66");
688 //dirty |= check_nv("pa0maxpwr", "0x4e");
689 break;
690 case HW_BCM5352E: // G v4, GS v3, v4
691 dirty |= check_nv("sdram_init", "0x010b");
692 dirty |= check_nv("sdram_config", "0x0062");
693 if (dirty) {
694 nvram_set("sdram_ncdl", "0x0");
696 dirty |= check_nv("pa0itssit", "62");
697 dirty |= check_nv("pa0b0", "0x168b");
698 dirty |= check_nv("pa0b1", "0xfabf");
699 dirty |= check_nv("pa0b2", "0xfeaf");
700 //dirty |= check_nv("pa0maxpwr", "0x4e");
701 break;
702 case HW_BCM5354G:
703 dirty |= check_nv("pa0itssit", "62");
704 dirty |= check_nv("pa0b0", "0x1326");
705 dirty |= check_nv("pa0b1", "0xFB51");
706 dirty |= check_nv("pa0b2", "0xFE87");
707 //dirty |= check_nv("pa0maxpwr", "0x4e");
708 break;
709 case HW_BCM4704_BCM5325F:
710 // nothing to do
711 break;
712 default:
713 dirty |= check_nv("pa0itssit", "62");
714 dirty |= check_nv("pa0b0", "0x170c");
715 dirty |= check_nv("pa0b1", "0xfa24");
716 dirty |= check_nv("pa0b2", "0xfe70");
717 //dirty |= check_nv("pa0maxpwr", "0x48");
718 break;
720 break;
722 } // switch (model)
724 dirty |= init_vlan_ports();
726 if (dirty) {
727 nvram_commit();
728 REBOOT: // do a simple reboot
729 sync();
730 reboot(RB_AUTOBOOT);
731 exit(0);
735 static int init_nvram(void)
737 unsigned long features;
738 int model;
739 const char *mfr;
740 const char *name;
741 const char *ver;
742 char s[256];
743 unsigned long bf;
744 unsigned long n;
746 model = get_model();
747 sprintf(s, "%d", model);
748 nvram_set("t_model", s);
750 mfr = "Broadcom";
751 name = NULL;
752 ver = NULL;
753 features = 0;
754 switch (model) {
755 case MODEL_WRT54G:
756 mfr = "Linksys";
757 name = "WRT54G/GS/GL";
758 switch (check_hw_type()) {
759 case HW_BCM4712:
760 nvram_set("gpio2", "adm_eecs");
761 nvram_set("gpio3", "adm_eesk");
762 nvram_unset("gpio4");
763 nvram_set("gpio5", "adm_eedi");
764 nvram_set("gpio6", "adm_rc");
765 break;
766 case HW_BCM4702:
767 nvram_unset("gpio2");
768 nvram_unset("gpio3");
769 nvram_unset("gpio4");
770 nvram_unset("gpio5");
771 nvram_unset("gpio6");
772 break;
773 case HW_BCM5352E:
774 nvram_set("opo", "0x0008");
775 nvram_set("ag0", "0x02");
776 // drop
777 default:
778 nvram_set("gpio2", "ses_led");
779 nvram_set("gpio3", "ses_led2");
780 nvram_set("gpio4", "ses_button");
781 features = SUP_SES | SUP_WHAM_LED;
782 break;
784 break;
785 case MODEL_WTR54GS:
786 mfr = "Linksys";
787 name = "WTR54GS";
788 if (!nvram_match("t_fix1", (char *)name)) {
789 nvram_set("lan_ifnames", "vlan0 eth1");
790 nvram_set("gpio2", "ses_button");
791 nvram_set("reset_gpio", "7");
793 nvram_set("pa0itssit", "62");
794 nvram_set("pa0b0", "0x1542");
795 nvram_set("pa0b1", "0xfacb");
796 nvram_set("pa0b2", "0xfec7");
797 //nvram_set("pa0maxpwr", "0x4c");
798 features = SUP_SES;
799 break;
800 case MODEL_WRTSL54GS:
801 mfr = "Linksys";
802 name = "WRTSL54GS";
803 features = SUP_SES | SUP_WHAM_LED;
804 break;
805 case MODEL_WHRG54S:
806 mfr = "Buffalo";
807 name = "WHR-G54S";
808 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
809 break;
810 case MODEL_WHRHPG54:
811 case MODEL_WZRRSG54HP:
812 case MODEL_WZRHPG54:
813 mfr = "Buffalo";
814 features = SUP_SES | SUP_AOSS_LED | SUP_HPAMP;
815 switch (model) {
816 case MODEL_WZRRSG54HP:
817 name = "WZR-RS-G54HP";
818 break;
819 case MODEL_WZRHPG54:
820 name = "WZR-HP-G54";
821 break;
822 default:
823 name = "WHR-HP-G54";
824 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_HPAMP;
825 break;
828 bf = strtoul(nvram_safe_get("boardflags"), NULL, 0);
829 switch (bf) {
830 case 0x0758:
831 case 0x1758:
832 case 0x2758:
833 case 0x3758:
834 if (nvram_match("wlx_hpamp", "")) {
835 if (nvram_get_int("wl_txpwr") > 10) nvram_set("wl_txpwr", "10");
836 nvram_set("wlx_hpamp", "1");
837 nvram_set("wlx_hperx", "0");
840 n = bf;
841 if (nvram_match("wlx_hpamp", "0")) {
842 n &= ~0x2000UL;
844 else {
845 n |= 0x2000UL;
847 if (nvram_match("wlx_hperx", "0")) {
848 n |= 0x1000UL;
850 else {
851 n &= ~0x1000UL;
853 if (bf != n) {
854 sprintf(s, "0x%lX", n);
855 nvram_set("boardflags", s);
857 break;
858 default:
859 syslog(LOG_WARNING, "Unexpected: boardflag=%lX", bf);
860 break;
862 break;
863 case MODEL_WBRG54:
864 mfr = "Buffalo";
865 name = "WBR-G54";
866 break;
867 case MODEL_WBR2G54:
868 mfr = "Buffalo";
869 name = "WBR2-G54";
870 features = SUP_SES | SUP_AOSS_LED;
871 break;
872 case MODEL_WHR2A54G54:
873 mfr = "Buffalo";
874 name = "WHR2-A54G54";
875 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
876 break;
877 case MODEL_WHR3AG54:
878 mfr = "Buffalo";
879 name = "WHR3-AG54";
880 features = SUP_SES | SUP_AOSS_LED;
881 break;
882 case MODEL_WZRG54:
883 mfr = "Buffalo";
884 name = "WZR-G54";
885 features = SUP_SES | SUP_AOSS_LED;
886 break;
887 case MODEL_WZRRSG54:
888 mfr = "Buffalo";
889 name = "WZR-RS-G54";
890 features = SUP_SES | SUP_AOSS_LED;
891 break;
892 case MODEL_WVRG54NF:
893 mfr = "Buffalo";
894 name = "WVR-G54-NF";
895 features = SUP_SES;
896 break;
897 case MODEL_WZRG108:
898 mfr = "Buffalo";
899 name = "WZR-G108";
900 features = SUP_SES | SUP_AOSS_LED;
901 break;
902 case MODEL_RT390W:
903 mfr = "Fuji";
904 name = "RT390W";
905 break;
906 case MODEL_WR850GV1:
907 mfr = "Motorola";
908 name = "WR850G v1";
909 features = SUP_NONVE;
910 break;
911 case MODEL_WR850GV2:
912 mfr = "Motorola";
913 name = "WR850G v2/v3";
914 features = SUP_NONVE;
915 break;
916 case MODEL_WL500GP:
917 mfr = "Asus";
918 name = "WL-500gP";
919 features = SUP_SES;
920 #ifdef TCONFIG_USB
921 nvram_set("usb_ohci", "-1");
922 #endif
923 if (!nvram_match("t_fix1", (char *)name)) {
924 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3"); // set to "vlan0 eth2" by DD-WRT; default: vlan0 eth1
926 break;
927 case MODEL_WL500W:
928 mfr = "Asus";
929 name = "WL-500W";
930 features = SUP_SES | SUP_80211N;
931 #ifdef TCONFIG_USB
932 nvram_set("usb_ohci", "-1");
933 #endif
934 break;
935 case MODEL_WL500GE:
936 mfr = "Asus";
937 name = "WL-550gE";
938 // features = ?
939 #ifdef TCONFIG_USB
940 nvram_set("usb_uhci", "-1");
941 #endif
942 break;
943 case MODEL_WX6615GT:
944 mfr = "SparkLAN";
945 name = "WX-6615GT";
946 // features = ?
947 break;
948 case MODEL_MN700:
949 mfr = "Microsoft";
950 name = "MN-700";
951 break;
952 case MODEL_WR100:
953 mfr = "Viewsonic";
954 name = "WR100";
955 break;
956 case MODEL_WLA2G54L:
957 mfr = "Buffalo";
958 name = "WLA2-G54L";
959 if (!nvram_match("t_fix1", (char *)name)) {
960 nvram_set("lan_ifnames", "vlan0 eth1 eth2");
961 nvram_set("wl_ifname", "eth1");
962 nvram_set("wan_ifname", "none");
964 break;
965 case MODEL_TM2300:
966 mfr = "Dell";
967 name = "TrueMobile 2300";
968 break;
976 #ifndef WL_BSS_INFO_VERSION
977 #error WL_BSS_INFO_VERSION
978 #endif
979 #if WL_BSS_INFO_VERSION >= 108
981 case MODEL_WRH54G:
982 mfr = "Linksys";
983 name = "WRH54G";
985 nvram_set("opo", "12");
986 break;
988 case MODEL_WHRG125:
989 mfr = "Buffalo";
990 name = "WHR-G125";
991 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
993 nvram_set("opo", "0x0008");
994 nvram_set("ag0", "0x0C");
995 break;
996 #ifdef CONFIG_BCMWL5
997 case MODEL_L600N:
998 mfr = "Rosewill";
999 name = "L600N";
1000 features = SUP_SES | SUP_80211N;
1001 if (!nvram_match("t_fix1", (char *)name)) {
1002 #ifdef TCONFIG_USBAP
1003 nvram_set("wl1_hwaddr", nvram_safe_get("0:macaddr"));
1004 nvram_set("ehciirqt", "3");
1005 nvram_set("qtdc_pid", "48407");
1006 nvram_set("qtdc_vid", "2652");
1007 nvram_set("qtdc0_ep", "4");
1008 nvram_set("qtdc0_sz", "0");
1009 nvram_set("qtdc1_ep", "18");
1010 nvram_set("qtdc1_sz", "10");
1011 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1012 nvram_set("landevs", "vlan1 wl0 wl1");
1013 nvram_set("wl0_ifname", "wl0");
1014 nvram_set("wl1_ifname", "wl1");
1015 #else
1016 nvram_set("lan_ifnames", "vlan1 eth1");
1017 nvram_set("landevs", "vlan1 wl0");
1018 #endif
1019 nvram_set("wl_ifname", "eth1");
1020 nvram_set("wan_ifnameX", "vlan2");
1021 nvram_set("wandevs", "vlan2");
1023 break;
1024 case MODEL_DIR620C1:
1025 mfr = "D-Link";
1026 name = "Dir-620 C1";
1027 features = SUP_SES | SUP_80211N;
1028 if (!nvram_match("t_fix1", (char *)name)) {
1029 #ifdef TCONFIG_USBAP
1030 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1031 nvram_set("landevs", "vlan1 wl0 wl1");
1032 nvram_set("wl0_ifname", "eth1");
1033 nvram_set("wl1_ifname", "eth2");
1034 #else
1035 nvram_set("lan_ifnames", "vlan1 eth1");
1036 nvram_set("landevs", "vlan1 wl0");
1037 #endif
1038 nvram_set("wan_ifnameX", "vlan2");
1039 nvram_set("wl_ifname", "eth1");
1042 break;
1043 case MODEL_CW5358U:
1044 mfr = "Catchtech";
1045 name = "CW-5358U";
1046 features = SUP_SES | SUP_80211N;
1047 #ifdef TCONFIG_USB
1048 nvram_set("usb_uhci", "-1");
1049 #endif
1050 if (!nvram_match("t_fix1", (char *)name)) {
1051 nvram_set("lan_ifnames", "vlan0 eth1");
1052 nvram_set("wan_ifnameX", "vlan1");
1053 nvram_set("wl_ifname", "eth1");
1055 break;
1056 case MODEL_HG320:
1057 mfr = "FiberHome";
1058 name = "HG320";
1059 features = SUP_SES | SUP_80211N;
1060 #ifdef TCONFIG_USB
1061 nvram_set("usb_uhci", "-1");
1062 #endif
1063 if (!nvram_match("t_fix1", (char *)name)) {
1064 nvram_set("lan_ifnames", "vlan1 eth1");
1065 nvram_set("wan_ifname", "vlan2");
1066 nvram_set("wan_ifnames", "vlan2");
1067 nvram_set("wan_ifnameX", "vlan2");
1068 nvram_set("wl_ifname", "eth1");
1070 break;
1071 case MODEL_RG200E_CA:
1072 mfr = "ChinaNet";
1073 name = "RG200E-CA";
1074 features = SUP_SES | SUP_80211N;
1075 #ifdef TCONFIG_USB
1076 nvram_set("usb_uhci", "-1");
1077 #endif
1078 if (!nvram_match("t_fix1", (char *)name)) {
1079 nvram_set("lan_ifnames", "vlan1 eth1");
1080 nvram_set("wan_ifname", "vlan2");
1081 nvram_set("wan_ifnames", "vlan2");
1082 nvram_set("wan_ifnameX", "vlan2");
1083 nvram_set("wl_ifname", "eth1");
1085 break;
1086 case MODEL_H218N:
1087 mfr = "ZTE";
1088 name = "H218N";
1089 features = SUP_SES | SUP_80211N;
1090 #ifdef TCONFIG_USB
1091 nvram_set("usb_uhci", "-1");
1092 #endif
1093 if (!nvram_match("t_fix1", (char *)name)) {
1094 nvram_set("lan_ifnames", "vlan1 eth1");
1095 nvram_set("wan_ifname", "vlan2");
1096 nvram_set("wan_ifnames", "vlan2");
1097 nvram_set("wan_ifnameX", "vlan2");
1098 nvram_set("wl_ifname", "eth1");
1100 break;
1101 case MODEL_RTN10:
1102 case MODEL_RTN10P:
1103 mfr = "Asus";
1104 name = nvram_match("boardrev", "0x1153") ? "RT-N10P" : "RT-N10";
1105 features = SUP_SES | SUP_80211N;
1106 if (!nvram_match("t_fix1", (char *)name)) {
1107 nvram_set("lan_ifnames", "vlan0 eth1");
1108 nvram_set("wan_ifnameX", "vlan1");
1109 nvram_set("wl_ifname", "eth1");
1111 break;
1112 case MODEL_RTN10U:
1113 mfr = "Asus";
1114 name = "RT-N10U";
1115 features = SUP_SES | SUP_80211N;
1116 #ifdef TCONFIG_USB
1117 nvram_set("usb_uhci", "-1");
1118 #endif
1119 if (!nvram_match("t_fix1", (char *)name)) {
1120 nvram_set("lan_ifnames", "vlan0 eth1");
1121 nvram_set("wan_ifnameX", "vlan1");
1122 nvram_set("wl_ifname", "eth1");
1124 break;
1125 case MODEL_RTN12:
1126 mfr = "Asus";
1127 name = "RT-N12";
1128 features = SUP_SES | SUP_BRAU | SUP_80211N;
1129 if (!nvram_match("t_fix1", (char *)name)) {
1130 nvram_set("lan_ifnames", "vlan0 eth1");
1131 nvram_set("wan_ifnameX", "vlan1");
1132 nvram_set("wl_ifname", "eth1");
1134 break;
1135 case MODEL_RTN12B1:
1136 mfr = "Asus";
1137 name = "RT-N12 B1";
1138 features = SUP_80211N;
1139 if (!nvram_match("t_fix1", (char *)name)) {
1140 nvram_set("lan_ifnames", "vlan0 eth1");
1141 nvram_set("wan_ifnameX", "vlan1");
1142 nvram_set("wl_ifname", "eth1");
1144 break;
1145 case MODEL_RTN15U:
1146 mfr = "Asus";
1147 name = "RT-N15U";
1148 features = SUP_SES | SUP_80211N | SUP_1000ET;
1149 #ifdef TCONFIG_USB
1150 nvram_set("usb_uhci", "-1");
1151 #endif
1152 if (!nvram_match("t_fix1", (char *)name)) {
1153 nvram_set("lan_ifnames", "vlan1 eth1");
1154 nvram_set("wan_iface", "vlan2");
1155 nvram_set("wan_ifname", "vlan2");
1156 nvram_set("wan_ifnameX", "vlan2");
1157 nvram_set("wan_ifnames", "vlan2");
1158 nvram_set("wl_ifname", "eth1");
1160 break;
1161 case MODEL_RTN16:
1162 mfr = "Asus";
1163 name = "RT-N16";
1164 features = SUP_SES | SUP_80211N | SUP_1000ET;
1165 #ifdef TCONFIG_USB
1166 nvram_set("usb_uhci", "-1");
1167 #endif
1168 if (!nvram_match("t_fix1", (char *)name)) {
1169 nvram_set("lan_ifnames", "vlan1 eth1");
1170 nvram_set("wan_ifnameX", "vlan2");
1171 nvram_set("wl_ifname", "eth1");
1172 nvram_set("vlan_enable", "1");
1174 break;
1175 case MODEL_RTN53:
1176 case MODEL_RTN53A1:
1177 mfr = "Asus";
1178 name = nvram_match("boardrev", "0x1446") ? "RT-N53 A1" : "RT-N53";
1179 features = SUP_SES | SUP_80211N;
1180 #if defined(LINUX26) && defined(TCONFIG_USBAP)
1181 if (nvram_get_int("usb_storage") == 1) nvram_set("usb_storage", "-1");
1182 #endif
1183 if (!nvram_match("t_fix1", (char *)name)) {
1184 #ifdef TCONFIG_USBAP
1185 nvram_set("wl1_hwaddr", nvram_safe_get("0:macaddr"));
1186 nvram_set("ehciirqt", "3");
1187 nvram_set("qtdc_pid", "48407");
1188 nvram_set("qtdc_vid", "2652");
1189 nvram_set("qtdc0_ep", "4");
1190 nvram_set("qtdc0_sz", "0");
1191 nvram_set("qtdc1_ep", "18");
1192 nvram_set("qtdc1_sz", "10");
1193 nvram_set("lan_ifnames", "vlan2 eth1 eth2");
1194 nvram_set("landevs", "vlan2 wl0 wl1");
1195 nvram_set("wl1_ifname", "eth2");
1196 #else
1197 nvram_set("lan_ifnames", "vlan2 eth1");
1198 nvram_set("landevs", "vlan2 wl0");
1199 #endif
1200 nvram_set("lan_ifname", "br0");
1201 nvram_set("wl_ifname", "eth1");
1202 nvram_set("wl0_ifname", "eth1");
1203 nvram_set("wan_ifnameX", "vlan1");
1204 nvram_set("wandevs", "vlan1");
1205 nvram_unset("vlan0ports");
1207 break;
1208 case MODEL_RTN66U:
1209 mfr = "Asus";
1210 #ifdef TCONFIG_AC66U
1211 name = "RT-AC66U";
1212 features = SUP_SES | SUP_80211N | SUP_1000ET | SUP_80211AC;
1213 #else
1214 name = "RT-N66U";
1215 features = SUP_SES | SUP_80211N | SUP_1000ET;
1216 #if defined(LINUX26) && defined(TCONFIG_MICROSD)
1217 if (nvram_get_int("usb_mmc") == -1) nvram_set("usb_mmc", "1");
1218 #endif
1219 #endif
1221 #ifdef TCONFIG_USB
1222 nvram_set("usb_uhci", "-1");
1223 #endif
1224 if (!nvram_match("t_fix1", (char *)name)) {
1225 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1226 nvram_set("wan_ifnameX", "vlan2");
1227 nvram_set("wl_ifnames", "eth1 eth2");
1228 #ifdef TCONFIG_AC66U
1229 nvram_set("wl_ifname", "eth1");
1230 nvram_set("wl0_ifname", "eth1");
1231 nvram_set("wl1_ifname", "eth2");
1232 #endif
1233 nvram_set("landevs", "vlan1 wl0 wl1");
1234 nvram_set("wandevs", "vlan2");
1235 #ifndef TCONFIG_AC66U
1236 #if defined(LINUX26) && defined(TCONFIG_USB)
1237 nvram_set("usb_noled", "1-1.4"); /* SD/MMC Card */
1238 #endif
1239 #else
1240 nvram_set("wl1_bw_cap","7");
1241 nvram_set("wl1_chanspec","36/80");
1242 nvram_set("wl0_bw_cap","3");
1243 nvram_set("wl0_chanspec","1l");
1244 nvram_set("blink_5g_interface","eth2");
1246 // fix WL mac`s
1247 strcpy(s, nvram_safe_get("et0macaddr"));
1248 inc_mac(s, +2);
1249 nvram_set("wl0_hwaddr", s);
1250 inc_mac(s, +1);
1251 nvram_set("wl1_hwaddr", s);
1253 // bcm4360ac_defaults
1254 nvram_set("pci/2/1/aa2g", "0");
1255 nvram_set("pci/2/1/aa5g", "7");
1256 nvram_set("pci/2/1/aga0", "71");
1257 nvram_set("pci/2/1/aga1", "71");
1258 nvram_set("pci/2/1/aga2", "71");
1259 nvram_set("pci/2/1/agbg0", "133");
1260 nvram_set("pci/2/1/agbg1", "133");
1261 nvram_set("pci/2/1/agbg2", "133");
1262 nvram_set("pci/2/1/antswitch", "0");
1263 nvram_set("pci/2/1/cckbw202gpo", "0");
1264 nvram_set("pci/2/1/cckbw20ul2gpo", "0");
1265 nvram_set("pci/2/1/dot11agofdmhrbw202gpo", "0");
1266 nvram_set("pci/2/1/femctrl", "3");
1267 nvram_set("pci/2/1/papdcap2g", "0");
1268 nvram_set("pci/2/1/tworangetssi2g", "0");
1269 nvram_set("pci/2/1/pdgain2g", "4");
1270 nvram_set("pci/2/1/epagain2g", "0");
1271 nvram_set("pci/2/1/tssiposslope2g", "1");
1272 nvram_set("pci/2/1/gainctrlsph", "0");
1273 nvram_set("pci/2/1/papdcap5g", "0");
1274 nvram_set("pci/2/1/tworangetssi5g", "0");
1275 nvram_set("pci/2/1/pdgain5g", "4");
1276 nvram_set("pci/2/1/epagain5g", "0");
1277 nvram_set("pci/2/1/tssiposslope5g", "1");
1278 nvram_set("pci/2/1/maxp2ga0", "76");
1279 nvram_set("pci/2/1/maxp2ga1", "76");
1280 nvram_set("pci/2/1/maxp2ga2", "76");
1281 nvram_set("pci/2/1/mcsbw202gpo", "0");
1282 nvram_set("pci/2/1/mcsbw402gpo", "0");
1283 nvram_set("pci/2/1/measpower", "0x7f");
1284 nvram_set("pci/2/1/measpower1", "0x7f");
1285 nvram_set("pci/2/1/measpower2", "0x7f");
1286 nvram_set("pci/2/1/noiselvl2ga0", "31");
1287 nvram_set("pci/2/1/noiselvl2ga1", "31");
1288 nvram_set("pci/2/1/noiselvl2ga2", "31");
1289 nvram_set("pci/2/1/noiselvl5gha0", "31");
1290 nvram_set("pci/2/1/noiselvl5gha1", "31");
1291 nvram_set("pci/2/1/noiselvl5gha2", "31");
1292 nvram_set("pci/2/1/noiselvl5gla0", "31");
1293 nvram_set("pci/2/1/noiselvl5gla1", "31");
1294 nvram_set("pci/2/1/noiselvl5gla2", "31");
1295 nvram_set("pci/2/1/noiselvl5gma0", "31");
1296 nvram_set("pci/2/1/noiselvl5gma1", "31");
1297 nvram_set("pci/2/1/noiselvl5gma2", "31");
1298 nvram_set("pci/2/1/noiselvl5gua0", "31");
1299 nvram_set("pci/2/1/noiselvl5gua1", "31");
1300 nvram_set("pci/2/1/noiselvl5gua2", "31");
1301 nvram_set("pci/2/1/ofdmlrbw202gpo", "0");
1302 nvram_set("pci/2/1/pa2ga0", "0xfe72,0x14c0,0xfac7");
1303 nvram_set("pci/2/1/pa2ga1", "0xfe80,0x1472,0xfabc");
1304 nvram_set("pci/2/1/pa2ga2", "0xfe82,0x14bf,0xfad9");
1305 nvram_set("pci/2/1/pcieingress_war", "15");
1306 nvram_set("pci/2/1/phycal_tempdelta", "255");
1307 nvram_set("pci/2/1/rawtempsense", "0x1ff");
1308 nvram_set("pci/2/1/rxchain", "7");
1309 nvram_set("pci/2/1/rxgainerr2g", "0xffff");
1310 nvram_set("pci/2/1/rxgainerr5g", "0xffff,0xffff,0xffff,0xffff");
1311 nvram_set("pci/2/1/rxgains2gelnagaina0", "0");
1312 nvram_set("pci/2/1/rxgains2gelnagaina1", "0");
1313 nvram_set("pci/2/1/rxgains2gelnagaina2", "0");
1314 nvram_set("pci/2/1/rxgains2gtrelnabypa0", "0");
1315 nvram_set("pci/2/1/rxgains2gtrelnabypa1", "0");
1316 nvram_set("pci/2/1/rxgains2gtrelnabypa2", "0");
1317 nvram_set("pci/2/1/rxgains2gtrisoa0", "0");
1318 nvram_set("pci/2/1/rxgains2gtrisoa1", "0");
1319 nvram_set("pci/2/1/rxgains2gtrisoa2", "0");
1320 nvram_set("pci/2/1/sar2g", "18");
1321 nvram_set("pci/2/1/sar5g", "15");
1322 nvram_set("pci/2/1/sromrev", "11");
1323 nvram_set("pci/2/1/subband5gver", "0x4");
1324 nvram_set("pci/2/1/tempcorrx", "0x3f");
1325 nvram_set("pci/2/1/tempoffset", "255");
1326 nvram_set("pci/2/1/temps_hysteresis", "15");
1327 nvram_set("pci/2/1/temps_period", "15");
1328 nvram_set("pci/2/1/tempsense_option", "0x3");
1329 nvram_set("pci/2/1/tempsense_slope", "0xff");
1330 nvram_set("pci/2/1/tempthresh", "255");
1331 nvram_set("pci/2/1/txchain", "7");
1332 nvram_set("pci/2/1/ledbh0", "2");
1333 nvram_set("pci/2/1/ledbh1", "5");
1334 nvram_set("pci/2/1/ledbh2", "4");
1335 nvram_set("pci/2/1/ledbh3", "11");
1336 nvram_set("pci/2/1/ledbh10", "7");
1338 //force EU country for eth2
1339 nvram_set("pci/2/1/ccode", "EU");
1340 #endif // TCONFIG_AC66U
1342 break;
1343 #ifdef CONFIG_BCMWL6
1344 case MODEL_W1800R:
1345 mfr = "Tenda";
1346 name = "W1800R";
1347 features = SUP_SES | SUP_80211N | SUP_1000ET | SUP_80211AC;
1348 #ifdef TCONFIG_USB
1349 nvram_set("usb_uhci", "-1");
1350 #endif
1351 if (!nvram_match("t_fix1", (char *)name)) {
1352 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1353 nvram_set("wan_ifnameX", "vlan2");
1354 nvram_set("wl_ifnames", "eth1 eth2");
1355 nvram_set("wl_ifname", "eth1");
1356 nvram_set("wl0_ifname", "eth2");
1357 nvram_set("wl1_ifname", "eth1");
1358 nvram_set("wl0_bw_cap","7");
1359 nvram_set("wl0_chanspec","36/80");
1360 nvram_set("wl1_bw_cap","3");
1361 nvram_set("wl1_chanspec","1l");
1362 nvram_set("blink_5g_interface","eth1");
1363 //nvram_set("landevs", "vlan1 wl0 wl1");
1364 //nvram_set("wandevs", "vlan2");
1366 // fix WL mac`s
1367 strcpy(s, nvram_safe_get("et0macaddr"));
1368 nvram_set("wl0_hwaddr", nvram_safe_get("0:macaddr"));
1369 nvram_set("wl1_hwaddr", nvram_safe_get("1:macaddr"));
1371 break;
1372 #endif // CONFIG_BCMWL6
1373 case MODEL_WNR3500L:
1374 mfr = "Netgear";
1375 name = "WNR3500L/U/v2";
1376 features = SUP_SES | SUP_AOSS_LED | SUP_80211N | SUP_1000ET;
1377 if (!nvram_match("t_fix1", (char *)name)) {
1378 nvram_set("sromrev", "3");
1379 nvram_set("lan_ifnames", "vlan1 eth1");
1380 nvram_set("wan_ifnameX", "vlan2");
1381 nvram_set("wl_ifname", "eth1");
1383 break;
1384 case MODEL_WNR3500LV2:
1385 mfr = "Netgear";
1386 name = "WNR3500L v2";
1387 features = SUP_SES | SUP_AOSS_LED | SUP_80211N | SUP_1000ET;
1388 if (!nvram_match("t_fix1", (char *)name)) {
1389 nvram_set("lan_ifnames", "vlan1 eth1");
1390 nvram_set("wan_ifnameX", "vlan2");
1391 nvram_set("wl_ifname", "eth1");
1393 break;
1394 case MODEL_WNR2000v2:
1395 mfr = "Netgear";
1396 name = "WNR2000 v2";
1397 features = SUP_SES | SUP_AOSS_LED | SUP_80211N;
1398 if (!nvram_match("t_fix1", (char *)name)) {
1399 nvram_set("lan_ifnames", "vlan0 eth1");
1400 nvram_set("wan_ifnameX", "vlan1");
1401 nvram_set("wl_ifname", "eth1");
1403 break;
1404 case MODEL_F7D3301:
1405 case MODEL_F7D3302:
1406 case MODEL_F7D4301:
1407 case MODEL_F7D4302:
1408 case MODEL_F5D8235v3:
1409 mfr = "Belkin";
1410 features = SUP_SES | SUP_80211N;
1411 switch (model) {
1412 case MODEL_F7D3301:
1413 name = "Share Max N300 (F7D3301/F7D7301) v1";
1414 break;
1415 case MODEL_F7D3302:
1416 name = "Share N300 (F7D3302/F7D7302) v1";
1417 break;
1418 case MODEL_F7D4301:
1419 name = "Play Max / N600 HD (F7D4301/F7D8301) v1";
1420 break;
1421 case MODEL_F7D4302:
1422 name = "Play N600 (F7D4302/F7D8302) v1";
1423 break;
1424 case MODEL_F5D8235v3:
1425 name = "N F5D8235-4 v3";
1426 break;
1428 #ifdef TCONFIG_USB
1429 nvram_set("usb_uhci", "-1");
1430 #endif
1431 if (!nvram_match("t_fix1", (char *)name)) {
1432 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1433 nvram_set("wan_ifnameX", "vlan2");
1434 nvram_set("landevs", "vlan1 wl0 wl1");
1435 nvram_set("wandevs", "vlan2");
1437 break;
1438 case MODEL_E900:
1439 case MODEL_E1500:
1440 mfr = "Linksys";
1441 name = nvram_safe_get("boot_hw_model");
1442 ver = nvram_safe_get("boot_hw_ver");
1443 features = SUP_SES | SUP_80211N;
1444 if (!nvram_match("t_fix1", (char *)name)) {
1445 nvram_set("lan_ifnames", "vlan1 eth1");
1446 nvram_set("wan_ifnameX", "vlan2");
1447 nvram_set("wl_ifname", "eth1");
1449 break;
1450 case MODEL_E1550:
1451 mfr = "Linksys";
1452 name = nvram_safe_get("boot_hw_model");
1453 ver = nvram_safe_get("boot_hw_ver");
1454 features = SUP_SES | SUP_80211N;
1455 #ifdef TCONFIG_USB
1456 nvram_set("usb_uhci", "-1");
1457 #endif
1458 if (!nvram_match("t_fix1", (char *)name)) {
1459 nvram_set("lan_ifnames", "vlan1 eth1");
1460 nvram_set("wan_ifnameX", "vlan2");
1461 nvram_set("wl_ifname", "eth1");
1463 break;
1464 case MODEL_E2500:
1465 mfr = "Linksys";
1466 name = nvram_safe_get("boot_hw_model");
1467 ver = nvram_safe_get("boot_hw_ver");
1468 features = SUP_SES | SUP_80211N;
1469 #if defined(LINUX26) && defined(TCONFIG_USBAP)
1470 if (nvram_get_int("usb_storage") == 1) nvram_set("usb_storage", "-1");
1471 #endif
1472 if (!nvram_match("t_fix1", (char *)name)) {
1473 #ifdef TCONFIG_USBAP
1474 nvram_set("wl1_hwaddr", nvram_safe_get("0:macaddr"));
1475 nvram_set("ehciirqt", "3");
1476 nvram_set("qtdc_pid", "48407");
1477 nvram_set("qtdc_vid", "2652");
1478 nvram_set("qtdc0_ep", "4");
1479 nvram_set("qtdc0_sz", "0");
1480 nvram_set("qtdc1_ep", "18");
1481 nvram_set("qtdc1_sz", "10");
1482 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1483 nvram_set("landevs", "vlan1 wl0 wl1");
1484 nvram_set("wl0_ifname", "eth1");
1485 nvram_set("wl1_ifname", "eth2");
1486 #else
1487 nvram_set("lan_ifnames", "vlan1 eth1");
1488 nvram_set("landevs", "vlan1 wl0");
1489 #endif
1490 nvram_set("wan_ifnameX", "vlan2");
1491 nvram_set("wl_ifname", "eth1");
1494 break;
1495 case MODEL_E3200:
1496 mfr = "Linksys";
1497 name = nvram_safe_get("boot_hw_model");
1498 ver = nvram_safe_get("boot_hw_ver");
1499 features = SUP_SES | SUP_80211N | SUP_1000ET;
1500 #ifdef TCONFIG_USB
1501 nvram_set("usb_uhci", "-1");
1502 #endif
1503 if (!nvram_match("t_fix1", (char *)name)) {
1504 #ifdef TCONFIG_USBAP
1505 nvram_set("wl1_hwaddr", nvram_safe_get("usb/0xBD17/macaddr"));
1506 nvram_set("ehciirqt", "3");
1507 nvram_set("qtdc_pid", "48407");
1508 nvram_set("qtdc_vid", "2652");
1509 nvram_set("qtdc0_ep", "4");
1510 nvram_set("qtdc0_sz", "0");
1511 nvram_set("qtdc1_ep", "18");
1512 nvram_set("qtdc1_sz", "10");
1513 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1514 nvram_set("landevs", "vlan1 wl0 wl1");
1515 nvram_set("wl0_ifname", "eth1");
1516 nvram_set("wl1_ifname", "eth2");
1517 #else
1518 nvram_set("lan_ifnames", "vlan1 eth1");
1519 nvram_set("landevs", "vlan1 wl0");
1520 #endif
1521 nvram_set("wl_ifname", "eth1");
1522 nvram_set("wan_ifnameX", "vlan2");
1524 break;
1525 case MODEL_E1000v2:
1526 case MODEL_WRT160Nv3:
1527 // same as M10, M20, WRT310Nv2, E1000v1
1528 mfr = "Linksys";
1529 name = nvram_safe_get("boot_hw_model");
1530 ver = nvram_safe_get("boot_hw_ver");
1531 if (nvram_match("boot_hw_model", "E100")){
1532 name = "E1000";
1534 if (nvram_match("boot_hw_model", "M10") || nvram_match("boot_hw_model", "M20")){
1535 mfr = "Cisco";
1537 features = SUP_SES | SUP_80211N | SUP_WHAM_LED;
1538 if (!nvram_match("t_fix1", (char *)name)) {
1539 nvram_set("lan_ifnames", "vlan1 eth1");
1540 nvram_set("wan_ifnameX", "vlan2");
1541 nvram_set("wl_ifname", "eth1");
1543 break;
1544 case MODEL_WRT320N:
1545 mfr = "Linksys";
1546 name = nvram_match("boardrev", "0x1307") ? "E2000" : "WRT320N";
1547 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1548 if (!nvram_match("t_fix1", (char *)name)) {
1549 nvram_set("lan_ifnames", "vlan1 eth1");
1550 nvram_set("wan_ifnameX", "vlan2");
1551 nvram_set("wl_ifname", "eth1");
1553 break;
1554 case MODEL_WRT610Nv2:
1555 mfr = "Linksys";
1556 name = nvram_match("boot_hw_model", "E300") ? "E3000" : "WRT610N v2";
1557 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1558 #ifdef TCONFIG_USB
1559 nvram_set("usb_uhci", "-1");
1560 #endif
1561 if (!nvram_match("t_fix1", (char *)name)) {
1562 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1563 nvram_set("wan_ifnameX", "vlan2");
1564 nvram_set("wl_ifname", "eth1");
1566 break;
1567 case MODEL_E4200:
1568 mfr = "Linksys";
1569 name = "E4200 v1";
1570 features = SUP_SES | SUP_80211N | SUP_1000ET;
1571 #ifdef TCONFIG_USB
1572 nvram_set("usb_uhci", "-1");
1573 #endif
1574 if (!nvram_match("t_fix1", (char *)name)) {
1575 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1576 nvram_set("wan_ifnameX", "vlan2");
1577 nvram_set("wl_ifname", "eth1");
1579 break;
1580 #endif // CONFIG_BCMWL5
1582 case MODEL_WL330GE:
1583 mfr = "Asus";
1584 name = "WL-330gE";
1585 // The 330gE has only one wired port which can act either as WAN or LAN.
1586 // Failsafe mode is to have it start as a LAN port so you can get an IP
1587 // address via DHCP and access the router config page.
1588 if (!nvram_match("t_fix1", (char *)name)) {
1589 nvram_set("wl_ifname", "eth1");
1590 nvram_set("lan_ifnames", "eth1");
1591 nvram_set("wan_ifnameX", "eth0");
1592 nvram_set("wan_islan", "1");
1593 nvram_set("wan_proto", "disabled");
1595 break;
1596 case MODEL_WL500GPv2:
1597 mfr = "Asus";
1598 name = "WL-500gP v2";
1599 features = SUP_SES;
1600 #ifdef TCONFIG_USB
1601 nvram_set("usb_uhci", "-1");
1602 #endif
1603 break;
1604 case MODEL_WL520GU:
1605 mfr = "Asus";
1606 name = "WL-520GU";
1607 features = SUP_SES;
1608 #ifdef TCONFIG_USB
1609 nvram_set("usb_uhci", "-1");
1610 #endif
1611 break;
1612 case MODEL_WL500GD:
1613 mfr = "Asus";
1614 name = "WL-500g Deluxe";
1615 // features = SUP_SES;
1616 #ifdef TCONFIG_USB
1617 nvram_set("usb_ohci", "-1");
1618 #endif
1619 if (!nvram_match("t_fix1", (char *)name)) {
1620 nvram_set("wl_ifname", "eth1");
1621 nvram_set("lan_ifnames", "vlan0 eth1");
1622 nvram_set("wan_ifnameX", "vlan1");
1623 nvram_unset("wl0gpio0");
1625 break;
1626 case MODEL_DIR320:
1627 mfr = "D-Link";
1628 name = "DIR-320";
1629 features = SUP_SES;
1630 if (!nvram_match("t_fix1", (char *)name)) {
1631 nvram_set("wan_ifnameX", "vlan1");
1632 nvram_set("wl_ifname", "eth1");
1634 break;
1635 case MODEL_H618B:
1636 mfr = "ZTE";
1637 name = "ZXV10 H618B";
1638 features = SUP_SES | SUP_AOSS_LED;
1639 break;
1640 case MODEL_WL1600GL:
1641 mfr = "Ovislink";
1642 name = "WL1600GL";
1643 features = SUP_SES;
1644 break;
1645 #endif // WL_BSS_INFO_VERSION >= 108
1646 case MODEL_WZRG300N:
1647 mfr = "Buffalo";
1648 name = "WZR-G300N";
1649 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_80211N;
1650 break;
1651 case MODEL_WRT160Nv1:
1652 case MODEL_WRT300N:
1653 mfr = "Linksys";
1654 name = (model == MODEL_WRT300N) ? "WRT300N v1" : "WRT160N v1";
1655 features = SUP_SES | SUP_80211N;
1656 if (!nvram_match("t_fix1", (char *)name)) {
1657 nvram_set("wan_ifnameX", "eth1");
1658 nvram_set("lan_ifnames", "eth0 eth2");
1660 break;
1661 case MODEL_WRT310Nv1:
1662 mfr = "Linksys";
1663 name = "WRT310N v1";
1664 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1665 if (!nvram_match("t_fix1", (char *)name)) {
1666 nvram_set("lan_ifnames", "vlan1 eth1");
1667 nvram_set("wan_ifnameX", "vlan2");
1668 nvram_set("wl_ifname", "eth1");
1670 break;
1673 if (name) {
1674 nvram_set("t_fix1", name);
1675 if (ver && strcmp(ver, "")) {
1676 sprintf(s, "%s %s v%s", mfr, name, ver);
1677 } else {
1678 sprintf(s, "%s %s", mfr, name);
1681 else {
1682 snprintf(s, sizeof(s), "%s %d/%s/%s/%s/%s", mfr, check_hw_type(),
1683 nvram_safe_get("boardtype"), nvram_safe_get("boardnum"), nvram_safe_get("boardrev"), nvram_safe_get("boardflags"));
1684 s[64] = 0;
1686 nvram_set("t_model_name", s);
1688 nvram_set("pa0maxpwr", "400"); // allow Tx power up tp 400 mW, needed for ND only
1690 sprintf(s, "0x%lX", features);
1691 nvram_set("t_features", s);
1694 note: set wan_ifnameX if wan_ifname needs to be overriden
1697 if (nvram_is_empty("wan_ifnameX")) {
1698 #if 1
1699 nvram_set("wan_ifnameX", ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
1700 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1");
1701 #else
1702 p = nvram_safe_get("wan_ifname");
1703 if ((*p == 0) || (nvram_match("wl_ifname", p))) {
1704 p = ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
1705 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1";
1707 nvram_set("wan_ifnameX", p);
1708 #endif
1711 //!!TB - do not force country code here to allow nvram override
1712 //nvram_set("wl_country", "JP");
1713 //nvram_set("wl_country_code", "JP");
1714 nvram_set("wan_get_dns", "");
1715 nvram_set("wan_get_domain", "");
1716 nvram_set("ppp_get_ip", "");
1717 nvram_set("action_service", "");
1718 nvram_set("jffs2_format", "0");
1719 nvram_set("rrules_radio", "-1");
1720 nvram_unset("https_crt_gen");
1721 nvram_unset("log_wmclear");
1722 #ifdef TCONFIG_IPV6
1723 nvram_set("ipv6_get_dns", "");
1724 #endif
1725 #ifdef TCONFIG_MEDIA_SERVER
1726 nvram_unset("ms_rescan");
1727 #endif
1728 if (nvram_get_int("http_id_gen") == 1) nvram_unset("http_id");
1730 nvram_unset("sch_rboot_last");
1731 nvram_unset("sch_rcon_last");
1732 nvram_unset("sch_c1_last");
1733 nvram_unset("sch_c2_last");
1734 nvram_unset("sch_c3_last");
1736 nvram_set("brau_state", "");
1737 if ((features & SUP_BRAU) == 0) nvram_set("script_brau", "");
1738 if ((features & SUP_SES) == 0) nvram_set("sesx_script", "");
1740 if ((features & SUP_1000ET) == 0) nvram_set("jumbo_frame_enable", "0");
1742 // compatibility with old versions
1743 if (nvram_match("wl_net_mode", "disabled")) {
1744 nvram_set("wl_radio", "0");
1745 nvram_set("wl_net_mode", "mixed");
1748 return 0;
1751 /* Get the special files from nvram and copy them to disc.
1752 * These were files saved with "nvram setfile2nvram <filename>".
1753 * Better hope that they were saved with full pathname.
1755 static void load_files_from_nvram(void)
1757 char *name, *cp;
1758 int ar_loaded = 0;
1759 char buf[NVRAM_SPACE];
1761 if (nvram_getall(buf, sizeof(buf)) != 0)
1762 return;
1764 for (name = buf; *name; name += strlen(name) + 1) {
1765 if (strncmp(name, "FILE:", 5) == 0) { /* This special name marks a file to get. */
1766 if ((cp = strchr(name, '=')) == NULL)
1767 continue;
1768 *cp = 0;
1769 syslog(LOG_INFO, "Loading file '%s' from nvram", name + 5);
1770 nvram_nvram2file(name, name + 5);
1771 if (memcmp(".autorun", cp - 8, 9) == 0)
1772 ++ar_loaded;
1775 /* Start any autorun files that may have been loaded into one of the standard places. */
1776 if (ar_loaded != 0)
1777 run_nvscript(".autorun", NULL, 3);
1780 #if defined(LINUX26) && defined(TCONFIG_USB)
1781 static inline void tune_min_free_kbytes(void)
1783 struct sysinfo info;
1785 memset(&info, 0, sizeof(struct sysinfo));
1786 sysinfo(&info);
1787 if (info.totalram >= 55 * 1024 * 1024) {
1788 // If we have 64MB+ RAM, tune min_free_kbytes
1789 // to reduce page allocation failure errors.
1790 f_write_string("/proc/sys/vm/min_free_kbytes", "8192", 0, 0);
1793 #endif
1795 static void sysinit(void)
1797 static int noconsole = 0;
1798 static const time_t tm = 0;
1799 int hardware;
1800 int i;
1801 DIR *d;
1802 struct dirent *de;
1803 char s[256];
1804 char t[256];
1806 mount("proc", "/proc", "proc", 0, NULL);
1807 mount("tmpfs", "/tmp", "tmpfs", 0, NULL);
1809 #ifdef LINUX26
1810 mount("devfs", "/dev", "tmpfs", MS_MGC_VAL | MS_NOATIME, NULL);
1811 mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3));
1812 mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1));
1813 mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
1814 mkdir("/dev/shm", 0777);
1815 mkdir("/dev/pts", 0777);
1816 mknod("/dev/pts/ptmx", S_IRWXU|S_IFCHR, makedev(5, 2));
1817 mknod("/dev/pts/0", S_IRWXU|S_IFCHR, makedev(136, 0));
1818 mknod("/dev/pts/1", S_IRWXU|S_IFCHR, makedev(136, 1));
1819 mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
1820 #endif
1822 if (console_init()) noconsole = 1;
1824 stime(&tm);
1826 static const char *mkd[] = {
1827 "/tmp/etc", "/tmp/var", "/tmp/home", "/tmp/mnt",
1828 "/tmp/splashd", //!!Victek
1829 "/tmp/share", "/var/webmon", // !!TB
1830 "/var/log", "/var/run", "/var/tmp", "/var/lib", "/var/lib/misc",
1831 "/var/spool", "/var/spool/cron", "/var/spool/cron/crontabs",
1832 "/tmp/var/wwwext", "/tmp/var/wwwext/cgi-bin", // !!TB - CGI support
1833 NULL
1835 umask(0);
1836 for (i = 0; mkd[i]; ++i) {
1837 mkdir(mkd[i], 0755);
1839 mkdir("/var/lock", 0777);
1840 mkdir("/var/tmp/dhcp", 0777);
1841 mkdir("/home/root", 0700);
1842 chmod("/tmp", 0777);
1843 f_write("/etc/hosts", NULL, 0, 0, 0644); // blank
1844 f_write("/etc/fstab", NULL, 0, 0, 0644); // !!TB - blank
1845 simple_unlock("cron");
1846 simple_unlock("firewall");
1847 simple_unlock("restrictions");
1848 umask(022);
1850 if ((d = opendir("/rom/etc")) != NULL) {
1851 while ((de = readdir(d)) != NULL) {
1852 if (de->d_name[0] == '.') continue;
1853 snprintf(s, sizeof(s), "%s/%s", "/rom/etc", de->d_name);
1854 snprintf(t, sizeof(t), "%s/%s", "/etc", de->d_name);
1855 symlink(s, t);
1857 closedir(d);
1859 symlink("/proc/mounts", "/etc/mtab");
1861 #ifdef TCONFIG_SAMBASRV
1862 if ((d = opendir("/usr/codepages")) != NULL) {
1863 while ((de = readdir(d)) != NULL) {
1864 if (de->d_name[0] == '.') continue;
1865 snprintf(s, sizeof(s), "/usr/codepages/%s", de->d_name);
1866 snprintf(t, sizeof(t), "/usr/share/%s", de->d_name);
1867 symlink(s, t);
1869 closedir(d);
1871 #endif
1873 #ifdef LINUX26
1874 eval("hotplug2", "--coldplug");
1875 start_hotplug2();
1877 static const char *dn[] = {
1878 "null", "zero", "random", "urandom", "full", "ptmx", "nvram",
1879 NULL
1881 for (i = 0; dn[i]; ++i) {
1882 snprintf(s, sizeof(s), "/dev/%s", dn[i]);
1883 chmod(s, 0666);
1885 chmod("/dev/gpio", 0660);
1886 #endif
1888 set_action(ACT_IDLE);
1890 for (i = 0; defenv[i]; ++i) {
1891 putenv(defenv[i]);
1894 if (!noconsole) {
1895 printf("\n\nHit ENTER for console...\n\n");
1896 run_shell(1, 0);
1899 check_bootnv();
1901 #ifdef TCONFIG_IPV6
1902 // disable IPv6 by default on all interfaces
1903 f_write_string("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1", 0, 0);
1904 #endif
1906 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++) {
1907 signal(fatalsigs[i], handle_fatalsigs);
1909 signal(SIGCHLD, handle_reap);
1911 #ifdef CONFIG_BCMWL5
1912 // ctf must be loaded prior to any other modules
1913 if (nvram_invmatch("ctf_disable", "1"))
1914 modprobe("ctf");
1915 #endif
1917 #ifdef TCONFIG_EMF
1918 modprobe("emf");
1919 modprobe("igs");
1920 #endif
1922 switch (hardware = check_hw_type()) {
1923 case HW_BCM4785:
1924 modprobe("bcm57xx");
1925 break;
1926 default:
1927 modprobe("et");
1928 break;
1931 load_wl();
1933 config_loopback();
1935 eval("nvram", "defaults", "--initcheck");
1936 init_nvram();
1938 // set the packet size
1939 if (nvram_get_int("jumbo_frame_enable")) {
1940 // only set the size here - 'enable' flag is set by the driver
1941 // eval("et", "robowr", "0x40", "0x01", "0x1F"); // (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)
1942 eval("et", "robowr", "0x40", "0x05", nvram_safe_get("jumbo_frame_size"));
1945 klogctl(8, NULL, nvram_get_int("console_loglevel"));
1947 #if defined(LINUX26) && defined(TCONFIG_USB)
1948 tune_min_free_kbytes();
1949 #endif
1950 setup_conntrack();
1951 set_host_domain_name();
1953 set_tz();
1955 eval("buttons");
1957 #ifdef CONFIG_BCMWL6
1958 eval("blink_5g");
1959 #endif
1961 if (!noconsole) xstart("console");
1963 i = nvram_get_int("sesx_led");
1964 led(LED_AMBER, (i & 1) != 0);
1965 led(LED_WHITE, (i & 2) != 0);
1966 led(LED_AOSS, (i & 4) != 0);
1967 led(LED_BRIDGE, (i & 8) != 0);
1968 led(LED_DIAG, 1);
1971 int init_main(int argc, char *argv[])
1973 int state, i;
1974 sigset_t sigset;
1976 // AB - failsafe?
1977 nvram_unset("debug_rc_svc");
1979 sysinit();
1981 sigemptyset(&sigset);
1982 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++) {
1983 sigaddset(&sigset, initsigs[i]);
1985 sigprocmask(SIG_BLOCK, &sigset, NULL);
1987 #if defined(DEBUG_NOISY)
1988 nvram_set("debug_logeval", "1");
1989 nvram_set("debug_cprintf", "1");
1990 nvram_set("debug_cprintf_file", "1");
1991 nvram_set("debug_ddns", "1");
1992 #endif
1994 start_jffs2();
1996 state = SIGUSR2; /* START */
1998 for (;;) {
1999 TRACE_PT("main loop signal/state=%d\n", state);
2001 switch (state) {
2002 case SIGUSR1: /* USER1: service handler */
2003 exec_service();
2004 break;
2006 case SIGHUP: /* RESTART */
2007 case SIGINT: /* STOP */
2008 case SIGQUIT: /* HALT */
2009 case SIGTERM: /* REBOOT */
2010 led(LED_DIAG, 1);
2011 unlink("/var/notice/sysup");
2013 if( nvram_match( "webmon_bkp", "1" ) )
2014 xstart( "/usr/sbin/webmon_bkp", "hourly" ); // make a copy before halt/reboot router
2016 run_nvscript("script_shut", NULL, 10);
2018 stop_services();
2019 stop_wan();
2020 stop_lan();
2021 stop_vlan();
2022 stop_syslog();
2024 if ((state == SIGTERM /* REBOOT */) ||
2025 (state == SIGQUIT /* HALT */)) {
2026 remove_storage_main(1);
2027 stop_usb();
2029 shutdn(state == SIGTERM /* REBOOT */);
2030 exit(0);
2032 if (state == SIGINT /* STOP */) {
2033 break;
2036 // SIGHUP (RESTART) falls through
2038 case SIGUSR2: /* START */
2039 SET_LED(RELEASE_WAN_CONTROL);
2040 start_syslog();
2042 load_files_from_nvram();
2044 int fd = -1;
2045 fd = file_lock("usb"); // hold off automount processing
2046 start_usb();
2048 xstart("/usr/sbin/mymotd", "init");
2049 run_nvscript("script_init", NULL, 2);
2051 file_unlock(fd); // allow to process usb hotplug events
2052 #ifdef TCONFIG_USB
2054 * On RESTART some partitions can stay mounted if they are busy at the moment.
2055 * In that case USB drivers won't unload, and hotplug won't kick off again to
2056 * remount those drives that actually got unmounted. Make sure to remount ALL
2057 * partitions here by simulating hotplug event.
2059 if (state == SIGHUP /* RESTART */)
2060 add_remove_usbhost("-1", 1);
2061 #endif
2063 create_passwd();
2064 start_vlan();
2065 start_lan();
2066 start_arpbind();
2067 start_wan(BOOT);
2068 start_services();
2069 start_wl();
2071 #ifdef CONFIG_BCMWL5
2072 if (wds_enable()) {
2073 /* Restart NAS one more time - for some reason without
2074 * this the new driver doesn't always bring WDS up.
2076 stop_nas();
2077 start_nas();
2079 #endif
2081 syslog(LOG_INFO, "%s: Tomato %s", nvram_safe_get("t_model_name"), tomato_version);
2083 led(LED_DIAG, 0);
2084 notice_set("sysup", "");
2085 break;
2088 chld_reap(0); /* Periodically reap zombies. */
2089 check_services();
2090 sigwait(&sigset, &state);
2093 return 0;
2096 int reboothalt_main(int argc, char *argv[])
2098 int reboot = (strstr(argv[0], "reboot") != NULL);
2099 puts(reboot ? "Rebooting..." : "Shutting down...");
2100 fflush(stdout);
2101 sleep(1);
2102 kill(1, reboot ? SIGTERM : SIGQUIT);
2104 /* In the case we're hung, we'll get stuck and never actually reboot.
2105 * The only way out is to pull power.
2106 * So after 'reset_wait' seconds (default: 20), forcibly crash & restart.
2108 if (fork() == 0) {
2109 int wait = nvram_get_int("reset_wait") ? : 20;
2110 if ((wait < 10) || (wait > 120)) wait = 10;
2112 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
2113 sleep(wait);
2114 puts("Still running... Doing machine reset.");
2115 fflush(stdout);
2116 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
2117 sleep(1);
2118 f_write("/proc/sysrq-trigger", "b", 1, 0 , 0); /* machine reset */
2121 return 0;