3 Copyright 2005, Broadcom Corporation
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.
17 #include <sys/ioctl.h>
18 #include <sys/mount.h>
23 #include <sys/reboot.h>
26 #include <sys/types.h>
28 #include <sys/sysinfo.h>
33 #define SHELL "/bin/sh"
47 static int fatalsigs
[] = {
58 static int initsigs
[] = {
67 static char *defenv
[] = {
70 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
76 static int noconsole
= 0;
77 static volatile int state
= INIT
;
78 static volatile int signaled
= -1;
81 /* Set terminal settings to reasonable defaults */
82 static void set_term(int fd
)
88 /* set control chars */
89 tty
.c_cc
[VINTR
] = 3; /* C-c */
90 tty
.c_cc
[VQUIT
] = 28; /* C-\ */
91 tty
.c_cc
[VERASE
] = 127; /* C-? */
92 tty
.c_cc
[VKILL
] = 21; /* C-u */
93 tty
.c_cc
[VEOF
] = 4; /* C-d */
94 tty
.c_cc
[VSTART
] = 17; /* C-q */
95 tty
.c_cc
[VSTOP
] = 19; /* C-s */
96 tty
.c_cc
[VSUSP
] = 26; /* C-z */
98 /* use line dicipline 0 */
101 /* Make it be sane */
102 tty
.c_cflag
&= CBAUD
|CBAUDEX
|CSIZE
|CSTOPB
|PARENB
|PARODD
;
103 tty
.c_cflag
|= CREAD
|HUPCL
|CLOCAL
;
107 tty
.c_iflag
= ICRNL
| IXON
| IXOFF
;
110 tty
.c_oflag
= OPOST
| ONLCR
;
114 ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOKE
| IEXTEN
;
116 tcsetattr(fd
, TCSANOW
, &tty
);
119 static int console_init(void)
124 ioctl(0, TIOCNOTTY
, 0);
131 if ((fd
= open(_PATH_CONSOLE
, O_RDWR
)) < 0) {
132 /* Avoid debug messages is redirected to socket packet if no exist a UART chip, added by honor, 2003-12-04 */
133 open("/dev/null", O_RDONLY
);
134 open("/dev/null", O_WRONLY
);
135 open("/dev/null", O_WRONLY
);
136 perror(_PATH_CONSOLE
);
143 ioctl(0, TIOCSCTTY
, 1);
144 tcsetpgrp(0, getpgrp());
151 * Waits for a file descriptor to change status or unblocked signal
152 * @param fd file descriptor
153 * @param timeout seconds to wait before timing out or 0 for no timeout
154 * @return 1 if descriptor changed status or 0 if timed out or -1 on error
156 static int waitfor(int fd
, int timeout
)
159 struct timeval tv
= { timeout
, 0 };
163 return select(fd
+ 1, &rfds
, NULL
, NULL
, (timeout
> 0) ? &tv
: NULL
);
166 static pid_t
run_shell(int timeout
, int nowait
)
171 /* Wait for user input */
172 if (waitfor(STDIN_FILENO
, timeout
) <= 0) return 0;
174 switch (pid
= fork()) {
179 /* Reset signal handlers set for parent process */
180 for (sig
= 0; sig
< (_NSIG
-1); sig
++)
181 signal(sig
, SIG_DFL
);
185 printf("\n\nTomato %s\n\n", tomato_version
);
187 /* Now run it. The new program will take over this PID,
188 * so nothing further in init.c should be run. */
189 execve(SHELL
, (char *[]) { SHELL
, NULL
}, defenv
);
191 /* We're still here? Some error happened. */
199 waitpid(pid
, NULL
, 0);
205 static void shutdn(int rb
)
211 _dprintf("shutdn rb=%d\n", rb
);
214 for (i
= 0; i
< sizeof(fatalsigs
) / sizeof(fatalsigs
[0]); i
++)
215 sigaddset(&ss
, fatalsigs
[i
]);
216 for (i
= 0; i
< sizeof(initsigs
) / sizeof(initsigs
[0]); i
++)
217 sigaddset(&ss
, initsigs
[i
]);
218 sigaddset(&ss
, SIGCHLD
);
219 sigprocmask(SIG_BLOCK
, &ss
, NULL
);
221 for (i
= 30; i
> 0; --i
) {
222 if (((act
= check_action()) == ACT_IDLE
) || (act
== ACT_REBOOT
)) break;
223 cprintf("Busy with %d. Waiting before shutdown... %d\n", act
, i
);
226 set_action(ACT_REBOOT
);
238 umount("/jffs"); // may hang if not
244 for (i
= 4; i
> 0; --i
) {
255 reboot(rb
? RB_AUTOBOOT
: RB_HALT_SYSTEM
);
262 static void handle_fatalsigs(int sig
)
264 _dprintf("fatal sig=%d\n", sig
);
268 void handle_reap(int sig
)
270 while (waitpid(-1, NULL
, WNOHANG
) > 0) {
275 static void handle_initsigs(int sig
)
277 // TRACE_PT("sig=%d state=%d, signaled=%d\n", sig, state, signaled);
301 static int check_nv(const char *name
, const char *value
)
304 if (!nvram_match("manual_boot_nv", "1")) {
305 if (((p
= nvram_get(name
)) == NULL
) || (strcmp(p
, value
) != 0)) {
306 // cprintf("Error: Critical variable %s is invalid. Resetting.\n", name);
307 nvram_set(name
, value
);
314 static int find_dir320_mac_addr()
318 int i
, part
, size
, found
= 0;
320 if (!mtd_getinfo("board_data", &part
, &size
)) return 0;
321 sprintf(s
, MTD_DEV(%dro
), part
);
325 buffer
= malloc(size
);
326 memset(buffer
, 0, size
);
327 fread(buffer
, size
, 1, fp
);
328 if (!memcmp(buffer
, "RGCFG1", 6)) {
329 for (i
= 6; i
< size
- 24; i
++) {
330 if (!memcmp(buffer
+ i
, "lanmac=", 7)) {
331 memcpy(s
, buffer
+ i
+ 7, 17);
333 nvram_set("il0macaddr", s
);
335 else if (!memcmp(buffer
+ i
, "wanmac=", 7)) {
336 memcpy(s
, buffer
+ i
+ 7, 17);
338 nvram_set("et0macaddr", s
);
350 static void check_bootnv(void)
361 dirty
|= check_nv("vlan0hwname", "et0");
362 dirty
|= check_nv("vlan1hwname", "et0");
365 dirty
|= check_nv("sdram_init", "0x0009"); // 32MB; defaults: 0x000b, 0x0009
366 if (nvram_match("vlan1ports", "0 5u")) // default: 0 5u
367 dirty
|= check_nv("vlan1ports", "0 5");
370 /* fix WL500W mac adresses for WAN port */
371 if (nvram_match("et1macaddr", "00:90:4c:a1:00:2d"))
372 dirty
|= check_nv("et1macaddr", nvram_get("et0macaddr"));
375 if (nvram_match("vlan1ports", "0 5u")) // default: 0 5u
376 dirty
|= check_nv("vlan1ports", "0 5");
378 case MODEL_WL500GPv2
:
379 if (nvram_match("vlan1ports", "4 5u"))
380 dirty
|= check_nv("vlan1ports", "4 5");
381 else if (nvram_match("vlan1ports", "0 5u")) // 520GU?
382 dirty
|= check_nv("vlan1ports", "0 5");
385 if (nvram_match("vlan1ports", "0 5u"))
386 dirty
|= check_nv("vlan1ports", "0 5");
389 dirty
|= check_nv("vlan0hwname", "et0");
390 dirty
|= check_nv("vlan1hwname", "et0");
391 if (!nvram_get("vlan0ports")) {
392 dirty
|= check_nv("vlan0ports", "1 2 3 4 5*");
393 dirty
|= check_nv("vlan1ports", "0 5");
397 if (strlen(nvram_safe_get("et0macaddr")) == 12) {
398 if (!find_dir320_mac_addr()) {
399 // goofy et0macaddr, make something up
400 nvram_set("et0macaddr", "00:90:4c:c0:00:01");
404 if (nvram_get("vlan2ports") != NULL
) {
405 nvram_unset("vlan2ports");
406 nvram_unset("vlan2hwname");
409 dirty
|= check_nv("vlan1hwname", "et0");
410 if ((nvram_get("vlan1ports") == NULL
) || nvram_match("vlan1ports", "0 5u"))
411 dirty
|= check_nv("vlan1ports", "0 5");
415 dirty
|= check_nv("boardflags", "0x00000710"); // needed to enable USB
416 dirty
|= check_nv("vlan2hwname", "et0");
417 if (nvram_match("vlan1ports", "1 2 3 4 8*") || nvram_match("vlan2ports", "0 8u")) {
418 dirty
|= check_nv("vlan1ports", "4 3 2 1 8*");
419 dirty
|= check_nv("vlan2ports", "0 8");
421 dirty
|= check_nv("ledbh0", "7");
423 case MODEL_WNR2000v2
:
424 if (nvram_match("vlan0ports", "1 2 3 4 5*") || nvram_match("vlan1ports", "0 5u")) {
425 dirty
|= check_nv("vlan0ports", "4 3 2 1 5*");
426 dirty
|= check_nv("vlan1ports", "0 5");
428 if (nvram_match("ledbh5", "2"))
429 dirty
|= check_nv("ledbh5", "8");
432 if (nvram_match("vlan1ports", "4 5u"))
433 dirty
|= check_nv("vlan1ports", "4 5");
436 if (nvram_match("vlan0ports", "0 1 2 3 5*") || nvram_match("vlan1ports", "4 5u")) {
437 dirty
|= check_nv("vlan0ports", "3 2 1 0 5*");
438 dirty
|= check_nv("vlan1ports", "4 5");
442 dirty
|= check_nv("reset_gpio", "5");
443 dirty
|= check_nv("ledbh0", "136");
444 dirty
|= check_nv("ledbh1", "11");
445 /* fall through, same as RT-N16 */
447 dirty
|= check_nv("vlan2hwname", "et0");
448 if (nvram_match("vlan1ports", "1 2 3 4 8*") || nvram_match("vlan2ports", "0 8u")) {
449 dirty
|= check_nv("vlan1ports", "4 3 2 1 8*");
450 dirty
|= check_nv("vlan2ports", "0 8");
453 case MODEL_WRT160Nv3
:
454 if (nvram_match("clkdivsf", "4") && nvram_match("vlan1ports", "1 2 3 4 5*")) {
455 // fix lan port numbering on CSE41, CSE51
456 dirty
|= check_nv("vlan1ports", "4 3 2 1 5*");
462 if (strncmp(nvram_safe_get("pmon_ver"), "CFE", 3) != 0) return;
464 hardware
= check_hw_type();
465 if (!nvram_get("boardtype") ||
466 !nvram_get("boardnum") ||
467 !nvram_get("boardflags") ||
468 !nvram_get("clkfreq") ||
469 !nvram_get("os_flash_addr") ||
470 !nvram_get("dl_ram_addr") ||
471 !nvram_get("os_ram_addr") ||
472 !nvram_get("scratch") ||
473 !nvram_get("et0macaddr") ||
474 ((hardware
!= HW_BCM4704_BCM5325F
) && (!nvram_get("vlan0ports") || !nvram_get("vlan0hwname")))) {
475 cprintf("Unable to find critical settings, erasing NVRAM\n");
480 dirty
|= check_nv("aa0", "3");
481 dirty
|= check_nv("wl0gpio0", "136");
482 dirty
|= check_nv("wl0gpio2", "0");
483 dirty
|= check_nv("wl0gpio3", "0");
484 dirty
|= check_nv("cctl", "0");
485 dirty
|= check_nv("ccode", "0");
489 /* Lower the DDR ram drive strength , the value will be stable for all boards
490 Latency 3 is more stable for all ddr 20050420 by honor */
491 dirty
|= check_nv("sdram_init", "0x010b");
492 dirty
|= check_nv("sdram_config", "0x0062");
493 if (!nvram_match("debug_clkfix", "0")) {
494 dirty
|= check_nv("clkfreq", "216");
497 nvram_set("sdram_ncdl", "0x0");
499 dirty
|= check_nv("pa0itssit", "62");
500 dirty
|= check_nv("pa0b0", "0x15eb");
501 dirty
|= check_nv("pa0b1", "0xfa82");
502 dirty
|= check_nv("pa0b2", "0xfe66");
503 //dirty |= check_nv("pa0maxpwr", "0x4e");
505 case HW_BCM5352E
: // G v4, GS v3, v4
506 dirty
|= check_nv("sdram_init", "0x010b");
507 dirty
|= check_nv("sdram_config", "0x0062");
509 nvram_set("sdram_ncdl", "0x0");
511 dirty
|= check_nv("pa0itssit", "62");
512 dirty
|= check_nv("pa0b0", "0x168b");
513 dirty
|= check_nv("pa0b1", "0xfabf");
514 dirty
|= check_nv("pa0b2", "0xfeaf");
515 //dirty |= check_nv("pa0maxpwr", "0x4e");
516 dirty
|= check_nv("vlan0ports", "3 2 1 0 5*");
519 dirty
|= check_nv("pa0itssit", "62");
520 dirty
|= check_nv("pa0b0", "0x1326");
521 dirty
|= check_nv("pa0b1", "0xFB51");
522 dirty
|= check_nv("pa0b2", "0xFE87");
523 //dirty |= check_nv("pa0maxpwr", "0x4e");
525 case HW_BCM4704_BCM5325F
:
529 dirty
|= check_nv("pa0itssit", "62");
530 dirty
|= check_nv("pa0b0", "0x170c");
531 dirty
|= check_nv("pa0b1", "0xfa24");
532 dirty
|= check_nv("pa0b2", "0xfe70");
533 //dirty |= check_nv("pa0maxpwr", "0x48");
542 REBOOT
: // do a simple reboot
549 static int init_nvram(void)
551 unsigned long features
;
560 sprintf(s
, "%d", model
);
561 nvram_set("t_model", s
);
569 name
= "WRT54G/GS/GL";
570 switch (check_hw_type()) {
572 nvram_set("gpio2", "adm_eecs");
573 nvram_set("gpio3", "adm_eesk");
574 nvram_unset("gpio4");
575 nvram_set("gpio5", "adm_eedi");
576 nvram_set("gpio6", "adm_rc");
579 nvram_unset("gpio2");
580 nvram_unset("gpio3");
581 nvram_unset("gpio4");
582 nvram_unset("gpio5");
583 nvram_unset("gpio6");
586 nvram_set("opo", "0x0008");
587 nvram_set("ag0", "0x02");
590 nvram_set("gpio2", "ses_led");
591 nvram_set("gpio3", "ses_led2");
592 nvram_set("gpio4", "ses_button");
593 features
= SUP_SES
| SUP_WHAM_LED
;
600 if (!nvram_match("t_fix1", (char *)name
)) {
601 nvram_set("vlan0ports", "0 5*");
602 nvram_set("vlan1ports", "1 5");
603 nvram_set("vlan_enable", "1");
604 nvram_set("lan_ifnames", "vlan0 eth1");
605 nvram_set("gpio2", "ses_button");
606 nvram_set("reset_gpio", "7");
608 nvram_set("pa0itssit", "62");
609 nvram_set("pa0b0", "0x1542");
610 nvram_set("pa0b1", "0xfacb");
611 nvram_set("pa0b2", "0xfec7");
612 //nvram_set("pa0maxpwr", "0x4c");
615 case MODEL_WRTSL54GS
:
618 features
= SUP_SES
| SUP_WHAM_LED
;
623 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
;
626 case MODEL_WZRRSG54HP
:
629 features
= SUP_SES
| SUP_AOSS_LED
| SUP_HPAMP
;
631 case MODEL_WZRRSG54HP
:
632 name
= "WZR-RS-G54HP";
639 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
| SUP_HPAMP
;
643 bf
= strtoul(nvram_safe_get("boardflags"), NULL
, 0);
649 if (nvram_match("wlx_hpamp", "")) {
650 if (nvram_get_int("wl_txpwr") > 10) nvram_set("wl_txpwr", "10");
651 nvram_set("wlx_hpamp", "1");
652 nvram_set("wlx_hperx", "0");
656 if (nvram_match("wlx_hpamp", "0")) {
662 if (nvram_match("wlx_hperx", "0")) {
669 sprintf(s
, "0x%lX", n
);
670 nvram_set("boardflags", s
);
674 syslog(LOG_WARNING
, "Unexpected: boardflag=%lX", bf
);
681 nvram_set("wl0gpio0", "130");
686 features
= SUP_SES
| SUP_AOSS_LED
;
688 case MODEL_WHR2A54G54
:
690 name
= "WHR2-A54G54";
691 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
;
696 features
= SUP_SES
| SUP_AOSS_LED
;
701 features
= SUP_SES
| SUP_AOSS_LED
;
706 features
= SUP_SES
| SUP_AOSS_LED
;
716 features
= SUP_SES
| SUP_AOSS_LED
;
725 features
= SUP_NONVE
;
729 name
= "WR850G v2/v3";
730 features
= SUP_NONVE
;
736 if (!nvram_match("t_fix1", (char *)name
)) {
737 nvram_set("t_fix1", name
);
738 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3"); // set to "vlan0 eth2" by DD-WRT; default: vlan0 eth1
739 // !!TB - WLAN LED fix
740 nvram_set("wl0gpio0", "136");
746 features
= SUP_SES
| SUP_80211N
;
748 if (!nvram_get("wl0gpio0") || nvram_match("wl0gpio0", "2"))
749 nvram_set("wl0gpio0", "0x88");
772 if (!nvram_match("t_fix1", (char *)name
)) {
773 nvram_set("t_fix1", name
);
774 nvram_set("lan_ifnames", "vlan0 eth1 eth2");
775 nvram_set("wl_ifname", "eth1");
776 nvram_set("wan_ifname", "none");
781 name
= "TrueMobile 2300";
790 #ifndef WL_BSS_INFO_VERSION
791 #error WL_BSS_INFO_VERSION
793 #if WL_BSS_INFO_VERSION >= 108
799 nvram_set("opo", "12");
805 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
;
807 nvram_set("opo", "0x0008");
808 nvram_set("ag0", "0x0C");
814 features
= SUP_SES
| SUP_80211N
;
815 if (!nvram_match("t_fix1", (char *)name
)) {
816 nvram_set("lan_ifnames", "vlan0 eth1");
817 nvram_set("wan_ifnameX", "vlan1");
818 nvram_set("wl_ifname", "eth1");
819 nvram_set("t_fix1", name
);
825 features
= SUP_SES
| SUP_BRAU
| SUP_80211N
;
826 if (!nvram_match("t_fix1", (char *)name
)) {
827 nvram_set("lan_ifnames", "vlan0 eth1");
828 nvram_set("wan_ifnameX", "vlan1");
829 nvram_set("wl_ifname", "eth1");
830 nvram_set("t_fix1", name
);
836 features
= SUP_SES
| SUP_80211N
| SUP_1000ET
;
837 if (!nvram_match("t_fix1", (char *)name
)) {
838 nvram_set("lan_ifnames", "vlan1 eth1");
839 nvram_set("wan_ifnameX", "vlan2");
840 nvram_set("wl_ifname", "eth1");
841 nvram_set("vlan_enable", "1");
842 nvram_set("t_fix1", name
);
847 name
= "WNR3500L/U/v2";
848 features
= SUP_SES
| SUP_AOSS_LED
| SUP_80211N
| SUP_1000ET
;
849 if (!nvram_match("t_fix1", (char *)name
)) {
850 nvram_set("sromrev", "3");
851 nvram_set("lan_ifnames", "vlan1 eth1");
852 nvram_set("wan_ifnameX", "vlan2");
853 nvram_set("wl_ifname", "eth1");
854 nvram_set("portprio_support", "0");
855 nvram_set("t_fix1", name
);
858 case MODEL_WNR2000v2
:
861 features
= SUP_SES
| SUP_AOSS_LED
| SUP_80211N
;
862 if (!nvram_match("t_fix1", (char *)name
)) {
863 nvram_set("lan_ifnames", "vlan0 eth1");
864 nvram_set("wan_ifnameX", "vlan1");
865 nvram_set("wl_ifname", "eth1");
866 nvram_set("t_fix1", name
);
869 case MODEL_WRT160Nv3
:
871 if (nvram_match("boot_hw_model", "M10") && nvram_match("boot_hw_ver", "1.0"))
872 name
= "M10 v1"; // renamed wrt160nv3
875 features
= SUP_SES
| SUP_80211N
| SUP_WHAM_LED
;
876 if (!nvram_match("t_fix1", (char *)name
)) {
877 nvram_set("lan_ifnames", "vlan1 eth1");
878 nvram_set("wan_ifnameX", "vlan2");
879 nvram_set("wl_ifname", "eth1");
880 nvram_set("t_fix1", name
);
885 if (nvram_match("boardrev", "0x1307")) {
887 gpio_write(1 << 0, 0); // fix for WLAN LED
890 features
= SUP_SES
| SUP_80211N
| SUP_WHAM_LED
| SUP_1000ET
;
891 if (!nvram_match("t_fix1", (char *)name
)) {
892 nvram_set("lan_ifnames", "vlan1 eth1");
893 nvram_set("wan_ifnameX", "vlan2");
894 nvram_set("wl_ifname", "eth1");
895 nvram_set("t_fix1", name
);
898 #endif // CONFIG_BCMWL5
899 case MODEL_WL500GPv2
:
901 name
= "WL-500gP v2";
903 if (!nvram_match("t_fix1", (char *)name
)) {
904 nvram_set("t_fix1", name
);
907 if (nvram_match("wl0gpio1", "0x02"))
908 nvram_set("wl0gpio1", "0x88");
914 if (!nvram_match("t_fix1", (char *)name
)) {
915 nvram_set("t_fix1", name
);
917 nvram_set("wl0gpio0", "0");
918 nvram_set("wl0gpio1", "136");
919 nvram_set("wl0gpio2", "0");
920 nvram_set("wl0gpio3", "0");
925 name
= "WL-500g Deluxe";
926 // features = SUP_SES;
927 if (!nvram_match("t_fix1", (char *)name
)) {
928 nvram_set("t_fix1", name
);
929 nvram_set("wl_ifname", "eth1");
930 nvram_set("lan_ifnames", "vlan0 eth1");
931 nvram_set("wan_ifnameX", "vlan1");
932 nvram_unset("wl0gpio0");
939 if (!nvram_match("t_fix1", (char *)name
)) {
940 nvram_set("t_fix1", name
);
941 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3");
942 nvram_set("wan_ifnameX", "vlan1");
943 nvram_set("wl_ifname", "eth1");
944 nvram_set("wl0gpio0", "8");
945 nvram_set("wl0gpio1", "0");
946 nvram_set("wl0gpio2", "0");
947 nvram_set("wl0gpio3", "0");
950 #endif // WL_BSS_INFO_VERSION >= 108
955 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
| SUP_80211N
;
960 features
= SUP_SES
| SUP_80211N
;
961 if (!nvram_match("t_fix1", (char *)name
)) {
962 nvram_set("wan_ifnameX", "eth1");
963 nvram_set("wl0gpio0", "8");
970 sprintf(s
, "%s %s", mfr
, name
);
973 snprintf(s
, sizeof(s
), "%s %d/%s/%s/%s/%s", mfr
, check_hw_type(),
974 nvram_safe_get("boardtype"), nvram_safe_get("boardnum"), nvram_safe_get("boardrev"), nvram_safe_get("boardflags"));
977 nvram_set("t_model_name", s
);
979 nvram_set("pa0maxpwr", "251"); // allow Tx power up tp 251 mW, needed for ND only
981 sprintf(s
, "0x%lX", features
);
982 nvram_set("t_features", s
);
987 note: set wan_ifnameX if wan_ifname needs to be overriden
990 if (nvram_is_empty("wan_ifnameX")) {
992 nvram_set("wan_ifnameX", ((strtoul(nvram_safe_get("boardflags"), NULL
, 0) & BFL_ENETVLAN
) ||
993 (check_hw_type() == HW_BCM4712
)) ? "vlan1" : "eth1");
995 p
= nvram_safe_get("wan_ifname");
996 if ((*p
== 0) || (nvram_match("wl_ifname", p
))) {
997 p
= ((strtoul(nvram_safe_get("boardflags"), NULL
, 0) & BFL_ENETVLAN
) ||
998 (check_hw_type() == HW_BCM4712
)) ? "vlan1" : "eth1";
1000 nvram_set("wan_ifnameX", p
);
1005 nvram_set("wl_hwaddr", ""); // when disabling wireless, we must get null wireless mac ??
1006 //!!TB - do not force country code here to allow nvram override
1007 //nvram_set("wl_country", "JP");
1008 //nvram_set("wl_country_code", "JP");
1009 nvram_set("wan_get_dns", "");
1010 nvram_set("wan_get_domain", "");
1011 nvram_set("pppoe_pid0", "");
1012 nvram_set("action_service", "");
1013 nvram_set("jffs2_format", "0");
1014 nvram_set("rrules_radio", "-1");
1015 nvram_unset("https_crt_gen");
1016 #ifdef TCONFIG_MEDIA_SERVER
1017 nvram_unset("ms_rescan");
1019 if (nvram_get_int("http_id_gen") == 1) nvram_unset("http_id");
1021 nvram_unset("sch_rboot_last");
1022 nvram_unset("sch_rcon_last");
1023 nvram_unset("sch_c1_last");
1024 nvram_unset("sch_c2_last");
1025 nvram_unset("sch_c3_last");
1027 nvram_set("brau_state", "");
1028 if ((features
& SUP_BRAU
) == 0) nvram_set("script_brau", "");
1029 if ((features
& SUP_SES
) == 0) nvram_set("sesx_script", "");
1031 if ((features
& SUP_1000ET
) == 0) nvram_set("jumbo_frame_enable", "0");
1033 if (nvram_match("wl_net_mode", "disabled")) {
1034 nvram_set("wl_radio", "0");
1035 nvram_set("wl_net_mode", "mixed");
1041 /* Get the special files from nvram and copy them to disc.
1042 * These were files saved with "nvram setfile2nvram <filename>".
1043 * Better hope that they were saved with full pathname.
1045 static void load_files_from_nvram(void)
1048 char buf
[NVRAM_SPACE
];
1050 if (nvram_getall(buf
, sizeof(buf
)) != 0)
1053 for (name
= buf
; *name
; name
+= strlen(name
) + 1) {
1054 if (strncmp(name
, "FILE:", 5) == 0) { /* This special name marks a file to get. */
1055 if ((cp
= strchr(name
, '=')) == NULL
)
1058 syslog(LOG_INFO
, "Loading file '%s' from nvram", name
+ 5);
1059 nvram_nvram2file(name
, name
+ 5);
1064 #if defined(LINUX26) && defined(TCONFIG_USB)
1065 static inline void tune_min_free_kbytes(void)
1067 struct sysinfo info
;
1069 memset(&info
, 0, sizeof(struct sysinfo
));
1071 if (info
.totalram
>= 55 * 1024 * 1024) {
1072 // If we have 64MB+ RAM, tune min_free_kbytes
1073 // to reduce page allocation failure errors.
1074 f_write_string("/proc/sys/vm/min_free_kbytes", "8192", 0, 0);
1079 static void sysinit(void)
1081 static const time_t tm
= 0;
1090 mount("proc", "/proc", "proc", 0, NULL
);
1091 mount("tmpfs", "/tmp", "tmpfs", 0, NULL
);
1094 mount("devfs", "/dev", "tmpfs", MS_MGC_VAL
| MS_NOATIME
, NULL
);
1095 mknod("/dev/null", S_IFCHR
| 0666, makedev(1, 3));
1096 mknod("/dev/console", S_IFCHR
| 0600, makedev(5, 1));
1097 mount("sysfs", "/sys", "sysfs", MS_MGC_VAL
, NULL
);
1098 mkdir("/dev/shm", 0777);
1099 mkdir("/dev/pts", 0777);
1100 mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL
, NULL
);
1103 if (console_init()) noconsole
= 1;
1107 static const char *mkd
[] = {
1108 "/tmp/etc", "/tmp/var", "/tmp/home", "/tmp/mnt",
1109 "/tmp/share", "/var/webmon", // !!TB
1110 "/var/log", "/var/run", "/var/tmp", "/var/lib", "/var/lib/misc",
1111 "/var/spool", "/var/spool/cron", "/var/spool/cron/crontabs",
1112 "/tmp/var/wwwext", "/tmp/var/wwwext/cgi-bin", // !!TB - CGI support
1116 for (i
= 0; mkd
[i
]; ++i
) {
1117 mkdir(mkd
[i
], 0755);
1119 mkdir("/var/lock", 0777);
1120 mkdir("/var/tmp/dhcp", 0777);
1121 mkdir("/home/root", 0700);
1122 chmod("/tmp", 0777);
1123 f_write("/etc/hosts", NULL
, 0, 0, 0644); // blank
1124 f_write("/etc/fstab", NULL
, 0, 0, 0644); // !!TB - blank
1125 simple_unlock("cron");
1126 simple_unlock("firewall");
1127 simple_unlock("restrictions");
1130 if ((d
= opendir("/rom/etc")) != NULL
) {
1131 while ((de
= readdir(d
)) != NULL
) {
1132 if (de
->d_name
[0] == '.') continue;
1133 snprintf(s
, sizeof(s
), "%s/%s", "/rom/etc", de
->d_name
);
1134 snprintf(t
, sizeof(t
), "%s/%s", "/etc", de
->d_name
);
1139 symlink("/proc/mounts", "/etc/mtab");
1141 #ifdef TCONFIG_SAMBASRV
1142 if ((d
= opendir("/usr/codepages")) != NULL
) {
1143 while ((de
= readdir(d
)) != NULL
) {
1144 if (de
->d_name
[0] == '.') continue;
1145 snprintf(s
, sizeof(s
), "/usr/codepages/%s", de
->d_name
);
1146 snprintf(t
, sizeof(t
), "/usr/share/%s", de
->d_name
);
1154 eval("hotplug2", "--coldplug");
1158 set_action(ACT_IDLE
);
1160 for (i
= 0; defenv
[i
]; ++i
) {
1165 printf("\n\nHit ENTER for console...\n\n");
1171 for (i
= 0; i
< sizeof(fatalsigs
) / sizeof(fatalsigs
[0]); i
++) {
1172 signal(fatalsigs
[i
], handle_fatalsigs
);
1174 for (i
= 0; i
< sizeof(initsigs
) / sizeof(initsigs
[0]); i
++) {
1175 signal(initsigs
[i
], handle_initsigs
);
1177 signal(SIGCHLD
, handle_reap
);
1179 switch (model
= get_model()) {
1180 case MODEL_WR850GV1
:
1181 case MODEL_WR850GV2
:
1182 // need to cleanup some variables...
1183 if ((nvram_get("t_model") == NULL
) && (nvram_get("MyFirmwareVersion") != NULL
)) {
1184 nvram_unset("MyFirmwareVersion");
1185 nvram_set("restore_defaults", "1");
1190 #ifdef CONFIG_BCMWL5
1191 // ctf must be loaded prior to any other modules
1195 hardware
= check_hw_type();
1196 #if WL_BSS_INFO_VERSION >= 108
1199 if ((hardware
== HW_BCM4702
) && (model
!= MODEL_WR850GV1
)) {
1208 #ifdef CONFIG_BCMWL5
1213 modprobe("tomato_ct");
1217 system("nvram defaults --initcheck");
1220 // set the packet size
1221 if (nvram_get_int("jumbo_frame_enable")) {
1222 // only set the size here - 'enable' flag is set by the driver
1223 // eval("et", "robowr", "0x40", "0x01", "0x1F"); // (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)
1224 eval("et", "robowr", "0x40", "0x05", nvram_safe_get("jumbo_frame_size"));
1227 klogctl(8, NULL
, nvram_get_int("console_loglevel"));
1229 #if defined(LINUX26) && defined(TCONFIG_USB)
1230 tune_min_free_kbytes();
1233 set_host_domain_name();
1241 i
= nvram_get_int("sesx_led");
1242 led(LED_AMBER
, (i
& 1) != 0);
1243 led(LED_WHITE
, (i
& 2) != 0);
1244 led(LED_AOSS
, (i
& 4) != 0);
1245 led(LED_BRIDGE
, (i
& 8) != 0);
1249 int init_main(int argc
, char *argv
[])
1251 pid_t shell_pid
= 0;
1258 #if defined(DEBUG_NOISY)
1259 nvram_set("debug_logeval", "1");
1260 nvram_set("debug_cprintf", "1");
1261 nvram_set("debug_cprintf_file", "1");
1262 nvram_set("debug_ddns", "1");
1266 // TRACE_PT("main loop state=%d\n", state);
1278 unlink("/var/notice/sysup");
1280 run_nvscript("script_shut", NULL
, 10);
1288 if ((state
== REBOOT
) || (state
== HALT
)) {
1289 remove_storage_main(1);
1292 shutdn(state
== REBOOT
);
1295 if (state
== STOP
) {
1300 // RESTART falls through
1303 SET_LED(RELEASE_WAN_CONTROL
);
1306 load_files_from_nvram();
1309 fd
= file_lock("usb"); // hold off automount processing
1312 run_nvscript("script_init", NULL
, 2);
1314 file_unlock(fd
); // allow to process usb hotplug events
1317 * On RESTART some partitions can stay mounted if they are busy at the moment.
1318 * In that case USB drivers won't unload, and hotplug won't kick off again to
1319 * remount those drives that actually got unmounted. Make sure to remount ALL
1320 * partitions here by simulating hotplug event.
1322 if (state
== RESTART
) add_remove_usbhost("-1", 1);
1331 #ifdef CONFIG_BCMWL5
1332 if (nvram_match("wds_enable", "1")) {
1333 /* Restart NAS one more time - for some reason without
1334 * this the new driver doesn't always bring WDS up.
1341 syslog(LOG_INFO
, "Tomato %s", tomato_version
);
1342 syslog(LOG_INFO
, "%s", nvram_safe_get("t_model_name"));
1345 notice_set("sysup", "");
1352 while (signaled
== -1) {
1354 if ((!noconsole
) && ((!shell_pid
) || (kill(shell_pid
, 0) != 0))) {
1355 shell_pid
= run_shell(0, 1);
1368 int reboothalt_main(int argc
, char *argv
[])
1370 int reboot
= (strstr(argv
[0], "reboot") != NULL
);
1371 puts(reboot
? "Rebooting..." : "Shutting down...");
1374 kill(1, reboot
? SIGTERM
: SIGQUIT
);
1376 /* In the case we're hung, we'll get stuck and never actually reboot.
1377 * The only way out is to pull power.
1378 * So after 10 seconds, forcibly crash & restart.
1381 int wait
= nvram_get_int("reset_wait");
1382 if ((wait
< 10) || (wait
> 120)) wait
= 10;
1384 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
1386 puts("Still running... Doing machine reset.");
1388 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
1390 f_write("/proc/sysrq-trigger", "b", 1, 0 , 0); /* machine reset */