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"
35 static int fatalsigs
[] = {
46 static int initsigs
[] = {
56 static char *defenv
[] = {
59 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
65 /* Set terminal settings to reasonable defaults */
66 static void set_term(int fd
)
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 */
86 tty
.c_cflag
&= CBAUD
|CBAUDEX
|CSIZE
|CSTOPB
|PARENB
|PARODD
;
87 tty
.c_cflag
|= CREAD
|HUPCL
|CLOCAL
;
91 tty
.c_iflag
= ICRNL
| IXON
| IXOFF
;
94 tty
.c_oflag
= OPOST
| ONLCR
;
98 ISIG
| ICANON
| ECHO
| ECHOE
| ECHOK
| ECHOCTL
| ECHOKE
| IEXTEN
;
100 tcsetattr(fd
, TCSANOW
, &tty
);
103 static int console_init(void)
108 ioctl(0, TIOCNOTTY
, 0);
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
);
127 ioctl(0, TIOCSCTTY
, 1);
128 tcsetpgrp(0, getpgrp());
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
)
143 struct timeval tv
= { timeout
, 0 };
147 return select(fd
+ 1, &rfds
, NULL
, NULL
, (timeout
> 0) ? &tv
: NULL
);
150 static pid_t
run_shell(int timeout
, int nowait
)
155 /* Wait for user input */
156 if (waitfor(STDIN_FILENO
, timeout
) <= 0) return 0;
158 switch (pid
= fork()) {
163 /* Reset signal handlers set for parent process */
164 for (sig
= 0; sig
< (_NSIG
-1); sig
++)
165 signal(sig
, SIG_DFL
);
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. */
183 waitpid(pid
, NULL
, 0);
189 int console_main(int argc
, char *argv
[])
191 for (;;) run_shell(0, 0);
196 static void shutdn(int rb
)
202 _dprintf("shutdn rb=%d\n", rb
);
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
);
216 set_action(ACT_REBOOT
);
218 // Disconnect pppd - need this for PPTP/L2TP to finish gracefully
232 umount("/jffs"); // may hang if not
238 for (i
= 4; i
> 0; --i
) {
249 reboot(rb
? RB_AUTOBOOT
: RB_HALT_SYSTEM
);
256 static void handle_fatalsigs(int sig
)
258 _dprintf("fatal sig=%d\n", sig
);
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
270 static void handle_reap(int sig
)
276 static int check_nv(const char *name
, const char *value
)
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
);
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)
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
);
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
);
312 static int find_dir320_mac_addr(void)
316 int i
, part
, size
, found
= 0;
318 if (!mtd_getinfo("board_data", &part
, &size
))
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);
331 nvram_set("et0macaddr", s
);
334 else if (!memcmp(buffer
+ i
, "wanmac=", 7)) {
335 memcpy(s
, buffer
+ i
+ 7, 17);
337 nvram_set("il0macaddr", s
);
340 nvram_set("et0macaddr", s
);
351 strcpy(s
, nvram_safe_get("wl0_hwaddr"));
353 nvram_set("et0macaddr", s
);
358 static int init_vlan_ports(void)
361 int model
= get_model();
365 switch (check_hw_type()) {
366 case HW_BCM5352E
: // G v4, GS v3, v4
367 dirty
|= check_nv("vlan0ports", "3 2 1 0 5*");
372 dirty
|= check_nv("vlan0ports", "0 5*");
373 dirty
|= check_nv("vlan1ports", "1 5");
374 dirty
|= check_nv("vlan_enable", "1");
378 case MODEL_WL500GPv2
:
380 if (nvram_match("vlan1ports", "0 5u")) // 520GU or WL500GE?
381 dirty
|= check_nv("vlan1ports", "0 5");
382 else if (nvram_match("vlan1ports", "4 5u"))
383 dirty
|= check_nv("vlan1ports", "4 5");
386 dirty
|= check_nv("vlan0ports", "1 2 3 4 5*");
387 dirty
|= check_nv("vlan1ports", "0 5");
391 dirty
|= (nvram_get("vlan2ports") != NULL
);
392 nvram_unset("vlan2ports");
393 dirty
|= check_nv("vlan1ports", "0 5");
395 case MODEL_WRT310Nv1
:
396 dirty
|= check_nv("vlan1ports", "1 2 3 4 8*");
397 dirty
|= check_nv("vlan2ports", "0 8");
400 dirty
|= check_nv("vlan0ports", "0 1 2 3 5*");
401 dirty
|= check_nv("vlan1ports", "4 5");
406 case MODEL_WNR3500LV2
:
409 dirty
|= check_nv("vlan1ports", "4 3 2 1 8*");
410 dirty
|= check_nv("vlan2ports", "0 8");
413 dirty
|= check_nv("vlan2ports", "0 1 2 3 5*");
414 dirty
|= check_nv("vlan1ports", "4 5");
416 case MODEL_WNR2000v2
:
417 dirty
|= check_nv("vlan1ports", "4 3 2 1 5*");
418 dirty
|= check_nv("vlan2ports", "0 5");
421 dirty
|= check_nv("vlan1ports", "4 5");
425 dirty
|= check_nv("vlan0ports", "1 2 3 4 5*");
426 dirty
|= check_nv("vlan1ports", "0 5");
430 dirty
|= check_nv("vlan0ports", "3 2 1 0 5*");
431 dirty
|= check_nv("vlan1ports", "4 5");
433 case MODEL_WRT610Nv2
:
434 case MODEL_F5D8235v3
:
435 dirty
|= check_nv("vlan1ports", "1 2 3 4 8*");
436 dirty
|= check_nv("vlan2ports", "0 8");
440 dirty
|= check_nv("vlan1ports", "3 2 1 0 8*");
441 dirty
|= check_nv("vlan2ports", "4 8");
448 dirty
|= check_nv("vlan1ports", "0 1 2 3 5*");
449 dirty
|= check_nv("vlan2ports", "4 5");
455 dirty
|= check_nv("vlan1ports", "0 1 2 3 8*");
456 dirty
|= check_nv("vlan2ports", "4 8");
458 case MODEL_WRT160Nv3
:
459 if (nvram_match("vlan1ports", "1 2 3 4 5*")) {
460 // fix lan port numbering on CSE41, CSE51
461 dirty
|= check_nv("vlan1ports", "4 3 2 1 5*");
463 else if (nvram_match("vlan1ports", "1 2 3 4 8*")) {
465 dirty
|= check_nv("vlan1ports", "4 3 2 1 8*");
474 static void check_bootnv(void)
482 dirty
= check_nv("wl0_leddc", "0x640000") | check_nv("wl1_leddc", "0x640000");
486 dirty
|= check_nv("vlan0hwname", "et0");
487 dirty
|= check_nv("vlan1hwname", "et0");
490 dirty
|= check_nv("wl0gpio0", "130");
494 // need to cleanup some variables...
495 if ((nvram_get("t_model") == NULL
) && (nvram_get("MyFirmwareVersion") != NULL
)) {
496 nvram_unset("MyFirmwareVersion");
497 nvram_set("restore_defaults", "1");
501 /* fix WL500W mac adresses for WAN port */
502 if (invalid_mac(nvram_get("et1macaddr"))) {
503 strcpy(mac
, nvram_safe_get("et0macaddr"));
505 dirty
|= check_nv("et1macaddr", mac
);
507 dirty
|= check_nv("wl0gpio0", "0x88");
510 dirty
|= check_nv("sdram_init", "0x0009"); // 32MB; defaults: 0x000b, 0x0009
511 dirty
|= check_nv("wl0gpio0", "136");
513 case MODEL_WL500GPv2
:
515 dirty
|= check_nv("wl0gpio1", "136");
518 dirty
|= check_nv("vlan0hwname", "et0");
519 dirty
|= check_nv("vlan1hwname", "et0");
520 dirty
|= check_nv("boardflags", "0x00000100"); // set BFL_ENETVLAN
521 nvram_unset("wl0gpio0");
524 if (strlen(nvram_safe_get("et0macaddr")) == 12 ||
525 strlen(nvram_safe_get("il0macaddr")) == 12) {
526 dirty
|= find_dir320_mac_addr();
528 if (nvram_get("vlan2hwname") != NULL
) {
529 nvram_unset("vlan2hwname");
532 dirty
|= check_nv("wandevs", "vlan1");
533 dirty
|= check_nv("vlan1hwname", "et0");
534 dirty
|= check_nv("wl0gpio0", "8");
535 dirty
|= check_nv("wl0gpio1", "0");
536 dirty
|= check_nv("wl0gpio2", "0");
537 dirty
|= check_nv("wl0gpio3", "0");
539 if (invalid_mac(nvram_get("et0macaddr"))) {
540 dirty
|= find_sercom_mac_addr();
543 case MODEL_WRT160Nv1
:
544 case MODEL_WRT310Nv1
:
546 dirty
|= check_nv("wl0gpio0", "8");
550 dirty
|= check_nv("boardflags", "0x00000710"); // needed to enable USB
551 dirty
|= check_nv("vlan2hwname", "et0");
552 dirty
|= check_nv("ledbh0", "7");
554 case MODEL_WNR3500LV2
:
555 dirty
|= check_nv("vlan2hwname", "et0");
557 case MODEL_WNR2000v2
:
558 dirty
|= check_nv("ledbh5", "8");
561 dirty
|= check_nv("reset_gpio", "5");
562 dirty
|= check_nv("ledbh0", "136");
563 dirty
|= check_nv("ledbh1", "11");
564 /* fall through, same as RT-N16 */
566 dirty
|= check_nv("vlan2hwname", "et0");
568 case MODEL_WRT610Nv2
:
569 dirty
|= check_nv("vlan2hwname", "et0");
570 dirty
|= check_nv("pci/1/1/ledbh2", "8");
571 dirty
|= check_nv("sb/1/ledbh1", "8");
572 if (invalid_mac(nvram_get("pci/1/1/macaddr"))) {
573 strcpy(mac
, nvram_safe_get("et0macaddr"));
575 dirty
|= check_nv("pci/1/1/macaddr", mac
);
582 case MODEL_F5D8235v3
:
583 if (nvram_match("sb/1/macaddr", nvram_safe_get("et0macaddr"))) {
584 strcpy(mac
, nvram_safe_get("et0macaddr"));
586 dirty
|= check_nv("sb/1/macaddr", mac
);
588 dirty
|= check_nv("pci/1/1/macaddr", mac
);
591 dirty
|= check_nv("vlan2hwname", "et0");
592 if (invalid_mac(nvram_get("pci/1/1/macaddr")) == 0 ||
593 invalid_mac(nvram_get("sb/1/macaddr"))) {
594 strcpy(mac
, nvram_safe_get("et0macaddr"));
596 dirty
|= check_nv("sb/1/macaddr", mac
);
598 dirty
|= check_nv("pci/1/1/macaddr", mac
);
607 case MODEL_WRT160Nv3
:
608 dirty
|= check_nv("vlan2hwname", "et0");
613 if (strncmp(nvram_safe_get("pmon_ver"), "CFE", 3) != 0) return;
615 hardware
= check_hw_type();
616 if (!nvram_get("boardtype") ||
617 !nvram_get("boardnum") ||
618 !nvram_get("boardflags") ||
619 !nvram_get("clkfreq") ||
620 !nvram_get("os_flash_addr") ||
621 !nvram_get("dl_ram_addr") ||
622 !nvram_get("os_ram_addr") ||
623 !nvram_get("scratch") ||
624 !nvram_get("et0macaddr") ||
625 ((hardware
!= HW_BCM4704_BCM5325F
) && (!nvram_get("vlan0ports") || !nvram_get("vlan0hwname")))) {
626 _dprintf("Unable to find critical settings, erasing NVRAM\n");
631 dirty
|= check_nv("aa0", "3");
632 dirty
|= check_nv("wl0gpio0", "136");
633 dirty
|= check_nv("wl0gpio2", "0");
634 dirty
|= check_nv("wl0gpio3", "0");
635 dirty
|= check_nv("cctl", "0");
636 dirty
|= check_nv("ccode", "0");
640 /* Lower the DDR ram drive strength , the value will be stable for all boards
641 Latency 3 is more stable for all ddr 20050420 by honor */
642 dirty
|= check_nv("sdram_init", "0x010b");
643 dirty
|= check_nv("sdram_config", "0x0062");
644 if (!nvram_match("debug_clkfix", "0")) {
645 dirty
|= check_nv("clkfreq", "216");
648 nvram_set("sdram_ncdl", "0x0");
650 dirty
|= check_nv("pa0itssit", "62");
651 dirty
|= check_nv("pa0b0", "0x15eb");
652 dirty
|= check_nv("pa0b1", "0xfa82");
653 dirty
|= check_nv("pa0b2", "0xfe66");
654 //dirty |= check_nv("pa0maxpwr", "0x4e");
656 case HW_BCM5352E
: // G v4, GS v3, v4
657 dirty
|= check_nv("sdram_init", "0x010b");
658 dirty
|= check_nv("sdram_config", "0x0062");
660 nvram_set("sdram_ncdl", "0x0");
662 dirty
|= check_nv("pa0itssit", "62");
663 dirty
|= check_nv("pa0b0", "0x168b");
664 dirty
|= check_nv("pa0b1", "0xfabf");
665 dirty
|= check_nv("pa0b2", "0xfeaf");
666 //dirty |= check_nv("pa0maxpwr", "0x4e");
669 dirty
|= check_nv("pa0itssit", "62");
670 dirty
|= check_nv("pa0b0", "0x1326");
671 dirty
|= check_nv("pa0b1", "0xFB51");
672 dirty
|= check_nv("pa0b2", "0xFE87");
673 //dirty |= check_nv("pa0maxpwr", "0x4e");
675 case HW_BCM4704_BCM5325F
:
679 dirty
|= check_nv("pa0itssit", "62");
680 dirty
|= check_nv("pa0b0", "0x170c");
681 dirty
|= check_nv("pa0b1", "0xfa24");
682 dirty
|= check_nv("pa0b2", "0xfe70");
683 //dirty |= check_nv("pa0maxpwr", "0x48");
690 dirty
|= init_vlan_ports();
694 REBOOT
: // do a simple reboot
701 static int init_nvram(void)
703 unsigned long features
;
713 sprintf(s
, "%d", model
);
714 nvram_set("t_model", s
);
722 name
= "WRT54G/GS/GL";
723 switch (check_hw_type()) {
725 nvram_set("gpio2", "adm_eecs");
726 nvram_set("gpio3", "adm_eesk");
727 nvram_unset("gpio4");
728 nvram_set("gpio5", "adm_eedi");
729 nvram_set("gpio6", "adm_rc");
732 nvram_unset("gpio2");
733 nvram_unset("gpio3");
734 nvram_unset("gpio4");
735 nvram_unset("gpio5");
736 nvram_unset("gpio6");
739 nvram_set("opo", "0x0008");
740 nvram_set("ag0", "0x02");
743 nvram_set("gpio2", "ses_led");
744 nvram_set("gpio3", "ses_led2");
745 nvram_set("gpio4", "ses_button");
746 features
= SUP_SES
| SUP_WHAM_LED
;
753 if (!nvram_match("t_fix1", (char *)name
)) {
754 nvram_set("lan_ifnames", "vlan0 eth1");
755 nvram_set("gpio2", "ses_button");
756 nvram_set("reset_gpio", "7");
758 nvram_set("pa0itssit", "62");
759 nvram_set("pa0b0", "0x1542");
760 nvram_set("pa0b1", "0xfacb");
761 nvram_set("pa0b2", "0xfec7");
762 //nvram_set("pa0maxpwr", "0x4c");
765 case MODEL_WRTSL54GS
:
768 features
= SUP_SES
| SUP_WHAM_LED
;
773 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
;
776 case MODEL_WZRRSG54HP
:
779 features
= SUP_SES
| SUP_AOSS_LED
| SUP_HPAMP
;
781 case MODEL_WZRRSG54HP
:
782 name
= "WZR-RS-G54HP";
789 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
| SUP_HPAMP
;
793 bf
= strtoul(nvram_safe_get("boardflags"), NULL
, 0);
799 if (nvram_match("wlx_hpamp", "")) {
800 if (nvram_get_int("wl_txpwr") > 10) nvram_set("wl_txpwr", "10");
801 nvram_set("wlx_hpamp", "1");
802 nvram_set("wlx_hperx", "0");
806 if (nvram_match("wlx_hpamp", "0")) {
812 if (nvram_match("wlx_hperx", "0")) {
819 sprintf(s
, "0x%lX", n
);
820 nvram_set("boardflags", s
);
824 syslog(LOG_WARNING
, "Unexpected: boardflag=%lX", bf
);
835 features
= SUP_SES
| SUP_AOSS_LED
;
837 case MODEL_WHR2A54G54
:
839 name
= "WHR2-A54G54";
840 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
;
845 features
= SUP_SES
| SUP_AOSS_LED
;
850 features
= SUP_SES
| SUP_AOSS_LED
;
855 features
= SUP_SES
| SUP_AOSS_LED
;
865 features
= SUP_SES
| SUP_AOSS_LED
;
874 features
= SUP_NONVE
;
878 name
= "WR850G v2/v3";
879 features
= SUP_NONVE
;
886 nvram_set("usb_ohci", "-1");
888 if (!nvram_match("t_fix1", (char *)name
)) {
889 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3"); // set to "vlan0 eth2" by DD-WRT; default: vlan0 eth1
895 features
= SUP_SES
| SUP_80211N
;
897 nvram_set("usb_ohci", "-1");
905 nvram_set("usb_uhci", "-1");
924 if (!nvram_match("t_fix1", (char *)name
)) {
925 nvram_set("lan_ifnames", "vlan0 eth1 eth2");
926 nvram_set("wl_ifname", "eth1");
927 nvram_set("wan_ifname", "none");
932 name
= "TrueMobile 2300";
941 #ifndef WL_BSS_INFO_VERSION
942 #error WL_BSS_INFO_VERSION
944 #if WL_BSS_INFO_VERSION >= 108
950 nvram_set("opo", "12");
956 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
;
958 nvram_set("opo", "0x0008");
959 nvram_set("ag0", "0x0C");
965 features
= SUP_SES
| SUP_80211N
;
966 if (!nvram_match("t_fix1", (char *)name
)) {
967 nvram_set("lan_ifnames", "vlan0 eth1");
968 nvram_set("wan_ifnameX", "vlan1");
969 nvram_set("wl_ifname", "eth1");
975 features
= SUP_SES
| SUP_80211N
;
977 nvram_set("usb_uhci", "-1");
979 if (!nvram_match("t_fix1", (char *)name
)) {
980 nvram_set("lan_ifnames", "vlan0 eth1");
981 nvram_set("wan_ifnameX", "vlan1");
982 nvram_set("wl_ifname", "eth1");
988 features
= SUP_SES
| SUP_BRAU
| SUP_80211N
;
989 if (!nvram_match("t_fix1", (char *)name
)) {
990 nvram_set("lan_ifnames", "vlan0 eth1");
991 nvram_set("wan_ifnameX", "vlan1");
992 nvram_set("wl_ifname", "eth1");
998 features
= SUP_80211N
;
999 if (!nvram_match("t_fix1", (char *)name
)) {
1000 nvram_set("lan_ifnames", "vlan0 eth1");
1001 nvram_set("wan_ifnameX", "vlan1");
1002 nvram_set("wl_ifname", "eth1");
1008 features
= SUP_SES
| SUP_80211N
| SUP_1000ET
;
1010 nvram_set("usb_uhci", "-1");
1012 if (!nvram_match("t_fix1", (char *)name
)) {
1013 nvram_set("lan_ifnames", "vlan1 eth1");
1014 nvram_set("wan_iface", "vlan2");
1015 nvram_set("wan_ifname", "vlan2");
1016 nvram_set("wan_ifnameX", "vlan2");
1017 nvram_set("wan_ifnames", "vlan2");
1018 nvram_set("wl_ifname", "eth1");
1024 features
= SUP_SES
| SUP_80211N
| SUP_1000ET
;
1026 nvram_set("usb_uhci", "-1");
1028 if (!nvram_match("t_fix1", (char *)name
)) {
1029 nvram_set("lan_ifnames", "vlan1 eth1");
1030 nvram_set("wan_ifnameX", "vlan2");
1031 nvram_set("wl_ifname", "eth1");
1032 nvram_set("vlan_enable", "1");
1038 features
= SUP_SES
| SUP_80211N
;
1039 if (!nvram_match("t_fix1", (char *)name
)) {
1040 nvram_set("lan_ifnames", "vlan2 eth1");
1041 nvram_set("wan_ifnameX", "vlan1");
1042 nvram_set("landevs", "vlan2 wl0");
1043 nvram_set("wandevs", "vlan1");
1044 nvram_unset("vlan0ports");
1050 features
= SUP_SES
| SUP_80211N
| SUP_1000ET
;
1052 nvram_set("usb_uhci", "-1");
1053 #if defined(LINUX26) && defined(TCONFIG_USB_EXTRAS)
1054 if (nvram_get_int("usb_mmc") == -1) nvram_set("usb_mmc", "0");
1057 if (!nvram_match("t_fix1", (char *)name
)) {
1058 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1059 nvram_set("wan_ifnameX", "vlan2");
1060 nvram_set("wl_ifnames", "eth1 eth2");
1061 nvram_set("landevs", "vlan1 wl0 wl1");
1062 nvram_set("wandevs", "vlan2");
1063 #if defined(LINUX26) && defined(TCONFIG_USB)
1064 nvram_set("usb_noled", "1-1.4"); /* SD/MMC Card */
1068 case MODEL_WNR3500L
:
1070 name
= "WNR3500L/U/v2";
1071 features
= SUP_SES
| SUP_AOSS_LED
| SUP_80211N
| SUP_1000ET
;
1072 if (!nvram_match("t_fix1", (char *)name
)) {
1073 nvram_set("sromrev", "3");
1074 nvram_set("lan_ifnames", "vlan1 eth1");
1075 nvram_set("wan_ifnameX", "vlan2");
1076 nvram_set("wl_ifname", "eth1");
1079 case MODEL_WNR3500LV2
:
1081 name
= "WNR3500L v2";
1082 features
= SUP_SES
| SUP_AOSS_LED
| SUP_80211N
| SUP_1000ET
;
1083 if (!nvram_match("t_fix1", (char *)name
)) {
1084 nvram_set("lan_ifnames", "vlan1 eth1");
1085 nvram_set("wan_ifnameX", "vlan2");
1086 nvram_set("wl_ifname", "eth1");
1089 case MODEL_WNR2000v2
:
1091 name
= "WNR2000 v2";
1092 features
= SUP_SES
| SUP_AOSS_LED
| SUP_80211N
;
1093 if (!nvram_match("t_fix1", (char *)name
)) {
1094 nvram_set("lan_ifnames", "vlan0 eth1");
1095 nvram_set("wan_ifnameX", "vlan1");
1096 nvram_set("wl_ifname", "eth1");
1103 case MODEL_F5D8235v3
:
1105 features
= SUP_SES
| SUP_80211N
;
1108 name
= "Share Max N300 (F7D3301/F7D7301) v1";
1111 name
= "Share N300 (F7D3302/F7D7302) v1";
1114 name
= "Play Max / N600 HD (F7D4301/F7D8301) v1";
1117 name
= "Play N600 (F7D4302/F7D8302) v1";
1119 case MODEL_F5D8235v3
:
1120 name
= "N F5D8235-4 v3";
1124 nvram_set("usb_uhci", "-1");
1126 if (!nvram_match("t_fix1", (char *)name
)) {
1127 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1128 nvram_set("wan_ifnameX", "vlan2");
1129 nvram_set("landevs", "vlan1 wl0 wl1");
1130 nvram_set("wandevs", "vlan2");
1136 name
= nvram_safe_get("boot_hw_model");
1137 ver
= nvram_safe_get("boot_hw_ver");
1138 features
= SUP_SES
| SUP_80211N
;
1139 if (!nvram_match("t_fix1", (char *)name
)) {
1140 nvram_set("lan_ifnames", "vlan1 eth1");
1141 nvram_set("wan_ifnameX", "vlan2");
1142 nvram_set("wl_ifname", "eth1");
1147 name
= nvram_safe_get("boot_hw_model");
1148 ver
= nvram_safe_get("boot_hw_ver");
1149 features
= SUP_SES
| SUP_80211N
;
1151 nvram_set("usb_uhci", "-1");
1153 if (!nvram_match("t_fix1", (char *)name
)) {
1154 nvram_set("lan_ifnames", "vlan1 eth1");
1155 nvram_set("wan_ifnameX", "vlan2");
1156 nvram_set("wl_ifname", "eth1");
1161 name
= nvram_safe_get("boot_hw_model");
1162 ver
= nvram_safe_get("boot_hw_ver");
1163 features
= SUP_SES
| SUP_80211N
;
1164 if (!nvram_match("t_fix1", (char *)name
)) {
1165 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1166 nvram_set("wan_ifnameX", "vlan2");
1167 nvram_set("wl_ifname", "eth1");
1172 name
= nvram_safe_get("boot_hw_model");
1173 ver
= nvram_safe_get("boot_hw_ver");
1174 features
= SUP_SES
| SUP_80211N
| SUP_1000ET
;
1176 nvram_set("usb_uhci", "-1");
1178 if (!nvram_match("t_fix1", (char *)name
)) {
1179 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1180 nvram_set("wan_ifnameX", "vlan2");
1181 nvram_set("wl_ifname", "eth1");
1185 case MODEL_WRT160Nv3
:
1186 // same as M10, M20, WRT310Nv2, E1000v1
1188 name
= nvram_safe_get("boot_hw_model");
1189 ver
= nvram_safe_get("boot_hw_ver");
1190 if (nvram_match("boot_hw_model", "E100")){
1193 if (nvram_match("boot_hw_model", "M10") || nvram_match("boot_hw_model", "M20")){
1196 features
= SUP_SES
| SUP_80211N
| SUP_WHAM_LED
;
1197 if (!nvram_match("t_fix1", (char *)name
)) {
1198 nvram_set("lan_ifnames", "vlan1 eth1");
1199 nvram_set("wan_ifnameX", "vlan2");
1200 nvram_set("wl_ifname", "eth1");
1205 name
= nvram_match("boardrev", "0x1307") ? "E2000" : "WRT320N";
1206 features
= SUP_SES
| SUP_80211N
| SUP_WHAM_LED
| SUP_1000ET
;
1207 if (!nvram_match("t_fix1", (char *)name
)) {
1208 nvram_set("lan_ifnames", "vlan1 eth1");
1209 nvram_set("wan_ifnameX", "vlan2");
1210 nvram_set("wl_ifname", "eth1");
1213 case MODEL_WRT610Nv2
:
1215 name
= nvram_match("boot_hw_model", "E300") ? "E3000" : "WRT610N v2";
1216 features
= SUP_SES
| SUP_80211N
| SUP_WHAM_LED
| SUP_1000ET
;
1218 nvram_set("usb_uhci", "-1");
1220 if (!nvram_match("t_fix1", (char *)name
)) {
1221 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1222 nvram_set("wan_ifnameX", "vlan2");
1223 nvram_set("wl_ifname", "eth1");
1229 features
= SUP_SES
| SUP_80211N
| SUP_1000ET
;
1231 nvram_set("usb_uhci", "-1");
1233 if (!nvram_match("t_fix1", (char *)name
)) {
1234 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1235 nvram_set("wan_ifnameX", "vlan2");
1236 nvram_set("wl_ifname", "eth1");
1239 #endif // CONFIG_BCMWL5
1240 case MODEL_WL500GPv2
:
1242 name
= "WL-500gP v2";
1245 nvram_set("usb_uhci", "-1");
1253 nvram_set("usb_uhci", "-1");
1258 name
= "WL-500g Deluxe";
1259 // features = SUP_SES;
1261 nvram_set("usb_ohci", "-1");
1263 if (!nvram_match("t_fix1", (char *)name
)) {
1264 nvram_set("wl_ifname", "eth1");
1265 nvram_set("lan_ifnames", "vlan0 eth1");
1266 nvram_set("wan_ifnameX", "vlan1");
1267 nvram_unset("wl0gpio0");
1274 if (!nvram_match("t_fix1", (char *)name
)) {
1275 nvram_set("wan_ifnameX", "vlan1");
1276 nvram_set("wl_ifname", "eth1");
1281 name
= "ZXV10 H618B";
1282 features
= SUP_SES
| SUP_AOSS_LED
;
1284 case MODEL_WL1600GL
:
1289 #endif // WL_BSS_INFO_VERSION >= 108
1290 case MODEL_WZRG300N
:
1293 features
= SUP_SES
| SUP_AOSS_LED
| SUP_BRAU
| SUP_80211N
;
1295 case MODEL_WRT160Nv1
:
1298 name
= (model
== MODEL_WRT300N
) ? "WRT300N v1" : "WRT160N v1";
1299 features
= SUP_SES
| SUP_80211N
;
1300 if (!nvram_match("t_fix1", (char *)name
)) {
1301 nvram_set("wan_ifnameX", "eth1");
1302 nvram_set("lan_ifnames", "eth0 eth2");
1305 case MODEL_WRT310Nv1
:
1307 name
= "WRT310N v1";
1308 features
= SUP_SES
| SUP_80211N
| SUP_WHAM_LED
| SUP_1000ET
;
1309 if (!nvram_match("t_fix1", (char *)name
)) {
1310 nvram_set("lan_ifnames", "vlan1 eth1");
1311 nvram_set("wan_ifnameX", "vlan2");
1312 nvram_set("wl_ifname", "eth1");
1318 nvram_set("t_fix1", name
);
1319 sprintf(s
, "%s %s %s", mfr
, name
, ver
);
1322 snprintf(s
, sizeof(s
), "%s %d/%s/%s/%s/%s", mfr
, check_hw_type(),
1323 nvram_safe_get("boardtype"), nvram_safe_get("boardnum"), nvram_safe_get("boardrev"), nvram_safe_get("boardflags"));
1326 nvram_set("t_model_name", s
);
1328 nvram_set("pa0maxpwr", "400"); // allow Tx power up tp 400 mW, needed for ND only
1330 sprintf(s
, "0x%lX", features
);
1331 nvram_set("t_features", s
);
1334 note: set wan_ifnameX if wan_ifname needs to be overriden
1337 if (nvram_is_empty("wan_ifnameX")) {
1339 nvram_set("wan_ifnameX", ((strtoul(nvram_safe_get("boardflags"), NULL
, 0) & BFL_ENETVLAN
) ||
1340 (check_hw_type() == HW_BCM4712
)) ? "vlan1" : "eth1");
1342 p
= nvram_safe_get("wan_ifname");
1343 if ((*p
== 0) || (nvram_match("wl_ifname", p
))) {
1344 p
= ((strtoul(nvram_safe_get("boardflags"), NULL
, 0) & BFL_ENETVLAN
) ||
1345 (check_hw_type() == HW_BCM4712
)) ? "vlan1" : "eth1";
1347 nvram_set("wan_ifnameX", p
);
1351 nvram_set("wl_hwaddr", ""); // zzz- when disabling wireless, we must get null wireless mac ??
1353 //!!TB - do not force country code here to allow nvram override
1354 //nvram_set("wl_country", "JP");
1355 //nvram_set("wl_country_code", "JP");
1356 nvram_set("wan_get_dns", "");
1357 nvram_set("wan_get_domain", "");
1358 nvram_set("ppp_get_ip", "");
1359 nvram_set("action_service", "");
1360 nvram_set("jffs2_format", "0");
1361 nvram_set("rrules_radio", "-1");
1362 nvram_unset("https_crt_gen");
1363 nvram_unset("log_wmclear");
1365 nvram_set("ipv6_get_dns", "");
1367 #ifdef TCONFIG_MEDIA_SERVER
1368 nvram_unset("ms_rescan");
1370 if (nvram_get_int("http_id_gen") == 1) nvram_unset("http_id");
1372 nvram_unset("sch_rboot_last");
1373 nvram_unset("sch_rcon_last");
1374 nvram_unset("sch_c1_last");
1375 nvram_unset("sch_c2_last");
1376 nvram_unset("sch_c3_last");
1378 nvram_set("brau_state", "");
1379 if ((features
& SUP_BRAU
) == 0) nvram_set("script_brau", "");
1380 if ((features
& SUP_SES
) == 0) nvram_set("sesx_script", "");
1382 if ((features
& SUP_1000ET
) == 0) nvram_set("jumbo_frame_enable", "0");
1384 // compatibility with old versions
1385 if (nvram_match("wl_net_mode", "disabled")) {
1386 nvram_set("wl_radio", "0");
1387 nvram_set("wl_net_mode", "mixed");
1393 /* Get the special files from nvram and copy them to disc.
1394 * These were files saved with "nvram setfile2nvram <filename>".
1395 * Better hope that they were saved with full pathname.
1397 static void load_files_from_nvram(void)
1401 char buf
[NVRAM_SPACE
];
1403 if (nvram_getall(buf
, sizeof(buf
)) != 0)
1406 for (name
= buf
; *name
; name
+= strlen(name
) + 1) {
1407 if (strncmp(name
, "FILE:", 5) == 0) { /* This special name marks a file to get. */
1408 if ((cp
= strchr(name
, '=')) == NULL
)
1411 syslog(LOG_INFO
, "Loading file '%s' from nvram", name
+ 5);
1412 nvram_nvram2file(name
, name
+ 5);
1413 if (memcmp(".autorun", cp
- 8, 9) == 0)
1417 /* Start any autorun files that may have been loaded into one of the standard places. */
1419 run_nvscript(".autorun", NULL
, 3);
1422 #if defined(LINUX26) && defined(TCONFIG_USB)
1423 static inline void tune_min_free_kbytes(void)
1425 struct sysinfo info
;
1427 memset(&info
, 0, sizeof(struct sysinfo
));
1429 if (info
.totalram
>= 55 * 1024 * 1024) {
1430 // If we have 64MB+ RAM, tune min_free_kbytes
1431 // to reduce page allocation failure errors.
1432 f_write_string("/proc/sys/vm/min_free_kbytes", "8192", 0, 0);
1437 static void sysinit(void)
1439 static int noconsole
= 0;
1440 static const time_t tm
= 0;
1449 mount("proc", "/proc", "proc", 0, NULL
);
1450 mount("tmpfs", "/tmp", "tmpfs", 0, NULL
);
1453 mount("devfs", "/dev", "tmpfs", MS_MGC_VAL
| MS_NOATIME
, NULL
);
1454 mknod("/dev/null", S_IFCHR
| 0666, makedev(1, 3));
1455 mknod("/dev/console", S_IFCHR
| 0600, makedev(5, 1));
1456 mount("sysfs", "/sys", "sysfs", MS_MGC_VAL
, NULL
);
1457 mkdir("/dev/shm", 0777);
1458 mkdir("/dev/pts", 0777);
1459 mknod("/dev/pts/ptmx", S_IRWXU
|S_IFCHR
, makedev(5, 2));
1460 mknod("/dev/pts/0", S_IRWXU
|S_IFCHR
, makedev(136, 0));
1461 mknod("/dev/pts/1", S_IRWXU
|S_IFCHR
, makedev(136, 1));
1462 mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL
, NULL
);
1465 if (console_init()) noconsole
= 1;
1469 static const char *mkd
[] = {
1470 "/tmp/etc", "/tmp/var", "/tmp/home", "/tmp/mnt",
1471 "/tmp/splashd", //!!Victek
1472 "/tmp/share", "/var/webmon", // !!TB
1473 "/var/log", "/var/run", "/var/tmp", "/var/lib", "/var/lib/misc",
1474 "/var/spool", "/var/spool/cron", "/var/spool/cron/crontabs",
1475 "/tmp/var/wwwext", "/tmp/var/wwwext/cgi-bin", // !!TB - CGI support
1479 for (i
= 0; mkd
[i
]; ++i
) {
1480 mkdir(mkd
[i
], 0755);
1482 mkdir("/var/lock", 0777);
1483 mkdir("/var/tmp/dhcp", 0777);
1484 mkdir("/home/root", 0700);
1485 chmod("/tmp", 0777);
1486 f_write("/etc/hosts", NULL
, 0, 0, 0644); // blank
1487 f_write("/etc/fstab", NULL
, 0, 0, 0644); // !!TB - blank
1488 simple_unlock("cron");
1489 simple_unlock("firewall");
1490 simple_unlock("restrictions");
1493 if ((d
= opendir("/rom/etc")) != NULL
) {
1494 while ((de
= readdir(d
)) != NULL
) {
1495 if (de
->d_name
[0] == '.') continue;
1496 snprintf(s
, sizeof(s
), "%s/%s", "/rom/etc", de
->d_name
);
1497 snprintf(t
, sizeof(t
), "%s/%s", "/etc", de
->d_name
);
1502 symlink("/proc/mounts", "/etc/mtab");
1504 #ifdef TCONFIG_SAMBASRV
1505 if ((d
= opendir("/usr/codepages")) != NULL
) {
1506 while ((de
= readdir(d
)) != NULL
) {
1507 if (de
->d_name
[0] == '.') continue;
1508 snprintf(s
, sizeof(s
), "/usr/codepages/%s", de
->d_name
);
1509 snprintf(t
, sizeof(t
), "/usr/share/%s", de
->d_name
);
1517 eval("hotplug2", "--coldplug");
1520 static const char *dn
[] = {
1521 "null", "zero", "random", "urandom", "full", "ptmx", "nvram",
1524 for (i
= 0; dn
[i
]; ++i
) {
1525 snprintf(s
, sizeof(s
), "/dev/%s", dn
[i
]);
1528 chmod("/dev/gpio", 0660);
1531 set_action(ACT_IDLE
);
1533 for (i
= 0; defenv
[i
]; ++i
) {
1538 printf("\n\nHit ENTER for console...\n\n");
1545 // disable IPv6 by default on all interfaces
1546 f_write_string("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1", 0, 0);
1549 for (i
= 0; i
< sizeof(fatalsigs
) / sizeof(fatalsigs
[0]); i
++) {
1550 signal(fatalsigs
[i
], handle_fatalsigs
);
1552 signal(SIGCHLD
, handle_reap
);
1554 switch (model
= get_model()) {
1555 case MODEL_WR850GV1
:
1556 case MODEL_WR850GV2
:
1557 // need to cleanup some variables...
1558 if ((nvram_get("t_model") == NULL
) && (nvram_get("MyFirmwareVersion") != NULL
)) {
1559 nvram_unset("MyFirmwareVersion");
1560 nvram_set("restore_defaults", "1");
1565 #ifdef CONFIG_BCMWL5
1566 // ctf must be loaded prior to any other modules
1567 if (nvram_invmatch("ctf_disable", "1"))
1576 switch (hardware
= check_hw_type()) {
1578 modprobe("bcm57xx");
1589 eval("nvram", "defaults", "--initcheck");
1592 // set the packet size
1593 if (nvram_get_int("jumbo_frame_enable")) {
1594 // only set the size here - 'enable' flag is set by the driver
1595 // eval("et", "robowr", "0x40", "0x01", "0x1F"); // (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)
1596 eval("et", "robowr", "0x40", "0x05", nvram_safe_get("jumbo_frame_size"));
1599 klogctl(8, NULL
, nvram_get_int("console_loglevel"));
1601 #if defined(LINUX26) && defined(TCONFIG_USB)
1602 tune_min_free_kbytes();
1605 set_host_domain_name();
1611 if (!noconsole
) xstart("console");
1613 i
= nvram_get_int("sesx_led");
1614 led(LED_AMBER
, (i
& 1) != 0);
1615 led(LED_WHITE
, (i
& 2) != 0);
1616 led(LED_AOSS
, (i
& 4) != 0);
1617 led(LED_BRIDGE
, (i
& 8) != 0);
1621 int init_main(int argc
, char *argv
[])
1627 nvram_unset("debug_rc_svc");
1631 sigemptyset(&sigset
);
1632 for (i
= 0; i
< sizeof(initsigs
) / sizeof(initsigs
[0]); i
++) {
1633 sigaddset(&sigset
, initsigs
[i
]);
1635 sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
1637 #if defined(DEBUG_NOISY)
1638 nvram_set("debug_logeval", "1");
1639 nvram_set("debug_cprintf", "1");
1640 nvram_set("debug_cprintf_file", "1");
1641 nvram_set("debug_ddns", "1");
1646 state
= SIGUSR2
; /* START */
1649 TRACE_PT("main loop signal/state=%d\n", state
);
1652 case SIGUSR1
: /* USER1: service handler */
1656 case SIGHUP
: /* RESTART */
1657 case SIGINT
: /* STOP */
1658 case SIGQUIT
: /* HALT */
1659 case SIGTERM
: /* REBOOT */
1661 unlink("/var/notice/sysup");
1663 run_nvscript("script_shut", NULL
, 10);
1671 if ((state
== SIGTERM
/* REBOOT */) ||
1672 (state
== SIGQUIT
/* HALT */)) {
1673 remove_storage_main(1);
1676 shutdn(state
== SIGTERM
/* REBOOT */);
1679 if (state
== SIGINT
/* STOP */) {
1683 // SIGHUP (RESTART) falls through
1685 case SIGUSR2
: /* START */
1686 SET_LED(RELEASE_WAN_CONTROL
);
1689 load_files_from_nvram();
1692 fd
= file_lock("usb"); // hold off automount processing
1695 run_nvscript("script_init", NULL
, 2);
1697 file_unlock(fd
); // allow to process usb hotplug events
1700 * On RESTART some partitions can stay mounted if they are busy at the moment.
1701 * In that case USB drivers won't unload, and hotplug won't kick off again to
1702 * remount those drives that actually got unmounted. Make sure to remount ALL
1703 * partitions here by simulating hotplug event.
1705 if (state
== SIGHUP
/* RESTART */)
1706 add_remove_usbhost("-1", 1);
1717 #ifdef CONFIG_BCMWL5
1719 /* Restart NAS one more time - for some reason without
1720 * this the new driver doesn't always bring WDS up.
1727 if (wl_security_on()) {
1731 #endif /* TCONFIG_VLAN */
1734 syslog(LOG_INFO
, "%s: Tomato %s", nvram_safe_get("t_model_name"), tomato_version
);
1737 notice_set("sysup", "");
1741 chld_reap(0); /* Periodically reap zombies. */
1743 sigwait(&sigset
, &state
);
1749 int reboothalt_main(int argc
, char *argv
[])
1751 int reboot
= (strstr(argv
[0], "reboot") != NULL
);
1752 puts(reboot
? "Rebooting..." : "Shutting down...");
1755 kill(1, reboot
? SIGTERM
: SIGQUIT
);
1757 /* In the case we're hung, we'll get stuck and never actually reboot.
1758 * The only way out is to pull power.
1759 * So after 'reset_wait' seconds (default: 20), forcibly crash & restart.
1762 int wait
= nvram_get_int("reset_wait") ? : 20;
1763 if ((wait
< 10) || (wait
> 120)) wait
= 10;
1765 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
1767 puts("Still running... Doing machine reset.");
1769 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
1771 f_write("/proc/sysrq-trigger", "b", 1, 0 , 0); /* machine reset */