Merge branch 'tomato-ND-usbmod-mixvpn' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / init.c
blob05b5b21da9d2f55b469c05cc567ce734d41f938e
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;
318 if (get_model() != MODEL_WRT54G) return;
319 if (strncmp(nvram_safe_get("pmon_ver"), "CFE", 3) != 0) return;
321 hardware = check_hw_type();
322 if (!nvram_get("boardtype") ||
323 !nvram_get("boardnum") ||
324 !nvram_get("boardflags") ||
325 !nvram_get("clkfreq") ||
326 !nvram_get("os_flash_addr") ||
327 !nvram_get("dl_ram_addr") ||
328 !nvram_get("os_ram_addr") ||
329 !nvram_get("scratch") ||
330 !nvram_get("et0macaddr") ||
331 ((hardware != HW_BCM4704_BCM5325F) && (!nvram_get("vlan0ports") || !nvram_get("vlan0hwname")))) {
332 cprintf("Unable to find critical settings, erasing NVRAM\n");
333 mtd_erase("nvram");
334 goto REBOOT;
337 dirty = 0;
339 switch (hardware) {
340 case HW_BCM5325E:
341 /* Lower the DDR ram drive strength , the value will be stable for all boards
342 Latency 3 is more stable for all ddr 20050420 by honor */
343 dirty |= check_nv("sdram_init", "0x010b");
344 dirty |= check_nv("sdram_config", "0x0062");
345 if (!nvram_match("debug_clkfix", "0")) {
346 dirty |= check_nv("clkfreq", "216");
348 if (dirty) {
349 nvram_set("sdram_ncdl", "0x0");
351 dirty |= check_nv("pa0itssit", "62");
352 dirty |= check_nv("pa0b0", "0x15eb");
353 dirty |= check_nv("pa0b1", "0xfa82");
354 dirty |= check_nv("pa0b2", "0xfe66");
355 //dirty |= check_nv("pa0maxpwr", "0x4e");
356 break;
357 case HW_BCM5352E: // G v4, GS v3, v4
358 dirty |= check_nv("sdram_init", "0x010b");
359 dirty |= check_nv("sdram_config", "0x0062");
360 if (dirty) {
361 nvram_set("sdram_ncdl", "0x0");
363 dirty |= check_nv("pa0itssit", "62");
364 dirty |= check_nv("pa0b0", "0x168b");
365 dirty |= check_nv("pa0b1", "0xfabf");
366 dirty |= check_nv("pa0b2", "0xfeaf");
367 //dirty |= check_nv("pa0maxpwr", "0x4e");
368 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
369 break;
370 case HW_BCM5354G:
371 dirty |= check_nv("pa0itssit", "62");
372 dirty |= check_nv("pa0b0", "0x1326");
373 dirty |= check_nv("pa0b1", "0xFB51");
374 dirty |= check_nv("pa0b2", "0xFE87");
375 //dirty |= check_nv("pa0maxpwr", "0x4e");
376 break;
377 case HW_BCM4704_BCM5325F:
378 // nothing to do
379 break;
380 default:
381 dirty |= check_nv("pa0itssit", "62");
382 dirty |= check_nv("pa0b0", "0x170c");
383 dirty |= check_nv("pa0b1", "0xfa24");
384 dirty |= check_nv("pa0b2", "0xfe70");
385 //dirty |= check_nv("pa0maxpwr", "0x48");
386 break;
389 if (dirty) {
390 nvram_commit();
391 REBOOT: // do a simple reboot
392 sync();
393 reboot(RB_AUTOBOOT);
394 exit(0);
398 static int init_nvram(void)
400 unsigned long features;
401 int model;
402 const char *mfr;
403 const char *name;
404 char s[256];
405 unsigned long bf;
406 unsigned long n;
408 model = get_model();
409 sprintf(s, "%d", model);
410 nvram_set("t_model", s);
412 mfr = "Broadcom";
413 name = NULL;
414 features = 0;
415 switch (model) {
416 case MODEL_WRT54G:
417 mfr = "Linksys";
418 name = "WRT54G/GS/GL";
419 switch (check_hw_type()) {
420 case HW_BCM4712:
421 nvram_set("gpio2", "adm_eecs");
422 nvram_set("gpio3", "adm_eesk");
423 nvram_unset("gpio4");
424 nvram_set("gpio5", "adm_eedi");
425 nvram_set("gpio6", "adm_rc");
426 break;
427 case HW_BCM4702:
428 nvram_unset("gpio2");
429 nvram_unset("gpio3");
430 nvram_unset("gpio4");
431 nvram_unset("gpio5");
432 nvram_unset("gpio6");
433 break;
434 case HW_BCM5352E:
435 nvram_set("opo", "0x0008");
436 nvram_set("ag0", "0x02");
437 // drop
438 default:
439 nvram_set("gpio2", "ses_led");
440 nvram_set("gpio3", "ses_led2");
441 nvram_set("gpio4", "ses_button");
442 features = SUP_SES | SUP_WHAM_LED;
443 break;
445 break;
446 case MODEL_WTR54GS:
447 mfr = "Linksys";
448 name = "WTR54GS";
449 if (!nvram_match("t_fix1", (char *)name)) {
450 nvram_set ("vlan0hwname", "et0");
451 nvram_set ("vlan1hwname", "et0");
452 nvram_set("vlan0ports", "0 5*");
453 nvram_set("vlan1ports", "1 5");
454 nvram_set("vlan_enable", "1");
455 nvram_set("lan_ifnames", "vlan0 eth1");
456 nvram_set("gpio2", "ses_button");
457 nvram_set("reset_gpio", "7");
460 nvram_set("pa0itssit", "62");
461 nvram_set("pa0b0", "0x1542");
462 nvram_set("pa0b1", "0xfacb");
463 nvram_set("pa0b2", "0xfec7");
464 //nvram_set("pa0maxpwr", "0x4c");
465 features = SUP_SES;
466 break;
467 case MODEL_WRTSL54GS:
468 mfr = "Linksys";
469 name = "WRTSL54GS";
470 features = SUP_SES | SUP_WHAM_LED;
471 break;
472 case MODEL_WHRG54S:
473 mfr = "Buffalo";
474 name = "WHR-G54S";
475 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
476 break;
477 case MODEL_WHRHPG54:
478 case MODEL_WZRRSG54HP:
479 case MODEL_WZRHPG54:
480 mfr = "Buffalo";
481 features = SUP_SES | SUP_AOSS_LED | SUP_HPAMP;
482 switch (model) {
483 case MODEL_WZRRSG54HP:
484 name = "WZR-RS-G54HP";
485 break;
486 case MODEL_WZRHPG54:
487 name = "WZR-HP-G54";
488 break;
489 default:
490 name = "WHR-HP-G54";
491 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_HPAMP;
492 break;
495 bf = strtoul(nvram_safe_get("boardflags"), NULL, 0);
496 switch (bf) {
497 case 0x0758:
498 case 0x1758:
499 case 0x2758:
500 case 0x3758:
501 if (nvram_match("wlx_hpamp", "")) {
502 if (nvram_get_int("wl_txpwr") > 10) nvram_set("wl_txpwr", "10");
503 nvram_set("wlx_hpamp", "1");
504 nvram_set("wlx_hperx", "0");
507 n = bf;
508 if (nvram_match("wlx_hpamp", "0")) {
509 n &= ~0x2000UL;
511 else {
512 n |= 0x2000UL;
514 if (nvram_match("wlx_hperx", "0")) {
515 n |= 0x1000UL;
517 else {
518 n &= ~0x1000UL;
520 if (bf != n) {
521 sprintf(s, "0x%lX", n);
522 nvram_set("boardflags", s);
524 break;
525 default:
526 syslog(LOG_WARNING, "Unexpected: boardflag=%lX", bf);
527 break;
529 break;
530 case MODEL_WBRG54:
531 mfr = "Buffalo";
532 name = "WBR-G54";
533 nvram_set("wl0gpio0", "130");
534 break;
535 case MODEL_WBR2G54:
536 mfr = "Buffalo";
537 name = "WBR2-G54";
538 features = SUP_SES | SUP_AOSS_LED;
539 break;
540 case MODEL_WHR2A54G54:
541 mfr = "Buffalo";
542 name = "WHR2-A54G54";
543 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
544 break;
545 case MODEL_WHR3AG54:
546 mfr = "Buffalo";
547 name = "WHR3-AG54";
548 features = SUP_SES | SUP_AOSS_LED;
549 break;
550 case MODEL_WZRG54:
551 mfr = "Buffalo";
552 name = "WZR-G54";
553 features = SUP_SES | SUP_AOSS_LED;
554 break;
555 case MODEL_WZRRSG54:
556 mfr = "Buffalo";
557 name = "WZR-RS-G54";
558 features = SUP_SES | SUP_AOSS_LED;
559 break;
560 case MODEL_WVRG54NF:
561 mfr = "Buffalo";
562 name = "WVR-G54-NF";
563 features = SUP_SES;
564 break;
565 case MODEL_WZRG108:
566 mfr = "Buffalo";
567 name = "WZR-G108";
568 features = SUP_SES | SUP_AOSS_LED;
569 break;
570 case MODEL_RT390W:
571 mfr = "Fuji";
572 name = "RT390W";
573 break;
574 case MODEL_WR850GV1:
575 mfr = "Motorola";
576 name = "WR850G v1";
577 features = SUP_NONVE;
578 break;
579 case MODEL_WR850GV2:
580 mfr = "Motorola";
581 name = "WR850G v2/v3";
582 features = SUP_NONVE;
583 break;
584 case MODEL_WL500GP:
585 mfr = "Asus";
586 name = "WL-500gP";
587 features = SUP_SES;
588 if (!nvram_match("t_fix1", (char *)name)) {
589 nvram_set("t_fix1", name);
590 nvram_set("sdram_init", "0x0009"); // 32MB; defaults: 0x000b, 0x0009
591 nvram_set("vlan1ports", "0 5"); // default: 0 5u
592 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3"); // set to "vlan0 eth2" by DD-WRT; default: vlan0 eth1
593 // !!TB - WLAN LED fix
594 nvram_set("wl0gpio0", "136");
596 break;
597 case MODEL_WL500W:
598 mfr = "Asus";
599 name = "WL-500W";
600 features = SUP_SES | SUP_80211N;
601 /* fix WL500W mac adresses for WAN port */
602 if (nvram_match("et1macaddr", "00:90:4c:a1:00:2d"))
603 nvram_set("et1macaddr", nvram_get("et0macaddr"));
604 /* fix AIR LED */
605 if (!nvram_get("wl0gpio0") || nvram_match("wl0gpio0", "2"))
606 nvram_set("wl0gpio0", "0x88");
607 break;
608 case MODEL_WL500GE:
609 mfr = "Asus";
610 name = "WL-500gE";
611 // features = ?
612 if (!nvram_match("t_fix1", (char *)name)) {
613 nvram_set("t_fix1", name);
614 nvram_set("vlan1ports", "0 5"); // default: 0 5u
616 break;
617 case MODEL_WX6615GT:
618 mfr = "SparkLAN";
619 name = "WX-6615GT";
620 // features = ?
621 break;
622 case MODEL_MN700:
623 mfr = "Microsoft";
624 name = "MN-700";
625 break;
626 case MODEL_WR100:
627 mfr = "Viewsonic";
628 name = "WR100";
629 break;
630 case MODEL_WLA2G54L:
631 mfr = "Buffalo";
632 name = "WLA2-G54L";
633 if (!nvram_match("t_fix1", (char *)name)) {
634 nvram_set("t_fix1", name);
635 nvram_set("lan_ifnames", "vlan0 eth1 eth2");
636 nvram_set("wl_ifname", "eth1");
637 nvram_set("wan_ifname", "none");
639 break;
640 case MODEL_TM2300:
641 mfr = "Dell";
642 name = "TrueMobile 2300";
643 break;
651 #ifndef WL_BSS_INFO_VERSION
652 #error WL_BSS_INFO_VERSION
653 #endif
654 #if WL_BSS_INFO_VERSION >= 108
656 case MODEL_WRH54G:
657 mfr = "Linksys";
658 name = "WRH54G";
660 nvram_set("opo", "12");
661 break;
663 case MODEL_WHRG125:
664 mfr = "Buffalo";
665 name = "WHR-G125";
666 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
668 nvram_set("opo", "0x0008");
669 nvram_set("ag0", "0x0C");
670 break;
671 case MODEL_RTN10:
672 mfr = "Asus";
673 name = "RT-N10";
674 features = SUP_SES | SUP_80211N;
675 if (!nvram_match("t_fix1", (char *)name)) {
676 nvram_set("lan_ifnames", "vlan0 eth1");
677 nvram_set("wan_ifnameX", "vlan1");
678 nvram_set("wl_ifname", "eth1");
679 nvram_set("vlan1ports", "4 5");
680 nvram_set("t_fix1", name);
682 break;
683 case MODEL_RTN12:
684 mfr = "Asus";
685 name = "RT-N12";
686 features = SUP_SES | SUP_BRAU | SUP_80211N;
687 if (!nvram_match("t_fix1", (char *)name)) {
688 nvram_set("lan_ifnames", "vlan0 eth1");
689 nvram_set("wan_ifnameX", "vlan1");
690 nvram_set("wl_ifname", "eth1");
691 nvram_set("vlan0ports", "3 2 1 0 5*");
692 nvram_set("vlan1ports", "4 5");
693 nvram_set("t_fix1", name);
695 break;
696 case MODEL_RTN16:
697 mfr = "Asus";
698 name = "RT-N16";
699 features = SUP_SES | SUP_80211N;
700 if (!nvram_match("t_fix1", (char *)name)) {
701 nvram_set("lan_ifnames", "vlan1 eth1");
702 nvram_set("wan_ifnameX", "vlan2");
703 nvram_set("wl_ifname", "eth1");
704 nvram_set("vlan2hwname", "et0");
705 nvram_set("vlan_enable", "1");
706 nvram_set("vlan1ports", "4 3 2 1 8*");
707 nvram_set("vlan2ports", "0 8");
708 nvram_set("t_fix1", name);
710 break;
711 case MODEL_WL500GPv2:
712 mfr = "Asus";
713 name = "WL-500gP v2";
714 features = SUP_SES;
715 if (!nvram_match("t_fix1", (char *)name)) {
716 if (nvram_match("vlan1ports", "4 5u")) {
717 nvram_set("vlan1ports", "4 5");
719 else if (nvram_match("vlan1ports", "0 5u")) { // 520GU?
720 nvram_set("vlan1ports", "0 5");
722 nvram_set("t_fix1", name);
724 break;
725 case MODEL_WL520GU:
726 mfr = "Asus";
727 name = "WL-520GU";
728 features = SUP_SES;
729 if (!nvram_match("t_fix1", (char *)name)) {
730 nvram_set("t_fix1", name);
731 nvram_set("vlan1ports", "0 5");
732 // !!TB - LED fix
733 nvram_set("wl0gpio0", "0");
734 nvram_set("wl0gpio1", "136");
735 nvram_set("wl0gpio2", "0");
736 nvram_set("wl0gpio3", "0");
738 break;
739 case MODEL_DIR320:
740 mfr = "D-Link";
741 name = "DIR-320";
742 features = SUP_SES;
743 if (nvram_match("wl0gpio0", "255"))
745 nvram_set("wl0gpio0", "8");
746 nvram_set("wl0gpio1", "0");
747 nvram_set("wl0gpio2", "0");
748 nvram_set("wl0gpio3", "0");
750 if (!nvram_match("t_fix1", (char *)name)) {
751 nvram_set("t_fix1", name);
752 nvram_unset( "vlan2ports" );
753 nvram_unset( "vlan2hwname" );
754 nvram_set("vlan1hwname", "et0");
755 nvram_set("vlan1ports", "0 5");
756 nvram_set("wandevs", "vlan1");
757 nvram_set("wan_ifname", "vlan1");
758 nvram_set("wan_ifnames", "vlan1");
759 nvram_set("wan0_ifname", "vlan1");
760 nvram_set("wan0_ifnames", "vlan1");
762 break;
763 #endif
764 #if TOMATO_N
765 case MODEL_WZRG300N:
766 mfr = "Buffalo";
767 name = "WZR-G300N";
768 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_80211N;
769 break;
770 case MODEL_WRT300N:
771 mfr = "Linksys";
772 name = "WRT300N";
773 features = SUP_SES | SUP_80211N;
774 break;
775 #endif
778 if (name) {
779 sprintf(s, "%s %s", mfr, name);
781 else {
782 snprintf(s, sizeof(s), "%s %d/%s/%s/%s/%s", mfr, check_hw_type(),
783 nvram_safe_get("boardtype"), nvram_safe_get("boardnum"), nvram_safe_get("boardrev"), nvram_safe_get("boardflags"));
784 s[64] = 0;
786 nvram_set("t_model_name", s);
788 nvram_set("pa0maxpwr", "251"); // allow Tx power up tp 251 mW, needed for ND only
790 sprintf(s, "0x%lX", features);
791 nvram_set("t_features", s);
796 note: set wan_ifnameX if wan_ifname needs to be overriden
799 if (nvram_is_empty("wan_ifnameX")) {
800 #if 1
801 nvram_set("wan_ifnameX", ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
802 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1");
803 #else
804 p = nvram_safe_get("wan_ifname");
805 if ((*p == 0) || (nvram_match("wl_ifname", p))) {
806 p = ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
807 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1";
809 nvram_set("wan_ifnameX", p);
810 #endif
814 nvram_set("wl_hwaddr", ""); // when disabling wireless, we must get null wireless mac ??
815 //!!TB - do not force country code here to allow nvram override
816 //nvram_set("wl_country", "JP");
817 //nvram_set("wl_country_code", "JP");
818 nvram_set("wan_get_dns", "");
819 nvram_set("wan_get_domain", "");
820 nvram_set("pppoe_pid0", "");
821 nvram_set("action_service", "");
822 nvram_set("jffs2_format", "0");
823 nvram_set("rrules_radio", "-1");
824 nvram_unset("https_crt_gen");
825 if (nvram_get_int("http_id_gen") == 1) nvram_unset("http_id");
827 nvram_unset("sch_rboot_last");
828 nvram_unset("sch_rcon_last");
829 nvram_unset("sch_c1_last");
830 nvram_unset("sch_c2_last");
831 nvram_unset("sch_c3_last");
833 nvram_set("brau_state", "");
834 if ((features & SUP_BRAU) == 0) nvram_set("script_brau", "");
835 if ((features & SUP_SES) == 0) nvram_set("sesx_script", "");
837 if ((features & SUP_1000ET) == 0) nvram_set("jumbo_frame_enable", "0");
839 if (nvram_match("wl_net_mode", "disabled")) {
840 nvram_set("wl_radio", "0");
841 nvram_set("wl_net_mode", "mixed");
844 return 0;
847 /* Get the special files from nvram and copy them to disc.
848 * These were files saved with "nvram setfile2nvram <filename>".
849 * Better hope that they were saved with full pathname.
851 static void load_files_from_nvram(void)
853 char *name, *cp;
854 char buf[NVRAM_SPACE];
856 if (nvram_getall(buf, sizeof(buf)) != 0)
857 return;
859 for (name = buf; *name; name += strlen(name) + 1) {
860 if (strncmp(name, "FILE:", 5) == 0) { /* This special name marks a file to get. */
861 if ((cp = strchr(name, '=')) == NULL)
862 continue;
863 *cp = 0;
864 syslog(LOG_INFO, "Loading file '%s' from nvram", name + 5);
865 nvram_nvram2file(name, name + 5);
870 static void sysinit(void)
872 static const time_t tm = 0;
873 int hardware;
874 int i;
875 DIR *d;
876 struct dirent *de;
877 char s[256];
878 char t[256];
879 int model;
881 mount("proc", "/proc", "proc", 0, NULL);
882 mount("tmpfs", "/tmp", "tmpfs", 0, NULL);
884 #ifdef LINUX26
885 mount("devfs", "/dev", "tmpfs", MS_MGC_VAL | MS_NOATIME, NULL);
886 mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3));
887 mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1));
888 mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
889 mkdir("/dev/shm", 0777);
890 mkdir("/dev/pts", 0777);
891 mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
892 #endif
894 if (console_init()) noconsole = 1;
896 stime(&tm);
898 static const char *mkd[] = {
899 "/tmp/etc", "/tmp/var", "/tmp/home", "/tmp/mnt",
900 "/tmp/share", // !!TB
901 "/var/log", "/var/run", "/var/tmp", "/var/lib", "/var/lib/misc",
902 "/var/spool", "/var/spool/cron", "/var/spool/cron/crontabs",
903 "/tmp/var/wwwext", "/tmp/var/wwwext/cgi-bin", // !!TB - CGI support
904 NULL
906 umask(0);
907 for (i = 0; mkd[i]; ++i) {
908 mkdir(mkd[i], 0755);
910 mkdir("/var/lock", 0777);
911 mkdir("/var/tmp/dhcp", 0777);
912 mkdir("/home/root", 0700);
913 chmod("/tmp", 0777);
914 f_write("/etc/hosts", NULL, 0, 0, 0644); // blank
915 f_write("/etc/fstab", NULL, 0, 0, 0644); // !!TB - blank
916 simple_unlock("cron");
917 simple_unlock("firewall");
918 simple_unlock("restrictions");
919 umask(022);
921 if ((d = opendir("/rom/etc")) != NULL) {
922 while ((de = readdir(d)) != NULL) {
923 if (de->d_name[0] == '.') continue;
924 snprintf(s, sizeof(s), "%s/%s", "/rom/etc", de->d_name);
925 snprintf(t, sizeof(t), "%s/%s", "/etc", de->d_name);
926 symlink(s, t);
928 closedir(d);
930 symlink("/proc/mounts", "/etc/mtab");
932 #ifdef TCONFIG_SAMBASRV
933 if ((d = opendir("/usr/codepages")) != NULL) {
934 while ((de = readdir(d)) != NULL) {
935 if (de->d_name[0] == '.') continue;
936 snprintf(s, sizeof(s), "/usr/codepages/%s", de->d_name);
937 snprintf(t, sizeof(t), "/usr/share/%s", de->d_name);
938 symlink(s, t);
940 closedir(d);
942 #endif
944 #ifdef LINUX26
945 eval("hotplug2", "--coldplug");
946 start_hotplug2();
947 #endif
949 set_action(ACT_IDLE);
951 for (i = 0; defenv[i]; ++i) {
952 putenv(defenv[i]);
955 if (!noconsole) {
956 printf("\n\nHit ENTER for console...\n\n");
957 run_shell(1, 0);
960 check_bootnv();
962 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++) {
963 signal(fatalsigs[i], handle_fatalsigs);
965 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++) {
966 signal(initsigs[i], handle_initsigs);
968 signal(SIGCHLD, handle_reap);
970 switch (model = get_model()) {
971 case MODEL_WR850GV1:
972 case MODEL_WR850GV2:
973 // need to cleanup some variables...
974 if ((nvram_get("t_model") == NULL) && (nvram_get("MyFirmwareVersion") != NULL)) {
975 nvram_unset("MyFirmwareVersion");
976 nvram_set("restore_defaults", "1");
978 break;
981 hardware = check_hw_type();
982 #if WL_BSS_INFO_VERSION >= 108
983 modprobe("et");
984 #else
985 if ((hardware == HW_BCM4702) && (model != MODEL_WR850GV1)) {
986 modprobe("4702et");
987 modprobe("diag");
989 else {
990 modprobe("et");
992 #endif
994 #ifdef CONFIG_BCMWL5
995 modprobe("emf");
996 modprobe("igs");
997 #endif
998 modprobe("wl");
999 modprobe("tomato_ct");
1001 config_loopback();
1003 system("nvram defaults --initcheck");
1004 init_nvram();
1006 // set the packet size
1007 if (nvram_get_int("jumbo_frame_enable")) {
1008 eval("et", "robowr", "0x40", "0x01", (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4));
1009 eval("et", "robowr", "0x40", "0x05", nvram_safe_get("jumbo_frame_size"));
1012 klogctl(8, NULL, nvram_get_int("console_loglevel"));
1014 setup_conntrack();
1015 set_host_domain_name();
1017 start_jffs2();
1019 set_tz();
1021 eval("buttons");
1023 i = nvram_get_int("sesx_led");
1024 led(LED_AMBER, (i & 1) != 0);
1025 led(LED_WHITE, (i & 2) != 0);
1026 led(LED_AOSS, (i & 4) != 0);
1027 led(LED_BRIDGE, (i & 8) != 0);
1028 led(LED_DIAG, 1);
1031 int init_main(int argc, char *argv[])
1033 pid_t shell_pid = 0;
1035 sysinit();
1037 state = START;
1038 signaled = -1;
1040 #if defined(DEBUG_NOISY)
1041 nvram_set("debug_logeval", "1");
1042 nvram_set("debug_cprintf", "1");
1043 nvram_set("debug_cprintf_file", "1");
1044 nvram_set("debug_ddns", "1");
1045 #endif
1047 for (;;) {
1048 // TRACE_PT("main loop state=%d\n", state);
1050 switch (state) {
1051 case USER1:
1052 exec_service();
1053 state = IDLE;
1054 break;
1055 case RESTART:
1056 case STOP:
1057 case HALT:
1058 case REBOOT:
1059 led(LED_DIAG, 1);
1061 run_nvscript("script_shut", NULL, 10);
1063 stop_services();
1064 stop_wan();
1065 stop_lan();
1066 stop_vlan();
1067 stop_syslog();
1069 // !!TB - USB Support
1070 remove_storage_main((state == REBOOT) || (state == HALT));
1071 stop_usb();
1073 if ((state == REBOOT) || (state == HALT)) {
1074 shutdn(state == REBOOT);
1075 exit(0);
1077 if (state == STOP) {
1078 state = IDLE;
1079 break;
1082 // RESTART falls through
1084 case START:
1085 SET_LED(RELEASE_WAN_CONTROL);
1086 start_syslog();
1088 load_files_from_nvram();
1090 int fd = -1;
1091 fd = file_lock("usb"); // hold off automount processing
1092 start_usb();
1094 run_nvscript("script_init", NULL, 2);
1096 file_unlock(fd); // allow to process usb hotplug events
1097 #ifdef TCONFIG_USB
1099 * On RESTART some partitions can stay mounted if they are busy at the moment.
1100 * In that case USB drivers won't unload, and hotplug won't kick off again to
1101 * remount those drives that actually got unmounted. Make sure to remount ALL
1102 * partitions here by simulating hotplug event.
1104 if (state == RESTART) add_remove_usbhost("-1", 1);
1105 #endif
1107 start_vlan();
1108 start_lan();
1109 start_wan(BOOT);
1110 start_services();
1111 start_wl();
1113 syslog(LOG_INFO, "Tomato %s", tomato_version);
1114 syslog(LOG_INFO, "%s", nvram_safe_get("t_model_name"));
1116 led(LED_DIAG, 0);
1117 notice_set("sysup", "");
1119 state = IDLE;
1121 // fall through
1123 case IDLE:
1124 while (signaled == -1) {
1125 check_services();
1126 if ((!noconsole) && ((!shell_pid) || (kill(shell_pid, 0) != 0))) {
1127 shell_pid = run_shell(0, 1);
1129 else {
1130 pause();
1133 state = signaled;
1134 signaled = -1;
1135 break;
1140 int reboothalt_main(int argc, char *argv[])
1142 int reboot = (strstr(argv[0], "reboot") != NULL);
1143 puts(reboot ? "Rebooting..." : "Shutting down...");
1144 fflush(stdout);
1145 sleep(1);
1146 kill(1, reboot ? SIGTERM : SIGQUIT);
1147 return 0;