nas/net restart tweaks
[tomato.git] / release / src / router / rc / init.c
blobf76417f75dc7ca94ec71b00836d06c5d6fb4613d
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 #endif
29 #include <wlutils.h>
30 #include <bcmdevs.h>
32 #define SHELL "/bin/sh"
35 enum {
36 RESTART,
37 STOP,
38 START,
39 USER1,
40 IDLE,
41 REBOOT,
42 HALT,
43 INIT
46 static int fatalsigs[] = {
47 SIGILL,
48 SIGABRT,
49 SIGFPE,
50 SIGPIPE,
51 SIGBUS,
52 SIGSYS,
53 SIGTRAP,
54 SIGPWR
57 static int initsigs[] = {
58 SIGHUP,
59 SIGUSR1,
60 SIGUSR2,
61 SIGINT,
62 SIGQUIT,
63 SIGTERM
66 static char *defenv[] = {
67 "TERM=vt100",
68 "HOME=/",
69 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
70 "SHELL=" SHELL,
71 "USER=root",
72 NULL
75 static int noconsole = 0;
76 static volatile int state = INIT;
77 static volatile int signaled = -1;
80 /* Set terminal settings to reasonable defaults */
81 static void set_term(int fd)
83 struct termios tty;
85 tcgetattr(fd, &tty);
87 /* set control chars */
88 tty.c_cc[VINTR] = 3; /* C-c */
89 tty.c_cc[VQUIT] = 28; /* C-\ */
90 tty.c_cc[VERASE] = 127; /* C-? */
91 tty.c_cc[VKILL] = 21; /* C-u */
92 tty.c_cc[VEOF] = 4; /* C-d */
93 tty.c_cc[VSTART] = 17; /* C-q */
94 tty.c_cc[VSTOP] = 19; /* C-s */
95 tty.c_cc[VSUSP] = 26; /* C-z */
97 /* use line dicipline 0 */
98 tty.c_line = 0;
100 /* Make it be sane */
101 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
102 tty.c_cflag |= CREAD|HUPCL|CLOCAL;
105 /* input modes */
106 tty.c_iflag = ICRNL | IXON | IXOFF;
108 /* output modes */
109 tty.c_oflag = OPOST | ONLCR;
111 /* local modes */
112 tty.c_lflag =
113 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
115 tcsetattr(fd, TCSANOW, &tty);
118 static int console_init(void)
120 int fd;
122 /* Clean up */
123 ioctl(0, TIOCNOTTY, 0);
124 close(0);
125 close(1);
126 close(2);
127 setsid();
129 /* Reopen console */
130 if ((fd = open(_PATH_CONSOLE, O_RDWR)) < 0) {
131 /* Avoid debug messages is redirected to socket packet if no exist a UART chip, added by honor, 2003-12-04 */
132 open("/dev/null", O_RDONLY);
133 open("/dev/null", O_WRONLY);
134 open("/dev/null", O_WRONLY);
135 perror(_PATH_CONSOLE);
136 return errno;
138 dup2(fd, 0);
139 dup2(fd, 1);
140 dup2(fd, 2);
142 ioctl(0, TIOCSCTTY, 1);
143 tcsetpgrp(0, getpgrp());
144 set_term(0);
146 return 0;
150 * Waits for a file descriptor to change status or unblocked signal
151 * @param fd file descriptor
152 * @param timeout seconds to wait before timing out or 0 for no timeout
153 * @return 1 if descriptor changed status or 0 if timed out or -1 on error
155 static int waitfor(int fd, int timeout)
157 fd_set rfds;
158 struct timeval tv = { timeout, 0 };
160 FD_ZERO(&rfds);
161 FD_SET(fd, &rfds);
162 return select(fd + 1, &rfds, NULL, NULL, (timeout > 0) ? &tv : NULL);
165 static pid_t run_shell(int timeout, int nowait)
167 pid_t pid;
168 int sig;
170 /* Wait for user input */
171 if (waitfor(STDIN_FILENO, timeout) <= 0) return 0;
173 switch (pid = fork()) {
174 case -1:
175 perror("fork");
176 return 0;
177 case 0:
178 /* Reset signal handlers set for parent process */
179 for (sig = 0; sig < (_NSIG-1); sig++)
180 signal(sig, SIG_DFL);
182 /* Reopen console */
183 console_init();
184 printf("\n\nTomato %s\n\n", tomato_version);
186 /* Now run it. The new program will take over this PID,
187 * so nothing further in init.c should be run. */
188 execve(SHELL, (char *[]) { SHELL, NULL }, defenv);
190 /* We're still here? Some error happened. */
191 perror(SHELL);
192 exit(errno);
193 default:
194 if (nowait) {
195 return pid;
197 else {
198 waitpid(pid, NULL, 0);
199 return 0;
204 static void shutdn(int rb)
206 int i;
207 int act;
208 sigset_t ss;
210 _dprintf("shutdn rb=%d\n", rb);
212 sigemptyset(&ss);
213 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++)
214 sigaddset(&ss, fatalsigs[i]);
215 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++)
216 sigaddset(&ss, initsigs[i]);
217 sigaddset(&ss, SIGCHLD);
218 sigprocmask(SIG_BLOCK, &ss, NULL);
220 for (i = 30; i > 0; --i) {
221 if (((act = check_action()) == ACT_IDLE) || (act == ACT_REBOOT)) break;
222 cprintf("Busy with %d. Waiting before shutdown... %d\n", act, i);
223 sleep(1);
225 set_action(ACT_REBOOT);
227 cprintf("TERM\n");
228 kill(-1, SIGTERM);
229 sleep(2);
230 sync();
232 cprintf("KILL\n");
233 kill(-1, SIGKILL);
234 sleep(1);
235 sync();
237 umount("/jffs"); // may hang if not
238 sleep(1);
240 if (rb != -1) {
241 led(LED_WLAN, 0);
242 if (rb == 0) {
243 for (i = 4; i > 0; --i) {
244 led(LED_DMZ, 1);
245 led(LED_WHITE, 1);
246 usleep(250000);
247 led(LED_DMZ, 0);
248 led(LED_WHITE, 0);
249 usleep(250000);
254 reboot(rb ? RB_AUTOBOOT : RB_HALT_SYSTEM);
256 do {
257 sleep(1);
258 } while (1);
261 static void handle_fatalsigs(int sig)
263 _dprintf("fatal sig=%d\n", sig);
264 shutdn(-1);
267 void handle_reap(int sig)
269 while (waitpid(-1, NULL, WNOHANG) > 0) {
274 static void handle_initsigs(int sig)
276 // TRACE_PT("sig=%d state=%d, signaled=%d\n", sig, state, signaled);
278 switch (sig) {
279 case SIGHUP:
280 signaled = RESTART;
281 break;
282 case SIGUSR1:
283 signaled = USER1;
284 break;
285 case SIGUSR2:
286 signaled = START;
287 break;
288 case SIGINT:
289 signaled = STOP;
290 break;
291 case SIGTERM:
292 signaled = REBOOT;
293 break;
294 case SIGQUIT:
295 signaled = HALT;
296 break;
300 static int check_nv(const char *name, const char *value)
302 const char *p;
303 if (!nvram_match("manual_boot_nv", "1")) {
304 if (((p = nvram_get(name)) == NULL) || (strcmp(p, value) != 0)) {
305 // cprintf("Error: Critical variable %s is invalid. Resetting.\n", name);
306 nvram_set(name, value);
307 return 1;
310 return 0;
313 static void check_bootnv(void)
315 int dirty;
316 int hardware;
317 int model;
319 model = get_model();
320 dirty = 0;
322 switch (model) {
323 case MODEL_WNR3500L:
324 dirty |= check_nv("boardflags", "0x00000710"); // needed to enable USB
325 dirty |= check_nv("vlan1ports", "4 3 2 1 8*");
326 dirty |= check_nv("vlan2ports", "0 8");
327 break;
328 case MODEL_RTN10:
329 dirty |= check_nv("vlan1ports", "4 5");
330 break;
331 case MODEL_RTN12:
332 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
333 dirty |= check_nv("vlan1ports", "4 5");
334 break;
335 case MODEL_RTN16:
336 dirty |= check_nv("vlan2hwname", "et0");
337 dirty |= check_nv("vlan1ports", "4 3 2 1 8*");
338 dirty |= check_nv("vlan2ports", "0 8");
339 break;
341 case MODEL_WRT54G:
342 if (strncmp(nvram_safe_get("pmon_ver"), "CFE", 3) != 0) return;
344 hardware = check_hw_type();
345 if (!nvram_get("boardtype") ||
346 !nvram_get("boardnum") ||
347 !nvram_get("boardflags") ||
348 !nvram_get("clkfreq") ||
349 !nvram_get("os_flash_addr") ||
350 !nvram_get("dl_ram_addr") ||
351 !nvram_get("os_ram_addr") ||
352 !nvram_get("scratch") ||
353 !nvram_get("et0macaddr") ||
354 ((hardware != HW_BCM4704_BCM5325F) && (!nvram_get("vlan0ports") || !nvram_get("vlan0hwname")))) {
355 cprintf("Unable to find critical settings, erasing NVRAM\n");
356 mtd_erase("nvram");
357 goto REBOOT;
360 switch (hardware) {
361 case HW_BCM5325E:
362 /* Lower the DDR ram drive strength , the value will be stable for all boards
363 Latency 3 is more stable for all ddr 20050420 by honor */
364 dirty |= check_nv("sdram_init", "0x010b");
365 dirty |= check_nv("sdram_config", "0x0062");
366 if (!nvram_match("debug_clkfix", "0")) {
367 dirty |= check_nv("clkfreq", "216");
369 if (dirty) {
370 nvram_set("sdram_ncdl", "0x0");
372 dirty |= check_nv("pa0itssit", "62");
373 dirty |= check_nv("pa0b0", "0x15eb");
374 dirty |= check_nv("pa0b1", "0xfa82");
375 dirty |= check_nv("pa0b2", "0xfe66");
376 //dirty |= check_nv("pa0maxpwr", "0x4e");
377 break;
378 case HW_BCM5352E: // G v4, GS v3, v4
379 dirty |= check_nv("sdram_init", "0x010b");
380 dirty |= check_nv("sdram_config", "0x0062");
381 if (dirty) {
382 nvram_set("sdram_ncdl", "0x0");
384 dirty |= check_nv("pa0itssit", "62");
385 dirty |= check_nv("pa0b0", "0x168b");
386 dirty |= check_nv("pa0b1", "0xfabf");
387 dirty |= check_nv("pa0b2", "0xfeaf");
388 //dirty |= check_nv("pa0maxpwr", "0x4e");
389 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
390 break;
391 case HW_BCM5354G:
392 dirty |= check_nv("pa0itssit", "62");
393 dirty |= check_nv("pa0b0", "0x1326");
394 dirty |= check_nv("pa0b1", "0xFB51");
395 dirty |= check_nv("pa0b2", "0xFE87");
396 //dirty |= check_nv("pa0maxpwr", "0x4e");
397 break;
398 case HW_BCM4704_BCM5325F:
399 // nothing to do
400 break;
401 default:
402 dirty |= check_nv("pa0itssit", "62");
403 dirty |= check_nv("pa0b0", "0x170c");
404 dirty |= check_nv("pa0b1", "0xfa24");
405 dirty |= check_nv("pa0b2", "0xfe70");
406 //dirty |= check_nv("pa0maxpwr", "0x48");
407 break;
409 break;
411 } // switch (model)
413 if (dirty) {
414 nvram_commit();
415 REBOOT: // do a simple reboot
416 sync();
417 reboot(RB_AUTOBOOT);
418 exit(0);
422 static int init_nvram(void)
424 unsigned long features;
425 int model;
426 const char *mfr;
427 const char *name;
428 char s[256];
429 unsigned long bf;
430 unsigned long n;
432 model = get_model();
433 sprintf(s, "%d", model);
434 nvram_set("t_model", s);
436 mfr = "Broadcom";
437 name = NULL;
438 features = 0;
439 switch (model) {
440 case MODEL_WRT54G:
441 mfr = "Linksys";
442 name = "WRT54G/GS/GL";
443 switch (check_hw_type()) {
444 case HW_BCM4712:
445 nvram_set("gpio2", "adm_eecs");
446 nvram_set("gpio3", "adm_eesk");
447 nvram_unset("gpio4");
448 nvram_set("gpio5", "adm_eedi");
449 nvram_set("gpio6", "adm_rc");
450 break;
451 case HW_BCM4702:
452 nvram_unset("gpio2");
453 nvram_unset("gpio3");
454 nvram_unset("gpio4");
455 nvram_unset("gpio5");
456 nvram_unset("gpio6");
457 break;
458 case HW_BCM5352E:
459 nvram_set("opo", "0x0008");
460 nvram_set("ag0", "0x02");
461 // drop
462 default:
463 nvram_set("gpio2", "ses_led");
464 nvram_set("gpio3", "ses_led2");
465 nvram_set("gpio4", "ses_button");
466 features = SUP_SES | SUP_WHAM_LED;
467 break;
469 break;
470 case MODEL_WTR54GS:
471 mfr = "Linksys";
472 name = "WTR54GS";
473 if (!nvram_match("t_fix1", (char *)name)) {
474 nvram_set ("vlan0hwname", "et0");
475 nvram_set ("vlan1hwname", "et0");
476 nvram_set("vlan0ports", "0 5*");
477 nvram_set("vlan1ports", "1 5");
478 nvram_set("vlan_enable", "1");
479 nvram_set("lan_ifnames", "vlan0 eth1");
480 nvram_set("gpio2", "ses_button");
481 nvram_set("reset_gpio", "7");
484 nvram_set("pa0itssit", "62");
485 nvram_set("pa0b0", "0x1542");
486 nvram_set("pa0b1", "0xfacb");
487 nvram_set("pa0b2", "0xfec7");
488 //nvram_set("pa0maxpwr", "0x4c");
489 features = SUP_SES;
490 break;
491 case MODEL_WRTSL54GS:
492 mfr = "Linksys";
493 name = "WRTSL54GS";
494 features = SUP_SES | SUP_WHAM_LED;
495 break;
496 case MODEL_WHRG54S:
497 mfr = "Buffalo";
498 name = "WHR-G54S";
499 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
500 break;
501 case MODEL_WHRHPG54:
502 case MODEL_WZRRSG54HP:
503 case MODEL_WZRHPG54:
504 mfr = "Buffalo";
505 features = SUP_SES | SUP_AOSS_LED | SUP_HPAMP;
506 switch (model) {
507 case MODEL_WZRRSG54HP:
508 name = "WZR-RS-G54HP";
509 break;
510 case MODEL_WZRHPG54:
511 name = "WZR-HP-G54";
512 break;
513 default:
514 name = "WHR-HP-G54";
515 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_HPAMP;
516 break;
519 bf = strtoul(nvram_safe_get("boardflags"), NULL, 0);
520 switch (bf) {
521 case 0x0758:
522 case 0x1758:
523 case 0x2758:
524 case 0x3758:
525 if (nvram_match("wlx_hpamp", "")) {
526 if (nvram_get_int("wl_txpwr") > 10) nvram_set("wl_txpwr", "10");
527 nvram_set("wlx_hpamp", "1");
528 nvram_set("wlx_hperx", "0");
531 n = bf;
532 if (nvram_match("wlx_hpamp", "0")) {
533 n &= ~0x2000UL;
535 else {
536 n |= 0x2000UL;
538 if (nvram_match("wlx_hperx", "0")) {
539 n |= 0x1000UL;
541 else {
542 n &= ~0x1000UL;
544 if (bf != n) {
545 sprintf(s, "0x%lX", n);
546 nvram_set("boardflags", s);
548 break;
549 default:
550 syslog(LOG_WARNING, "Unexpected: boardflag=%lX", bf);
551 break;
553 break;
554 case MODEL_WBRG54:
555 mfr = "Buffalo";
556 name = "WBR-G54";
557 nvram_set("wl0gpio0", "130");
558 break;
559 case MODEL_WBR2G54:
560 mfr = "Buffalo";
561 name = "WBR2-G54";
562 features = SUP_SES | SUP_AOSS_LED;
563 break;
564 case MODEL_WHR2A54G54:
565 mfr = "Buffalo";
566 name = "WHR2-A54G54";
567 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
568 break;
569 case MODEL_WHR3AG54:
570 mfr = "Buffalo";
571 name = "WHR3-AG54";
572 features = SUP_SES | SUP_AOSS_LED;
573 break;
574 case MODEL_WZRG54:
575 mfr = "Buffalo";
576 name = "WZR-G54";
577 features = SUP_SES | SUP_AOSS_LED;
578 break;
579 case MODEL_WZRRSG54:
580 mfr = "Buffalo";
581 name = "WZR-RS-G54";
582 features = SUP_SES | SUP_AOSS_LED;
583 break;
584 case MODEL_WVRG54NF:
585 mfr = "Buffalo";
586 name = "WVR-G54-NF";
587 features = SUP_SES;
588 break;
589 case MODEL_WZRG108:
590 mfr = "Buffalo";
591 name = "WZR-G108";
592 features = SUP_SES | SUP_AOSS_LED;
593 break;
594 case MODEL_RT390W:
595 mfr = "Fuji";
596 name = "RT390W";
597 break;
598 case MODEL_WR850GV1:
599 mfr = "Motorola";
600 name = "WR850G v1";
601 features = SUP_NONVE;
602 break;
603 case MODEL_WR850GV2:
604 mfr = "Motorola";
605 name = "WR850G v2/v3";
606 features = SUP_NONVE;
607 break;
608 case MODEL_WL500GP:
609 mfr = "Asus";
610 name = "WL-500gP";
611 features = SUP_SES;
612 if (!nvram_match("t_fix1", (char *)name)) {
613 nvram_set("t_fix1", name);
614 nvram_set("sdram_init", "0x0009"); // 32MB; defaults: 0x000b, 0x0009
615 nvram_set("vlan1ports", "0 5"); // default: 0 5u
616 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3"); // set to "vlan0 eth2" by DD-WRT; default: vlan0 eth1
617 // !!TB - WLAN LED fix
618 nvram_set("wl0gpio0", "136");
620 break;
621 case MODEL_WL500W:
622 mfr = "Asus";
623 name = "WL-500W";
624 features = SUP_SES | SUP_80211N;
625 /* fix WL500W mac adresses for WAN port */
626 if (nvram_match("et1macaddr", "00:90:4c:a1:00:2d"))
627 nvram_set("et1macaddr", nvram_get("et0macaddr"));
628 /* fix AIR LED */
629 if (!nvram_get("wl0gpio0") || nvram_match("wl0gpio0", "2"))
630 nvram_set("wl0gpio0", "0x88");
631 break;
632 case MODEL_WL500GE:
633 mfr = "Asus";
634 name = "WL-500gE";
635 // features = ?
636 if (!nvram_match("t_fix1", (char *)name)) {
637 nvram_set("t_fix1", name);
638 nvram_set("vlan1ports", "0 5"); // default: 0 5u
640 break;
641 case MODEL_WX6615GT:
642 mfr = "SparkLAN";
643 name = "WX-6615GT";
644 // features = ?
645 break;
646 case MODEL_MN700:
647 mfr = "Microsoft";
648 name = "MN-700";
649 break;
650 case MODEL_WR100:
651 mfr = "Viewsonic";
652 name = "WR100";
653 break;
654 case MODEL_WLA2G54L:
655 mfr = "Buffalo";
656 name = "WLA2-G54L";
657 if (!nvram_match("t_fix1", (char *)name)) {
658 nvram_set("t_fix1", name);
659 nvram_set("lan_ifnames", "vlan0 eth1 eth2");
660 nvram_set("wl_ifname", "eth1");
661 nvram_set("wan_ifname", "none");
663 break;
664 case MODEL_TM2300:
665 mfr = "Dell";
666 name = "TrueMobile 2300";
667 break;
675 #ifndef WL_BSS_INFO_VERSION
676 #error WL_BSS_INFO_VERSION
677 #endif
678 #if WL_BSS_INFO_VERSION >= 108
680 case MODEL_WRH54G:
681 mfr = "Linksys";
682 name = "WRH54G";
684 nvram_set("opo", "12");
685 break;
687 case MODEL_WHRG125:
688 mfr = "Buffalo";
689 name = "WHR-G125";
690 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
692 nvram_set("opo", "0x0008");
693 nvram_set("ag0", "0x0C");
694 break;
695 case MODEL_RTN10:
696 mfr = "Asus";
697 name = "RT-N10";
698 features = SUP_SES | SUP_80211N;
699 if (!nvram_match("t_fix1", (char *)name)) {
700 nvram_set("lan_ifnames", "vlan0 eth1");
701 nvram_set("wan_ifnameX", "vlan1");
702 nvram_set("wl_ifname", "eth1");
703 nvram_set("t_fix1", name);
705 break;
706 case MODEL_RTN12:
707 mfr = "Asus";
708 name = "RT-N12";
709 features = SUP_SES | SUP_BRAU | SUP_80211N;
710 if (!nvram_match("t_fix1", (char *)name)) {
711 nvram_set("lan_ifnames", "vlan0 eth1");
712 nvram_set("wan_ifnameX", "vlan1");
713 nvram_set("wl_ifname", "eth1");
714 nvram_set("t_fix1", name);
716 break;
717 case MODEL_RTN16:
718 mfr = "Asus";
719 name = "RT-N16";
720 features = SUP_SES | SUP_80211N | SUP_1000ET;
721 if (!nvram_match("t_fix1", (char *)name)) {
722 nvram_set("lan_ifnames", "vlan1 eth1");
723 nvram_set("wan_ifnameX", "vlan2");
724 nvram_set("wl_ifname", "eth1");
725 nvram_set("vlan_enable", "1");
726 nvram_set("t_fix1", name);
728 break;
729 case MODEL_WNR3500L:
730 mfr = "Netgear";
731 name = "WNR3500L";
732 features = SUP_SES | SUP_80211N | SUP_1000ET;
733 if (!nvram_match("t_fix1", (char *)name)) {
734 nvram_set("sromrev", "3");
735 nvram_set("lan_ifnames", "vlan1 eth1");
736 nvram_set("wan_ifnameX", "vlan2");
737 nvram_set("wl_ifname", "eth1");
738 nvram_set("t_fix1", name);
740 break;
741 case MODEL_WL500GPv2:
742 mfr = "Asus";
743 name = "WL-500gP v2";
744 features = SUP_SES;
745 if (!nvram_match("t_fix1", (char *)name)) {
746 if (nvram_match("vlan1ports", "4 5u")) {
747 nvram_set("vlan1ports", "4 5");
749 else if (nvram_match("vlan1ports", "0 5u")) { // 520GU?
750 nvram_set("vlan1ports", "0 5");
752 nvram_set("t_fix1", name);
754 /* fix AIR LED */
755 if (nvram_match("wl0gpio1", "0x02"))
756 nvram_set("wl0gpio1", "0x88");
757 break;
758 case MODEL_WL520GU:
759 mfr = "Asus";
760 name = "WL-520GU";
761 features = SUP_SES;
762 if (!nvram_match("t_fix1", (char *)name)) {
763 nvram_set("t_fix1", name);
764 nvram_set("vlan1ports", "0 5");
765 // !!TB - LED fix
766 nvram_set("wl0gpio0", "0");
767 nvram_set("wl0gpio1", "136");
768 nvram_set("wl0gpio2", "0");
769 nvram_set("wl0gpio3", "0");
771 break;
772 case MODEL_DIR320:
773 mfr = "D-Link";
774 name = "DIR-320";
775 features = SUP_SES;
776 if (nvram_match("wl0gpio0", "255"))
778 nvram_set("wl0gpio0", "8");
779 nvram_set("wl0gpio1", "0");
780 nvram_set("wl0gpio2", "0");
781 nvram_set("wl0gpio3", "0");
783 if (!nvram_match("t_fix1", (char *)name)) {
784 nvram_set("t_fix1", name);
785 nvram_unset( "vlan2ports" );
786 nvram_unset( "vlan2hwname" );
787 nvram_set("vlan1hwname", "et0");
788 nvram_set("vlan1ports", "0 5");
789 nvram_set("wandevs", "vlan1");
790 nvram_set("wan_ifname", "vlan1");
791 nvram_set("wan_ifnames", "vlan1");
792 nvram_set("wan0_ifname", "vlan1");
793 nvram_set("wan0_ifnames", "vlan1");
795 break;
796 #endif
797 #if TOMATO_N
798 case MODEL_WZRG300N:
799 mfr = "Buffalo";
800 name = "WZR-G300N";
801 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_80211N;
802 break;
803 case MODEL_WRT300N:
804 mfr = "Linksys";
805 name = "WRT300N";
806 features = SUP_SES | SUP_80211N;
807 break;
808 #endif
811 if (name) {
812 sprintf(s, "%s %s", mfr, name);
814 else {
815 snprintf(s, sizeof(s), "%s %d/%s/%s/%s/%s", mfr, check_hw_type(),
816 nvram_safe_get("boardtype"), nvram_safe_get("boardnum"), nvram_safe_get("boardrev"), nvram_safe_get("boardflags"));
817 s[64] = 0;
819 nvram_set("t_model_name", s);
821 nvram_set("pa0maxpwr", "251"); // allow Tx power up tp 251 mW, needed for ND only
823 sprintf(s, "0x%lX", features);
824 nvram_set("t_features", s);
829 note: set wan_ifnameX if wan_ifname needs to be overriden
832 if (nvram_is_empty("wan_ifnameX")) {
833 #if 1
834 nvram_set("wan_ifnameX", ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
835 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1");
836 #else
837 p = nvram_safe_get("wan_ifname");
838 if ((*p == 0) || (nvram_match("wl_ifname", p))) {
839 p = ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
840 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1";
842 nvram_set("wan_ifnameX", p);
843 #endif
847 nvram_set("wl_hwaddr", ""); // when disabling wireless, we must get null wireless mac ??
848 //!!TB - do not force country code here to allow nvram override
849 //nvram_set("wl_country", "JP");
850 //nvram_set("wl_country_code", "JP");
851 nvram_set("wan_get_dns", "");
852 nvram_set("wan_get_domain", "");
853 nvram_set("pppoe_pid0", "");
854 nvram_set("action_service", "");
855 nvram_set("jffs2_format", "0");
856 nvram_set("rrules_radio", "-1");
857 nvram_unset("https_crt_gen");
858 if (nvram_get_int("http_id_gen") == 1) nvram_unset("http_id");
860 nvram_unset("sch_rboot_last");
861 nvram_unset("sch_rcon_last");
862 nvram_unset("sch_c1_last");
863 nvram_unset("sch_c2_last");
864 nvram_unset("sch_c3_last");
866 nvram_set("brau_state", "");
867 if ((features & SUP_BRAU) == 0) nvram_set("script_brau", "");
868 if ((features & SUP_SES) == 0) nvram_set("sesx_script", "");
870 if ((features & SUP_1000ET) == 0) nvram_set("jumbo_frame_enable", "0");
872 if (nvram_match("wl_net_mode", "disabled")) {
873 nvram_set("wl_radio", "0");
874 nvram_set("wl_net_mode", "mixed");
877 return 0;
880 /* Get the special files from nvram and copy them to disc.
881 * These were files saved with "nvram setfile2nvram <filename>".
882 * Better hope that they were saved with full pathname.
884 static void load_files_from_nvram(void)
886 char *name, *cp;
887 char buf[NVRAM_SPACE];
889 if (nvram_getall(buf, sizeof(buf)) != 0)
890 return;
892 for (name = buf; *name; name += strlen(name) + 1) {
893 if (strncmp(name, "FILE:", 5) == 0) { /* This special name marks a file to get. */
894 if ((cp = strchr(name, '=')) == NULL)
895 continue;
896 *cp = 0;
897 syslog(LOG_INFO, "Loading file '%s' from nvram", name + 5);
898 nvram_nvram2file(name, name + 5);
903 static void sysinit(void)
905 static const time_t tm = 0;
906 int hardware;
907 int i;
908 DIR *d;
909 struct dirent *de;
910 char s[256];
911 char t[256];
912 int model;
914 mount("proc", "/proc", "proc", 0, NULL);
915 mount("tmpfs", "/tmp", "tmpfs", 0, NULL);
917 #ifdef LINUX26
918 mount("devfs", "/dev", "tmpfs", MS_MGC_VAL | MS_NOATIME, NULL);
919 mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3));
920 mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1));
921 mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
922 mkdir("/dev/shm", 0777);
923 mkdir("/dev/pts", 0777);
924 mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
925 #endif
927 if (console_init()) noconsole = 1;
929 stime(&tm);
931 static const char *mkd[] = {
932 "/tmp/etc", "/tmp/var", "/tmp/home", "/tmp/mnt",
933 "/tmp/share", // !!TB
934 "/var/log", "/var/run", "/var/tmp", "/var/lib", "/var/lib/misc",
935 "/var/spool", "/var/spool/cron", "/var/spool/cron/crontabs",
936 "/tmp/var/wwwext", "/tmp/var/wwwext/cgi-bin", // !!TB - CGI support
937 NULL
939 umask(0);
940 for (i = 0; mkd[i]; ++i) {
941 mkdir(mkd[i], 0755);
943 mkdir("/var/lock", 0777);
944 mkdir("/var/tmp/dhcp", 0777);
945 mkdir("/home/root", 0700);
946 chmod("/tmp", 0777);
947 f_write("/etc/hosts", NULL, 0, 0, 0644); // blank
948 f_write("/etc/fstab", NULL, 0, 0, 0644); // !!TB - blank
949 simple_unlock("cron");
950 simple_unlock("firewall");
951 simple_unlock("restrictions");
952 umask(022);
954 if ((d = opendir("/rom/etc")) != NULL) {
955 while ((de = readdir(d)) != NULL) {
956 if (de->d_name[0] == '.') continue;
957 snprintf(s, sizeof(s), "%s/%s", "/rom/etc", de->d_name);
958 snprintf(t, sizeof(t), "%s/%s", "/etc", de->d_name);
959 symlink(s, t);
961 closedir(d);
963 symlink("/proc/mounts", "/etc/mtab");
965 #ifdef TCONFIG_SAMBASRV
966 if ((d = opendir("/usr/codepages")) != NULL) {
967 while ((de = readdir(d)) != NULL) {
968 if (de->d_name[0] == '.') continue;
969 snprintf(s, sizeof(s), "/usr/codepages/%s", de->d_name);
970 snprintf(t, sizeof(t), "/usr/share/%s", de->d_name);
971 symlink(s, t);
973 closedir(d);
975 #endif
977 #ifdef LINUX26
978 eval("hotplug2", "--coldplug");
979 start_hotplug2();
980 #endif
982 set_action(ACT_IDLE);
984 for (i = 0; defenv[i]; ++i) {
985 putenv(defenv[i]);
988 if (!noconsole) {
989 printf("\n\nHit ENTER for console...\n\n");
990 run_shell(1, 0);
993 check_bootnv();
995 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++) {
996 signal(fatalsigs[i], handle_fatalsigs);
998 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++) {
999 signal(initsigs[i], handle_initsigs);
1001 signal(SIGCHLD, handle_reap);
1003 switch (model = get_model()) {
1004 case MODEL_WR850GV1:
1005 case MODEL_WR850GV2:
1006 // need to cleanup some variables...
1007 if ((nvram_get("t_model") == NULL) && (nvram_get("MyFirmwareVersion") != NULL)) {
1008 nvram_unset("MyFirmwareVersion");
1009 nvram_set("restore_defaults", "1");
1011 break;
1014 hardware = check_hw_type();
1015 #if WL_BSS_INFO_VERSION >= 108
1016 modprobe("et");
1017 #else
1018 if ((hardware == HW_BCM4702) && (model != MODEL_WR850GV1)) {
1019 modprobe("4702et");
1020 modprobe("diag");
1022 else {
1023 modprobe("et");
1025 #endif
1027 #ifdef CONFIG_BCMWL5
1028 modprobe("emf");
1029 modprobe("igs");
1030 #endif
1031 modprobe("wl");
1032 modprobe("tomato_ct");
1034 config_loopback();
1036 system("nvram defaults --initcheck");
1037 init_nvram();
1039 // set the packet size
1040 if (nvram_get_int("jumbo_frame_enable")) {
1041 eval("et", "robowr", "0x40", "0x01", "0x1F"); // (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)
1042 eval("et", "robowr", "0x40", "0x05", nvram_safe_get("jumbo_frame_size"));
1045 klogctl(8, NULL, nvram_get_int("console_loglevel"));
1047 setup_conntrack();
1048 set_host_domain_name();
1050 start_jffs2();
1052 set_tz();
1054 eval("buttons");
1056 i = nvram_get_int("sesx_led");
1057 led(LED_AMBER, (i & 1) != 0);
1058 led(LED_WHITE, (i & 2) != 0);
1059 led(LED_AOSS, (i & 4) != 0);
1060 led(LED_BRIDGE, (i & 8) != 0);
1061 led(LED_DIAG, 1);
1064 int init_main(int argc, char *argv[])
1066 pid_t shell_pid = 0;
1068 sysinit();
1070 state = START;
1071 signaled = -1;
1073 #if defined(DEBUG_NOISY)
1074 nvram_set("debug_logeval", "1");
1075 nvram_set("debug_cprintf", "1");
1076 nvram_set("debug_cprintf_file", "1");
1077 nvram_set("debug_ddns", "1");
1078 #endif
1080 for (;;) {
1081 // TRACE_PT("main loop state=%d\n", state);
1083 switch (state) {
1084 case USER1:
1085 exec_service();
1086 state = IDLE;
1087 break;
1088 case RESTART:
1089 case STOP:
1090 case HALT:
1091 case REBOOT:
1092 led(LED_DIAG, 1);
1094 run_nvscript("script_shut", NULL, 10);
1096 stop_services();
1097 stop_wan();
1098 stop_lan();
1099 stop_vlan();
1100 stop_syslog();
1102 // !!TB - USB Support
1103 remove_storage_main((state == REBOOT) || (state == HALT));
1104 stop_usb();
1106 if ((state == REBOOT) || (state == HALT)) {
1107 shutdn(state == REBOOT);
1108 exit(0);
1110 if (state == STOP) {
1111 state = IDLE;
1112 break;
1115 // RESTART falls through
1117 case START:
1118 SET_LED(RELEASE_WAN_CONTROL);
1119 start_syslog();
1121 load_files_from_nvram();
1123 int fd = -1;
1124 fd = file_lock("usb"); // hold off automount processing
1125 start_usb();
1127 run_nvscript("script_init", NULL, 2);
1129 file_unlock(fd); // allow to process usb hotplug events
1130 #ifdef TCONFIG_USB
1132 * On RESTART some partitions can stay mounted if they are busy at the moment.
1133 * In that case USB drivers won't unload, and hotplug won't kick off again to
1134 * remount those drives that actually got unmounted. Make sure to remount ALL
1135 * partitions here by simulating hotplug event.
1137 if (state == RESTART) add_remove_usbhost("-1", 1);
1138 #endif
1140 start_vlan();
1141 start_lan();
1142 start_wan(BOOT);
1143 start_services();
1144 start_wl();
1146 #ifdef CONFIG_BCMWL5
1147 if (nvram_match("wds_enable", "1")) {
1148 /* Restart NAS one more time - for some reason without
1149 * this the new driver doesn't always bring WDS up.
1151 stop_nas();
1152 start_nas();
1154 #endif
1156 syslog(LOG_INFO, "Tomato %s", tomato_version);
1157 syslog(LOG_INFO, "%s", nvram_safe_get("t_model_name"));
1159 led(LED_DIAG, 0);
1160 notice_set("sysup", "");
1162 state = IDLE;
1164 // fall through
1166 case IDLE:
1167 while (signaled == -1) {
1168 check_services();
1169 if ((!noconsole) && ((!shell_pid) || (kill(shell_pid, 0) != 0))) {
1170 shell_pid = run_shell(0, 1);
1172 else {
1173 pause();
1176 state = signaled;
1177 signaled = -1;
1178 break;
1183 int reboothalt_main(int argc, char *argv[])
1185 int reboot = (strstr(argv[0], "reboot") != NULL);
1186 puts(reboot ? "Rebooting..." : "Shutting down...");
1187 fflush(stdout);
1188 sleep(1);
1189 kill(1, reboot ? SIGTERM : SIGQUIT);
1190 return 0;