Fix remount of usb drives on reboot/restart
[tomato.git] / release / src / router / rc / init.c
blob0b92507395fc109b7048bca3eb241ed01b598e53
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 #include <wlutils.h>
26 #include <bcmdevs.h>
28 #define SHELL "/bin/sh"
31 enum {
32 RESTART,
33 STOP,
34 START,
35 USER1,
36 IDLE,
37 REBOOT,
38 HALT,
39 INIT
42 static int fatalsigs[] = {
43 SIGILL,
44 SIGABRT,
45 SIGFPE,
46 SIGPIPE,
47 SIGBUS,
48 SIGSYS,
49 SIGTRAP,
50 SIGPWR
53 static int initsigs[] = {
54 SIGHUP,
55 SIGUSR1,
56 SIGUSR2,
57 SIGINT,
58 SIGQUIT,
59 SIGTERM
62 static char *defenv[] = {
63 "TERM=vt100",
64 "HOME=/",
65 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
66 "SHELL=" SHELL,
67 "USER=root",
68 NULL
71 static int noconsole = 0;
72 static volatile int state = INIT;
73 static volatile int signaled = -1;
76 /* Set terminal settings to reasonable defaults */
77 static void set_term(int fd)
79 struct termios tty;
81 tcgetattr(fd, &tty);
83 /* set control chars */
84 tty.c_cc[VINTR] = 3; /* C-c */
85 tty.c_cc[VQUIT] = 28; /* C-\ */
86 tty.c_cc[VERASE] = 127; /* C-? */
87 tty.c_cc[VKILL] = 21; /* C-u */
88 tty.c_cc[VEOF] = 4; /* C-d */
89 tty.c_cc[VSTART] = 17; /* C-q */
90 tty.c_cc[VSTOP] = 19; /* C-s */
91 tty.c_cc[VSUSP] = 26; /* C-z */
93 /* use line dicipline 0 */
94 tty.c_line = 0;
96 /* Make it be sane */
97 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
98 tty.c_cflag |= CREAD|HUPCL|CLOCAL;
101 /* input modes */
102 tty.c_iflag = ICRNL | IXON | IXOFF;
104 /* output modes */
105 tty.c_oflag = OPOST | ONLCR;
107 /* local modes */
108 tty.c_lflag =
109 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
111 tcsetattr(fd, TCSANOW, &tty);
114 static int console_init(void)
116 int fd;
118 /* Clean up */
119 ioctl(0, TIOCNOTTY, 0);
120 close(0);
121 close(1);
122 close(2);
123 setsid();
125 /* Reopen console */
126 if ((fd = open(_PATH_CONSOLE, O_RDWR)) < 0) {
127 /* Avoid debug messages is redirected to socket packet if no exist a UART chip, added by honor, 2003-12-04 */
128 open("/dev/null", O_RDONLY);
129 open("/dev/null", O_WRONLY);
130 open("/dev/null", O_WRONLY);
131 perror(_PATH_CONSOLE);
132 return errno;
134 dup2(fd, 0);
135 dup2(fd, 1);
136 dup2(fd, 2);
138 ioctl(0, TIOCSCTTY, 1);
139 tcsetpgrp(0, getpgrp());
140 set_term(0);
142 return 0;
146 * Waits for a file descriptor to change status or unblocked signal
147 * @param fd file descriptor
148 * @param timeout seconds to wait before timing out or 0 for no timeout
149 * @return 1 if descriptor changed status or 0 if timed out or -1 on error
151 static int waitfor(int fd, int timeout)
153 fd_set rfds;
154 struct timeval tv = { timeout, 0 };
156 FD_ZERO(&rfds);
157 FD_SET(fd, &rfds);
158 return select(fd + 1, &rfds, NULL, NULL, (timeout > 0) ? &tv : NULL);
161 static pid_t run_shell(int timeout, int nowait)
163 pid_t pid;
164 int sig;
166 /* Wait for user input */
167 if (waitfor(STDIN_FILENO, timeout) <= 0) return 0;
169 switch (pid = fork()) {
170 case -1:
171 perror("fork");
172 return 0;
173 case 0:
174 /* Reset signal handlers set for parent process */
175 for (sig = 0; sig < (_NSIG-1); sig++)
176 signal(sig, SIG_DFL);
178 /* Reopen console */
179 console_init();
180 printf("\n\nTomato %s\n\n", tomato_version);
182 /* Now run it. The new program will take over this PID,
183 * so nothing further in init.c should be run. */
184 execve(SHELL, (char *[]) { SHELL, NULL }, defenv);
186 /* We're still here? Some error happened. */
187 perror(SHELL);
188 exit(errno);
189 default:
190 if (nowait) {
191 return pid;
193 else {
194 waitpid(pid, NULL, 0);
195 return 0;
200 static void shutdn(int rb)
202 int i;
203 int act;
204 sigset_t ss;
206 _dprintf("shutdn rb=%d\n", rb);
208 sigemptyset(&ss);
209 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++)
210 sigaddset(&ss, fatalsigs[i]);
211 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++)
212 sigaddset(&ss, initsigs[i]);
213 sigaddset(&ss, SIGCHLD);
214 sigprocmask(SIG_BLOCK, &ss, NULL);
216 for (i = 30; i > 0; --i) {
217 if (((act = check_action()) == ACT_IDLE) || (act == ACT_REBOOT)) break;
218 cprintf("Busy with %d. Waiting before shutdown... %d\n", act, i);
219 sleep(1);
221 set_action(ACT_REBOOT);
223 cprintf("TERM\n");
224 kill(-1, SIGTERM);
225 sleep(2);
226 sync();
228 cprintf("KILL\n");
229 kill(-1, SIGKILL);
230 sleep(1);
231 sync();
233 umount("/jffs"); // may hang if not
234 sleep(1);
236 if (rb != -1) {
237 led(LED_WLAN, 0);
238 if (rb == 0) {
239 for (i = 4; i > 0; --i) {
240 led(LED_DMZ, 1);
241 led(LED_WHITE, 1);
242 usleep(250000);
243 led(LED_DMZ, 0);
244 led(LED_WHITE, 0);
245 usleep(250000);
250 reboot(rb ? RB_AUTOBOOT : RB_HALT_SYSTEM);
252 do {
253 sleep(1);
254 } while (1);
257 static void handle_fatalsigs(int sig)
259 _dprintf("fatal sig=%d\n", sig);
260 shutdn(-1);
263 void handle_reap(int sig)
265 while (waitpid(-1, NULL, WNOHANG) > 0) {
270 static void handle_initsigs(int sig)
272 // TRACE_PT("sig=%d state=%d, signaled=%d\n", sig, state, signaled);
274 switch (sig) {
275 case SIGHUP:
276 signaled = RESTART;
277 break;
278 case SIGUSR1:
279 signaled = USER1;
280 break;
281 case SIGUSR2:
282 signaled = START;
283 break;
284 case SIGINT:
285 signaled = STOP;
286 break;
287 case SIGTERM:
288 signaled = REBOOT;
289 break;
290 case SIGQUIT:
291 signaled = HALT;
292 break;
296 static int check_nv(const char *name, const char *value)
298 const char *p;
299 if (!nvram_match("manual_boot_nv", "1")) {
300 if (((p = nvram_get(name)) == NULL) || (strcmp(p, value) != 0)) {
301 // cprintf("Error: Critical variable %s is invalid. Resetting.\n", name);
302 nvram_set(name, value);
303 return 1;
306 return 0;
309 static void check_bootnv(void)
311 int dirty;
312 int hardware;
314 if (get_model() != MODEL_WRT54G) return;
315 if (strncmp(nvram_safe_get("pmon_ver"), "CFE", 3) != 0) return;
317 hardware = check_hw_type();
318 if (!nvram_get("boardtype") ||
319 !nvram_get("boardnum") ||
320 !nvram_get("boardflags") ||
321 !nvram_get("clkfreq") ||
322 !nvram_get("os_flash_addr") ||
323 !nvram_get("dl_ram_addr") ||
324 !nvram_get("os_ram_addr") ||
325 !nvram_get("scratch") ||
326 !nvram_get("et0macaddr") ||
327 ((hardware != HW_BCM4704_BCM5325F) && (!nvram_get("vlan0ports") || !nvram_get("vlan0hwname")))) {
328 cprintf("Unable to find critical settings, erasing NVRAM\n");
329 mtd_erase("nvram");
330 goto REBOOT;
333 dirty = 0;
335 switch (hardware) {
336 case HW_BCM5325E:
337 /* Lower the DDR ram drive strength , the value will be stable for all boards
338 Latency 3 is more stable for all ddr 20050420 by honor */
339 dirty |= check_nv("sdram_init", "0x010b");
340 dirty |= check_nv("sdram_config", "0x0062");
341 if (!nvram_match("debug_clkfix", "0")) {
342 dirty |= check_nv("clkfreq", "216");
344 if (dirty) {
345 nvram_set("sdram_ncdl", "0x0");
347 dirty |= check_nv("pa0itssit", "62");
348 dirty |= check_nv("pa0b0", "0x15eb");
349 dirty |= check_nv("pa0b1", "0xfa82");
350 dirty |= check_nv("pa0b2", "0xfe66");
351 dirty |= check_nv("pa0maxpwr", "0x4e");
352 break;
353 case HW_BCM5352E: // G v4, GS v3, v4
354 dirty |= check_nv("sdram_init", "0x010b");
355 dirty |= check_nv("sdram_config", "0x0062");
356 if (dirty) {
357 nvram_set("sdram_ncdl", "0x0");
359 dirty |= check_nv("pa0itssit", "62");
360 dirty |= check_nv("pa0b0", "0x168b");
361 dirty |= check_nv("pa0b1", "0xfabf");
362 dirty |= check_nv("pa0b2", "0xfeaf");
363 dirty |= check_nv("pa0maxpwr", "0x4e");
364 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
365 break;
366 case HW_BCM5354G:
367 dirty |= check_nv("pa0itssit", "62");
368 dirty |= check_nv("pa0b0", "0x1326");
369 dirty |= check_nv("pa0b1", "0xFB51");
370 dirty |= check_nv("pa0b2", "0xFE87");
371 dirty |= check_nv("pa0maxpwr", "0x4e");
372 break;
373 case HW_BCM4704_BCM5325F:
374 // nothing to do
375 break;
376 default:
377 dirty |= check_nv("pa0itssit", "62");
378 dirty |= check_nv("pa0b0", "0x170c");
379 dirty |= check_nv("pa0b1", "0xfa24");
380 dirty |= check_nv("pa0b2", "0xfe70");
381 dirty |= check_nv("pa0maxpwr", "0x48");
382 break;
385 if (dirty) {
386 nvram_commit();
387 REBOOT: // do a simple reboot
388 sync();
389 reboot(RB_AUTOBOOT);
390 exit(0);
394 static int init_nvram(void)
396 unsigned long features;
397 int model;
398 const char *mfr;
399 const char *name;
400 char s[256];
401 unsigned long bf;
402 unsigned long n;
404 model = get_model();
405 sprintf(s, "%d", model);
406 nvram_set("t_model", s);
408 mfr = "Broadcom";
409 name = NULL;
410 features = 0;
411 switch (model) {
412 case MODEL_WRT54G:
413 mfr = "Linksys";
414 name = "WRT54G/GS/GL";
415 switch (check_hw_type()) {
416 case HW_BCM4712:
417 nvram_set("gpio2", "adm_eecs");
418 nvram_set("gpio3", "adm_eesk");
419 nvram_unset("gpio4");
420 nvram_set("gpio5", "adm_eedi");
421 nvram_set("gpio6", "adm_rc");
422 break;
423 case HW_BCM4702:
424 nvram_unset("gpio2");
425 nvram_unset("gpio3");
426 nvram_unset("gpio4");
427 nvram_unset("gpio5");
428 nvram_unset("gpio6");
429 break;
430 case HW_BCM5352E:
431 nvram_set("opo", "0x0008");
432 nvram_set("ag0", "0x02");
433 // drop
434 default:
435 nvram_set("gpio2", "ses_led");
436 nvram_set("gpio3", "ses_led2");
437 nvram_set("gpio4", "ses_button");
438 features = SUP_SES | SUP_WHAM_LED;
439 break;
441 break;
442 case MODEL_WTR54GS:
443 mfr = "Linksys";
444 name = "WTR54GS";
445 if (!nvram_match("t_fix1", (char *)name)) {
446 nvram_set ("vlan0hwname", "et0");
447 nvram_set ("vlan1hwname", "et0");
448 nvram_set("vlan0ports", "0 5*");
449 nvram_set("vlan1ports", "1 5");
450 nvram_set("vlan_enable", "1");
451 nvram_set("lan_ifnames", "vlan0 eth1");
452 nvram_set("gpio2", "ses_button");
453 nvram_set("reset_gpio", "7");
456 nvram_set("pa0itssit", "62");
457 nvram_set("pa0b0", "0x1542");
458 nvram_set("pa0b1", "0xfacb");
459 nvram_set("pa0b2", "0xfec7");
460 nvram_set("pa0maxpwr", "0x4c");
461 features = SUP_SES;
462 break;
463 case MODEL_WRTSL54GS:
464 mfr = "Linksys";
465 name = "WRTSL54GS";
466 features = SUP_SES | SUP_WHAM_LED;
467 break;
468 case MODEL_WHRG54S:
469 mfr = "Buffalo";
470 name = "WHR-G54S";
471 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
472 break;
473 case MODEL_WHRHPG54:
474 case MODEL_WZRRSG54HP:
475 case MODEL_WZRHPG54:
476 mfr = "Buffalo";
477 features = SUP_SES | SUP_AOSS_LED | SUP_HPAMP;
478 switch (model) {
479 case MODEL_WZRRSG54HP:
480 name = "WZR-RS-G54HP";
481 break;
482 case MODEL_WZRHPG54:
483 name = "WZR-HP-G54";
484 break;
485 default:
486 name = "WHR-HP-G54";
487 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_HPAMP;
488 break;
491 bf = strtoul(nvram_safe_get("boardflags"), NULL, 0);
492 switch (bf) {
493 case 0x0758:
494 case 0x1758:
495 case 0x2758:
496 case 0x3758:
497 if (nvram_match("wlx_hpamp", "")) {
498 if (nvram_get_int("wl_txpwr") > 10) nvram_set("wl_txpwr", "10");
499 nvram_set("wlx_hpamp", "1");
500 nvram_set("wlx_hperx", "0");
503 n = bf;
504 if (nvram_match("wlx_hpamp", "0")) {
505 n &= ~0x2000UL;
507 else {
508 n |= 0x2000UL;
510 if (nvram_match("wlx_hperx", "0")) {
511 n |= 0x1000UL;
513 else {
514 n &= ~0x1000UL;
516 if (bf != n) {
517 sprintf(s, "0x%lX", n);
518 nvram_set("boardflags", s);
520 break;
521 default:
522 syslog(LOG_WARNING, "Unexpected: boardflag=%lX", bf);
523 break;
525 break;
526 case MODEL_WBRG54:
527 mfr = "Buffalo";
528 name = "WBR-G54";
529 nvram_set("wl0gpio0", "130");
530 break;
531 case MODEL_WBR2G54:
532 mfr = "Buffalo";
533 name = "WBR2-G54";
534 features = SUP_SES | SUP_AOSS_LED;
535 break;
536 case MODEL_WHR2A54G54:
537 mfr = "Buffalo";
538 name = "WHR2-A54G54";
539 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
540 break;
541 case MODEL_WHR3AG54:
542 mfr = "Buffalo";
543 name = "WHR3-AG54";
544 features = SUP_SES | SUP_AOSS_LED;
545 break;
546 case MODEL_WZRG54:
547 mfr = "Buffalo";
548 name = "WZR-G54";
549 features = SUP_SES | SUP_AOSS_LED;
550 break;
551 case MODEL_WZRRSG54:
552 mfr = "Buffalo";
553 name = "WZR-RS-G54";
554 features = SUP_SES | SUP_AOSS_LED;
555 break;
556 case MODEL_WVRG54NF:
557 mfr = "Buffalo";
558 name = "WVR-G54-NF";
559 features = SUP_SES;
560 break;
561 case MODEL_WZRG108:
562 mfr = "Buffalo";
563 name = "WZR-G108";
564 features = SUP_SES | SUP_AOSS_LED;
565 break;
566 case MODEL_RT390W:
567 mfr = "Fuji";
568 name = "RT390W";
569 break;
570 case MODEL_WR850GV1:
571 mfr = "Motorola";
572 name = "WR850G v1";
573 features = SUP_NONVE;
574 break;
575 case MODEL_WR850GV2:
576 mfr = "Motorola";
577 name = "WR850G v2/v3";
578 features = SUP_NONVE;
579 break;
580 case MODEL_WL500GP:
581 mfr = "Asus";
582 name = "WL-500gP";
583 features = SUP_SES;
584 if (!nvram_match("t_fix1", (char *)name)) {
585 nvram_set("t_fix1", name);
586 nvram_set("sdram_init", "0x0009"); // 32MB; defaults: 0x000b, 0x0009
587 nvram_set("vlan1ports", "0 5"); // default: 0 5u
588 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3"); // set to "vlan0 eth2" by DD-WRT; default: vlan0 eth1
589 // !!TB - WLAN LED fix
590 nvram_set("wl0gpio0", "136");
592 break;
593 case MODEL_WL500GE:
594 mfr = "Asus";
595 name = "WL-500gE";
596 // features = ?
597 if (!nvram_match("t_fix1", (char *)name)) {
598 nvram_set("t_fix1", name);
599 nvram_set("vlan1ports", "0 5"); // default: 0 5u
601 break;
602 case MODEL_WX6615GT:
603 mfr = "SparkLAN";
604 name = "WX-6615GT";
605 // features = ?
606 break;
607 case MODEL_MN700:
608 mfr = "Microsoft";
609 name = "MN-700";
610 break;
611 case MODEL_WR100:
612 mfr = "Viewsonic";
613 name = "WR100";
614 break;
615 #ifndef WL_BSS_INFO_VERSION
616 #error WL_BSS_INFO_VERSION
617 #endif
618 #if WL_BSS_INFO_VERSION >= 108
620 case MODEL_WRH54G:
621 mfr = "Linksys";
622 name = "WRH54G";
624 nvram_set("opo", "12");
625 break;
627 case MODEL_WHRG125:
628 mfr = "Buffalo";
629 name = "WHR-G125";
630 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
632 nvram_set("opo", "0x0008");
633 nvram_set("ag0", "0x0C");
634 break;
635 case MODEL_WL500GPv2:
636 mfr = "Asus";
637 name = "WL-500gP v2";
638 // features = ?;
639 if (!nvram_match("t_fix1", (char *)name)) {
640 if (nvram_match("vlan1ports", "4 5u")) {
641 nvram_set("vlan1ports", "4 5");
643 else if (nvram_match("vlan1ports", "0 5u")) { // 520GU?
644 nvram_set("vlan1ports", "0 5");
647 break;
648 case MODEL_WL520GU:
649 mfr = "Asus";
650 name = "WL-520GU";
651 features = SUP_SES;
652 if (!nvram_match("t_fix1", (char *)name)) {
653 nvram_set("t_fix1", name);
654 nvram_set("vlan1ports", "0 5");
655 // !!TB - LED fix
656 nvram_set("wl0gpio0", "0");
657 nvram_set("wl0gpio1", "136");
658 nvram_set("wl0gpio2", "0");
659 nvram_set("wl0gpio3", "0");
661 break;
662 #endif
663 #if TOMATO_N
664 case MODEL_WZRG300N:
665 mfr = "Buffalo";
666 name = "WZR-G300N";
667 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_80211N;
668 break;
669 case MODEL_WRT300N:
670 mfr = "Linksys";
671 name = "WRT300N";
672 features = SUP_SES | SUP_80211N;
673 break;
674 #endif
677 if (name) {
678 sprintf(s, "%s %s", mfr, name);
680 else {
681 snprintf(s, sizeof(s), "%s %d/%s/%s/%s/%s", mfr, check_hw_type(),
682 nvram_safe_get("boardtype"), nvram_safe_get("boardnum"), nvram_safe_get("boardrev"), nvram_safe_get("boardflags"));
683 s[64] = 0;
685 nvram_set("t_model_name", s);
687 sprintf(s, "0x%lX", features);
688 nvram_set("t_features", s);
693 note: set wan_ifnameX if wan_ifname needs to be overriden
696 if (nvram_is_empty("wan_ifnameX")) {
697 #if 1
698 nvram_set("wan_ifnameX", ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
699 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1");
700 #else
701 p = nvram_safe_get("wan_ifname");
702 if ((*p == 0) || (nvram_match("wl_ifname", p))) {
703 p = ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
704 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1";
706 nvram_set("wan_ifnameX", p);
707 #endif
711 nvram_set("wl_hwaddr", ""); // when disabling wireless, we must get null wireless mac ??
712 nvram_set("wl_country", "JP");
713 nvram_set("wl_country_code", "JP");
714 nvram_set("wan_get_dns", "");
715 nvram_set("wan_get_domain", "");
716 nvram_set("pppoe_pid0", "");
717 nvram_set("action_service", "");
718 nvram_set("jffs2_format", "0");
719 nvram_set("rrules_radio", "-1");
720 nvram_unset("https_crt_gen");
721 if (nvram_get_int("http_id_gen") == 1) nvram_unset("http_id");
723 nvram_unset("sch_rboot_last");
724 nvram_unset("sch_rcon_last");
725 nvram_unset("sch_c1_last");
726 nvram_unset("sch_c2_last");
727 nvram_unset("sch_c3_last");
729 nvram_set("brau_state", "");
730 if ((features & SUP_BRAU) == 0) nvram_set("script_brau", "");
731 if ((features & SUP_SES) == 0) nvram_set("sesx_script", "");
733 if (nvram_match("wl_net_mode", "disabled")) {
734 nvram_set("wl_radio", "0");
735 nvram_set("wl_net_mode", "mixed");
738 return 0;
741 static void sysinit(void)
743 static const time_t tm = 0;
744 int hardware;
745 int i;
746 DIR *d;
747 struct dirent *de;
748 char s[256];
749 char t[256];
750 int model;
752 mount("", "/proc", "proc", 0, NULL);
753 mount("", "/tmp", "ramfs", 0, NULL);
755 if (console_init()) noconsole = 1;
757 stime(&tm);
759 static const char *mkd[] = {
760 "/tmp/etc", "/tmp/var", "/tmp/home", "/tmp/mnt",
761 "/tmp/share", // !!TB
762 "/var/log", "/var/run", "/var/tmp", "/var/lib", "/var/lib/misc",
763 "/var/spool", "/var/spool/cron", "/var/spool/cron/crontabs", NULL
765 umask(0);
766 for (i = 0; mkd[i]; ++i) {
767 mkdir(mkd[i], 0755);
769 mkdir("/var/lock", 0777);
770 mkdir("/var/tmp/dhcp", 0777);
771 mkdir("/home/root", 0700);
772 chmod("/tmp", 0777);
773 f_write("/etc/hosts", NULL, 0, 0, 0644); // blank
774 f_write("/etc/fstab", NULL, 0, 0, 0644); // !!TB - blank
775 simple_unlock("cron");
776 simple_unlock("firewall");
777 simple_unlock("restrictions");
778 umask(022);
780 if ((d = opendir("/rom/etc")) != NULL) {
781 while ((de = readdir(d)) != NULL) {
782 if (de->d_name[0] == '.') continue;
783 snprintf(s, sizeof(s), "%s/%s", "/rom/etc", de->d_name);
784 snprintf(t, sizeof(t), "%s/%s", "/etc", de->d_name);
785 symlink(s, t);
787 closedir(d);
789 symlink("/proc/mounts", "/etc/mtab");
791 #ifdef TCONFIG_SAMBASRV
792 if ((d = opendir("/usr/codepages")) != NULL) {
793 while ((de = readdir(d)) != NULL) {
794 if (de->d_name[0] == '.') continue;
795 snprintf(s, sizeof(s), "/usr/codepages/%s", de->d_name);
796 snprintf(t, sizeof(t), "/usr/share/%s", de->d_name);
797 symlink(s, t);
799 closedir(d);
801 #endif
803 set_action(ACT_IDLE);
805 for (i = 0; defenv[i]; ++i) {
806 putenv(defenv[i]);
809 if (!noconsole) {
810 printf("\n\nHit ENTER for console...\n\n");
811 run_shell(1, 0);
814 check_bootnv();
816 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++) {
817 signal(fatalsigs[i], handle_fatalsigs);
819 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++) {
820 signal(initsigs[i], handle_initsigs);
822 signal(SIGCHLD, handle_reap);
824 switch (model = get_model()) {
825 case MODEL_WR850GV1:
826 case MODEL_WR850GV2:
827 // need to cleanup some variables...
828 if ((nvram_get("t_model") == NULL) && (nvram_get("MyFirmwareVersion") != NULL)) {
829 nvram_unset("MyFirmwareVersion");
830 nvram_set("restore_defaults", "1");
832 break;
835 system("nvram defaults --initcheck");
836 init_nvram();
838 klogctl(8, NULL, nvram_get_int("console_loglevel"));
840 setup_conntrack();
842 hardware = check_hw_type();
843 #if WL_BSS_INFO_VERSION >= 108
844 modprobe("et");
845 #else
846 if ((hardware == HW_BCM4702) && (model != MODEL_WR850GV1)) {
847 modprobe("4702et");
848 modprobe("diag");
850 else {
851 modprobe("et");
853 #endif
854 modprobe("wl");
855 modprobe("tomato_ct");
857 set_host_domain_name();
858 config_loopback();
860 eval("buttons");
862 start_jffs2();
864 set_tz();
866 led(LED_DIAG, 1);
869 int init_main(int argc, char *argv[])
871 pid_t shell_pid = 0;
873 sysinit();
875 state = START;
876 signaled = -1;
878 #if defined(DEBUG_NOISY)
879 nvram_set("debug_logeval", "1");
880 nvram_set("debug_cprintf", "1");
881 nvram_set("debug_cprintf_file", "1");
882 nvram_set("debug_ddns", "1");
883 #endif
885 for (;;) {
886 // TRACE_PT("main loop state=%d\n", state);
888 switch (state) {
889 case USER1:
890 exec_service();
891 state = IDLE;
892 break;
893 case RESTART:
894 case STOP:
895 case HALT:
896 case REBOOT:
897 led(LED_DIAG, 1);
899 run_nvscript("script_shut", NULL, 10);
901 stop_services();
902 stop_wan();
903 stop_lan();
904 stop_vlan();
906 // !!TB - USB Support
907 remove_storage_main();
908 stop_usb();
910 if ((state == REBOOT) || (state == HALT)) {
911 shutdn(state == REBOOT);
912 exit(0);
914 if (state == STOP) {
915 state = IDLE;
916 break;
919 // RESTART falls through
921 case START:
922 SET_LED(RELEASE_WAN_CONTROL);
924 int fd = -1;
925 if (!nvram_get_int("usb_nolock")) {
926 fd = file_lock("usb"); // hold off automount processing
927 start_usb();
930 run_nvscript("script_init", NULL, 2);
932 if (nvram_get_int("usb_nolock"))
933 start_usb();
934 file_unlock(fd); // allow to process usb hotplug events
935 #ifdef TCONFIG_USB
937 * On RESTART some partitions can stay mounted if they are busy at the moment.
938 * In that case USB drivers won't unload, and hotplug won't kick off again to
939 * remount those drives that actually got unmounted. Make sure to remount ALL
940 * partitions here by simulating hotplug event.
942 if (state == RESTART) add_remove_usbhost("-1", 1);
943 #endif
945 start_vlan();
946 start_lan();
947 start_wan(BOOT);
948 start_services();
950 syslog(LOG_INFO, "Tomato %s", tomato_version);
951 syslog(LOG_INFO, "%s", nvram_safe_get("t_model_name"));
953 led(LED_DIAG, 0);
954 notice_set("sysup", "");
956 state = IDLE;
958 // fall through
960 case IDLE:
961 while (signaled == -1) {
962 check_services();
963 if ((!noconsole) && ((!shell_pid) || (kill(shell_pid, 0) != 0))) {
964 shell_pid = run_shell(0, 1);
966 else {
967 pause();
970 state = signaled;
971 signaled = -1;
972 break;
977 int reboothalt_main(int argc, char *argv[])
979 int reboot = (strstr(argv[0], "reboot") != NULL);
980 puts(reboot ? "Rebooting..." : "Shutting down...");
981 fflush(stdout);
982 sleep(1);
983 kill(1, reboot ? SIGTERM : SIGQUIT);
984 return 0;