Support Asus RT-N10U
[tomato.git] / release / src / router / rc / init.c
blobb9450e33b88d26cd2aa1f8c6276920f3bc24919a
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 #include <sys/sysinfo.h>
29 #endif
30 #include <wlutils.h>
31 #include <bcmdevs.h>
33 #define SHELL "/bin/sh"
35 static int fatalsigs[] = {
36 SIGILL,
37 SIGABRT,
38 SIGFPE,
39 SIGPIPE,
40 SIGBUS,
41 SIGSYS,
42 SIGTRAP,
43 SIGPWR
46 static int initsigs[] = {
47 SIGHUP,
48 SIGUSR1,
49 SIGUSR2,
50 SIGINT,
51 SIGQUIT,
52 SIGALRM,
53 SIGTERM
56 static char *defenv[] = {
57 "TERM=vt100",
58 "HOME=/",
59 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
60 "SHELL=" SHELL,
61 "USER=root",
62 NULL
65 /* Set terminal settings to reasonable defaults */
66 static void set_term(int fd)
68 struct termios tty;
70 tcgetattr(fd, &tty);
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 */
83 tty.c_line = 0;
85 /* Make it be sane */
86 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
87 tty.c_cflag |= CREAD|HUPCL|CLOCAL;
90 /* input modes */
91 tty.c_iflag = ICRNL | IXON | IXOFF;
93 /* output modes */
94 tty.c_oflag = OPOST | ONLCR;
96 /* local modes */
97 tty.c_lflag =
98 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
100 tcsetattr(fd, TCSANOW, &tty);
103 static int console_init(void)
105 int fd;
107 /* Clean up */
108 ioctl(0, TIOCNOTTY, 0);
109 close(0);
110 close(1);
111 close(2);
112 setsid();
114 /* Reopen console */
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);
121 return errno;
123 dup2(fd, 0);
124 dup2(fd, 1);
125 dup2(fd, 2);
127 ioctl(0, TIOCSCTTY, 1);
128 tcsetpgrp(0, getpgrp());
129 set_term(0);
131 return 0;
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)
142 fd_set rfds;
143 struct timeval tv = { timeout, 0 };
145 FD_ZERO(&rfds);
146 FD_SET(fd, &rfds);
147 return select(fd + 1, &rfds, NULL, NULL, (timeout > 0) ? &tv : NULL);
150 static pid_t run_shell(int timeout, int nowait)
152 pid_t pid;
153 int sig;
155 /* Wait for user input */
156 if (waitfor(STDIN_FILENO, timeout) <= 0) return 0;
158 switch (pid = fork()) {
159 case -1:
160 perror("fork");
161 return 0;
162 case 0:
163 /* Reset signal handlers set for parent process */
164 for (sig = 0; sig < (_NSIG-1); sig++)
165 signal(sig, SIG_DFL);
167 /* Reopen console */
168 console_init();
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. */
176 perror(SHELL);
177 exit(errno);
178 default:
179 if (nowait) {
180 return pid;
182 else {
183 waitpid(pid, NULL, 0);
184 return 0;
189 int console_main(int argc, char *argv[])
191 for (;;) run_shell(0, 0);
193 return 0;
196 static void shutdn(int rb)
198 int i;
199 int act;
200 sigset_t ss;
202 _dprintf("shutdn rb=%d\n", rb);
204 sigemptyset(&ss);
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);
214 sleep(1);
216 set_action(ACT_REBOOT);
218 // Disconnect pppd - need this for PPTP/L2TP to finish gracefully
219 stop_pptp();
220 stop_l2tp();
222 _dprintf("TERM\n");
223 kill(-1, SIGTERM);
224 sleep(3);
225 sync();
227 _dprintf("KILL\n");
228 kill(-1, SIGKILL);
229 sleep(1);
230 sync();
232 umount("/jffs"); // may hang if not
233 sleep(1);
235 if (rb != -1) {
236 led(LED_WLAN, 0);
237 if (rb == 0) {
238 for (i = 4; i > 0; --i) {
239 led(LED_DMZ, 1);
240 led(LED_WHITE, 1);
241 usleep(250000);
242 led(LED_DMZ, 0);
243 led(LED_WHITE, 0);
244 usleep(250000);
249 reboot(rb ? RB_AUTOBOOT : RB_HALT_SYSTEM);
251 do {
252 sleep(1);
253 } while (1);
256 static void handle_fatalsigs(int sig)
258 _dprintf("fatal sig=%d\n", sig);
259 shutdn(-1);
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
268 * raising ALRM.
270 static void handle_reap(int sig)
272 chld_reap(sig);
273 raise(SIGALRM);
276 static int check_nv(const char *name, const char *value)
278 const char *p;
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);
283 return 1;
286 return 0;
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)
296 FILE *fp;
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);
303 fclose(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);
309 return 0;
312 static int find_dir320_mac_addr(void)
314 FILE *fp;
315 char *buffer, s[18];
316 int i, part, size, found = 0;
318 if (!mtd_getinfo("board_data", &part, &size))
319 goto out;
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);
330 s[17] = 0;
331 nvram_set("et0macaddr", s);
332 found = 1;
334 else if (!memcmp(buffer + i, "wanmac=", 7)) {
335 memcpy(s, buffer + i + 7, 17);
336 s[17] = 0;
337 nvram_set("il0macaddr", s);
338 if (!found) {
339 inc_mac(s, -1);
340 nvram_set("et0macaddr", s);
342 found = 1;
346 free(buffer);
347 fclose(fp);
349 out:
350 if (!found) {
351 strcpy(s, nvram_safe_get("wl0_hwaddr"));
352 inc_mac(s, -2);
353 nvram_set("et0macaddr", s);
355 return 1;
358 static int init_vlan_ports(void)
360 int dirty = 0;
361 int model = get_model();
363 switch (model) {
364 case MODEL_WRT54G:
365 switch (check_hw_type()) {
366 case HW_BCM5352E: // G v4, GS v3, v4
367 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
368 break;
370 break;
371 case MODEL_WTR54GS:
372 dirty |= check_nv("vlan0ports", "0 5*");
373 dirty |= check_nv("vlan1ports", "1 5");
374 dirty |= check_nv("vlan_enable", "1");
375 break;
376 case MODEL_WL500GP:
377 case MODEL_WL500GE:
378 case MODEL_WL500GPv2:
379 case MODEL_WL520GU:
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");
384 break;
385 case MODEL_WL500GD:
386 dirty |= check_nv("vlan0ports", "1 2 3 4 5*");
387 dirty |= check_nv("vlan1ports", "0 5");
388 break;
389 case MODEL_DIR320:
390 case MODEL_H618B:
391 dirty |= (nvram_get("vlan2ports") != NULL);
392 nvram_unset("vlan2ports");
393 dirty |= check_nv("vlan1ports", "0 5");
394 break;
395 case MODEL_WRT310Nv1:
396 dirty |= check_nv("vlan1ports", "1 2 3 4 8*");
397 dirty |= check_nv("vlan2ports", "0 8");
398 break;
399 case MODEL_WL1600GL:
400 dirty |= check_nv("vlan0ports", "0 1 2 3 5*");
401 dirty |= check_nv("vlan1ports", "4 5");
402 break;
403 #ifdef CONFIG_BCMWL5
404 case MODEL_WNR3500L:
405 case MODEL_WRT320N:
406 case MODEL_RTN16:
407 case MODEL_RTN66U:
408 dirty |= check_nv("vlan1ports", "4 3 2 1 8*");
409 dirty |= check_nv("vlan2ports", "0 8");
410 break;
411 case MODEL_WNR2000v2:
412 dirty |= check_nv("vlan0ports", "4 3 2 1 5*");
413 dirty |= check_nv("vlan1ports", "0 5");
414 break;
415 case MODEL_RTN10:
416 dirty |= check_nv("vlan1ports", "4 5");
417 break;
418 case MODEL_RTN10U:
419 dirty |= check_nv("vlan0ports", "1 2 3 4 5*");
420 dirty |= check_nv("vlan1ports", "0 5");
421 break;
422 case MODEL_RTN12:
423 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
424 dirty |= check_nv("vlan1ports", "4 5");
425 break;
426 case MODEL_WRT610Nv2:
427 case MODEL_F5D8235v3:
428 dirty |= check_nv("vlan1ports", "1 2 3 4 8*");
429 dirty |= check_nv("vlan2ports", "0 8");
430 break;
431 case MODEL_F7D3301:
432 case MODEL_F7D4301:
433 dirty |= check_nv("vlan1ports", "3 2 1 0 8*");
434 dirty |= check_nv("vlan2ports", "4 8");
435 break;
436 case MODEL_F7D3302:
437 case MODEL_F7D4302:
438 dirty |= check_nv("vlan1ports", "0 1 2 3 5*");
439 dirty |= check_nv("vlan2ports", "4 5");
440 break;
441 case MODEL_E4200:
442 dirty |= check_nv("vlan1ports", "0 1 2 3 8*");
443 dirty |= check_nv("vlan2ports", "4 8");
444 break;
445 case MODEL_WRT160Nv3:
446 if (nvram_match("vlan1ports", "1 2 3 4 5*")) {
447 // fix lan port numbering on CSE41, CSE51
448 dirty |= check_nv("vlan1ports", "4 3 2 1 5*");
450 else if (nvram_match("vlan1ports", "1 2 3 4 8*")) {
451 // WRT310Nv2 ?
452 dirty |= check_nv("vlan1ports", "4 3 2 1 8*");
454 break;
455 #endif
458 return dirty;
461 static void check_bootnv(void)
463 int dirty;
464 int hardware;
465 int model;
466 char mac[18];
468 model = get_model();
469 dirty = check_nv("wl0_leddc", "0x640000") | check_nv("wl1_leddc", "0x640000");
471 switch (model) {
472 case MODEL_WTR54GS:
473 dirty |= check_nv("vlan0hwname", "et0");
474 dirty |= check_nv("vlan1hwname", "et0");
475 break;
476 case MODEL_WBRG54:
477 dirty |= check_nv("wl0gpio0", "130");
478 break;
479 case MODEL_WR850GV1:
480 case MODEL_WR850GV2:
481 // need to cleanup some variables...
482 if ((nvram_get("t_model") == NULL) && (nvram_get("MyFirmwareVersion") != NULL)) {
483 nvram_unset("MyFirmwareVersion");
484 nvram_set("restore_defaults", "1");
486 break;
487 case MODEL_WL500W:
488 /* fix WL500W mac adresses for WAN port */
489 if (invalid_mac(nvram_get("et1macaddr"))) {
490 strcpy(mac, nvram_safe_get("et0macaddr"));
491 inc_mac(mac, 1);
492 dirty |= check_nv("et1macaddr", mac);
494 dirty |= check_nv("wl0gpio0", "0x88");
495 break;
496 case MODEL_WL500GP:
497 dirty |= check_nv("sdram_init", "0x0009"); // 32MB; defaults: 0x000b, 0x0009
498 dirty |= check_nv("wl0gpio0", "136");
499 break;
500 case MODEL_WL500GPv2:
501 case MODEL_WL520GU:
502 dirty |= check_nv("wl0gpio1", "136");
503 break;
504 case MODEL_WL500GD:
505 dirty |= check_nv("vlan0hwname", "et0");
506 dirty |= check_nv("vlan1hwname", "et0");
507 dirty |= check_nv("boardflags", "0x00000100"); // set BFL_ENETVLAN
508 nvram_unset("wl0gpio0");
509 break;
510 case MODEL_DIR320:
511 if (strlen(nvram_safe_get("et0macaddr")) == 12 ||
512 strlen(nvram_safe_get("il0macaddr")) == 12) {
513 dirty |= find_dir320_mac_addr();
515 if (nvram_get("vlan2hwname") != NULL) {
516 nvram_unset("vlan2hwname");
517 dirty = 1;
519 dirty |= check_nv("wandevs", "vlan1");
520 dirty |= check_nv("vlan1hwname", "et0");
521 dirty |= check_nv("wl0gpio0", "8");
522 dirty |= check_nv("wl0gpio1", "0");
523 dirty |= check_nv("wl0gpio2", "0");
524 dirty |= check_nv("wl0gpio3", "0");
525 case MODEL_WL1600GL:
526 if (invalid_mac(nvram_get("et0macaddr"))) {
527 dirty |= find_sercom_mac_addr();
529 break;
530 case MODEL_WRT160Nv1:
531 case MODEL_WRT310Nv1:
532 case MODEL_WRT300N:
533 dirty |= check_nv("wl0gpio0", "8");
534 break;
535 #ifdef CONFIG_BCMWL5
536 case MODEL_WNR3500L:
537 dirty |= check_nv("boardflags", "0x00000710"); // needed to enable USB
538 dirty |= check_nv("vlan2hwname", "et0");
539 dirty |= check_nv("ledbh0", "7");
540 break;
541 case MODEL_WNR2000v2:
542 dirty |= check_nv("ledbh5", "8");
543 break;
544 case MODEL_WRT320N:
545 dirty |= check_nv("reset_gpio", "5");
546 dirty |= check_nv("ledbh0", "136");
547 dirty |= check_nv("ledbh1", "11");
548 /* fall through, same as RT-N16 */
549 case MODEL_RTN16:
550 dirty |= check_nv("vlan2hwname", "et0");
551 break;
552 case MODEL_WRT610Nv2:
553 dirty |= check_nv("vlan2hwname", "et0");
554 dirty |= check_nv("pci/1/1/ledbh2", "8");
555 dirty |= check_nv("sb/1/ledbh1", "8");
556 if (invalid_mac(nvram_get("pci/1/1/macaddr"))) {
557 strcpy(mac, nvram_safe_get("et0macaddr"));
558 inc_mac(mac, 3);
559 dirty |= check_nv("pci/1/1/macaddr", mac);
561 break;
562 case MODEL_F7D3301:
563 case MODEL_F7D3302:
564 case MODEL_F7D4301:
565 case MODEL_F7D4302:
566 case MODEL_F5D8235v3:
567 if (nvram_match("sb/1/macaddr", nvram_safe_get("et0macaddr"))) {
568 strcpy(mac, nvram_safe_get("et0macaddr"));
569 inc_mac(mac, 2);
570 dirty |= check_nv("sb/1/macaddr", mac);
571 inc_mac(mac, 1);
572 dirty |= check_nv("pci/1/1/macaddr", mac);
574 case MODEL_E4200:
575 dirty |= check_nv("vlan2hwname", "et0");
576 if (invalid_mac(nvram_get("pci/1/1/macaddr")) == 0 ||
577 invalid_mac(nvram_get("sb/1/macaddr"))) {
578 strcpy(mac, nvram_safe_get("et0macaddr"));
579 inc_mac(mac, 2);
580 dirty |= check_nv("sb/1/macaddr", mac);
581 inc_mac(mac, 1);
582 dirty |= check_nv("pci/1/1/macaddr", mac);
584 break;
585 case MODEL_WRT160Nv3:
586 dirty |= check_nv("vlan2hwname", "et0");
587 break;
588 #endif
590 case MODEL_WRT54G:
591 if (strncmp(nvram_safe_get("pmon_ver"), "CFE", 3) != 0) return;
593 hardware = check_hw_type();
594 if (!nvram_get("boardtype") ||
595 !nvram_get("boardnum") ||
596 !nvram_get("boardflags") ||
597 !nvram_get("clkfreq") ||
598 !nvram_get("os_flash_addr") ||
599 !nvram_get("dl_ram_addr") ||
600 !nvram_get("os_ram_addr") ||
601 !nvram_get("scratch") ||
602 !nvram_get("et0macaddr") ||
603 ((hardware != HW_BCM4704_BCM5325F) && (!nvram_get("vlan0ports") || !nvram_get("vlan0hwname")))) {
604 _dprintf("Unable to find critical settings, erasing NVRAM\n");
605 mtd_erase("nvram");
606 goto REBOOT;
609 dirty |= check_nv("aa0", "3");
610 dirty |= check_nv("wl0gpio0", "136");
611 dirty |= check_nv("wl0gpio2", "0");
612 dirty |= check_nv("wl0gpio3", "0");
613 dirty |= check_nv("cctl", "0");
614 dirty |= check_nv("ccode", "0");
616 switch (hardware) {
617 case HW_BCM5325E:
618 /* Lower the DDR ram drive strength , the value will be stable for all boards
619 Latency 3 is more stable for all ddr 20050420 by honor */
620 dirty |= check_nv("sdram_init", "0x010b");
621 dirty |= check_nv("sdram_config", "0x0062");
622 if (!nvram_match("debug_clkfix", "0")) {
623 dirty |= check_nv("clkfreq", "216");
625 if (dirty) {
626 nvram_set("sdram_ncdl", "0x0");
628 dirty |= check_nv("pa0itssit", "62");
629 dirty |= check_nv("pa0b0", "0x15eb");
630 dirty |= check_nv("pa0b1", "0xfa82");
631 dirty |= check_nv("pa0b2", "0xfe66");
632 //dirty |= check_nv("pa0maxpwr", "0x4e");
633 break;
634 case HW_BCM5352E: // G v4, GS v3, v4
635 dirty |= check_nv("sdram_init", "0x010b");
636 dirty |= check_nv("sdram_config", "0x0062");
637 if (dirty) {
638 nvram_set("sdram_ncdl", "0x0");
640 dirty |= check_nv("pa0itssit", "62");
641 dirty |= check_nv("pa0b0", "0x168b");
642 dirty |= check_nv("pa0b1", "0xfabf");
643 dirty |= check_nv("pa0b2", "0xfeaf");
644 //dirty |= check_nv("pa0maxpwr", "0x4e");
645 break;
646 case HW_BCM5354G:
647 dirty |= check_nv("pa0itssit", "62");
648 dirty |= check_nv("pa0b0", "0x1326");
649 dirty |= check_nv("pa0b1", "0xFB51");
650 dirty |= check_nv("pa0b2", "0xFE87");
651 //dirty |= check_nv("pa0maxpwr", "0x4e");
652 break;
653 case HW_BCM4704_BCM5325F:
654 // nothing to do
655 break;
656 default:
657 dirty |= check_nv("pa0itssit", "62");
658 dirty |= check_nv("pa0b0", "0x170c");
659 dirty |= check_nv("pa0b1", "0xfa24");
660 dirty |= check_nv("pa0b2", "0xfe70");
661 //dirty |= check_nv("pa0maxpwr", "0x48");
662 break;
664 break;
666 } // switch (model)
668 dirty |= init_vlan_ports();
670 if (dirty) {
671 nvram_commit();
672 REBOOT: // do a simple reboot
673 sync();
674 reboot(RB_AUTOBOOT);
675 exit(0);
679 static int init_nvram(void)
681 unsigned long features;
682 int model;
683 const char *mfr;
684 const char *name;
685 char s[256];
686 unsigned long bf;
687 unsigned long n;
689 model = get_model();
690 sprintf(s, "%d", model);
691 nvram_set("t_model", s);
693 mfr = "Broadcom";
694 name = NULL;
695 features = 0;
696 switch (model) {
697 case MODEL_WRT54G:
698 mfr = "Linksys";
699 name = "WRT54G/GS/GL";
700 switch (check_hw_type()) {
701 case HW_BCM4712:
702 nvram_set("gpio2", "adm_eecs");
703 nvram_set("gpio3", "adm_eesk");
704 nvram_unset("gpio4");
705 nvram_set("gpio5", "adm_eedi");
706 nvram_set("gpio6", "adm_rc");
707 break;
708 case HW_BCM4702:
709 nvram_unset("gpio2");
710 nvram_unset("gpio3");
711 nvram_unset("gpio4");
712 nvram_unset("gpio5");
713 nvram_unset("gpio6");
714 break;
715 case HW_BCM5352E:
716 nvram_set("opo", "0x0008");
717 nvram_set("ag0", "0x02");
718 // drop
719 default:
720 nvram_set("gpio2", "ses_led");
721 nvram_set("gpio3", "ses_led2");
722 nvram_set("gpio4", "ses_button");
723 features = SUP_SES | SUP_WHAM_LED;
724 break;
726 break;
727 case MODEL_WTR54GS:
728 mfr = "Linksys";
729 name = "WTR54GS";
730 if (!nvram_match("t_fix1", (char *)name)) {
731 nvram_set("lan_ifnames", "vlan0 eth1");
732 nvram_set("gpio2", "ses_button");
733 nvram_set("reset_gpio", "7");
735 nvram_set("pa0itssit", "62");
736 nvram_set("pa0b0", "0x1542");
737 nvram_set("pa0b1", "0xfacb");
738 nvram_set("pa0b2", "0xfec7");
739 //nvram_set("pa0maxpwr", "0x4c");
740 features = SUP_SES;
741 break;
742 case MODEL_WRTSL54GS:
743 mfr = "Linksys";
744 name = "WRTSL54GS";
745 features = SUP_SES | SUP_WHAM_LED;
746 break;
747 case MODEL_WHRG54S:
748 mfr = "Buffalo";
749 name = "WHR-G54S";
750 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
751 break;
752 case MODEL_WHRHPG54:
753 case MODEL_WZRRSG54HP:
754 case MODEL_WZRHPG54:
755 mfr = "Buffalo";
756 features = SUP_SES | SUP_AOSS_LED | SUP_HPAMP;
757 switch (model) {
758 case MODEL_WZRRSG54HP:
759 name = "WZR-RS-G54HP";
760 break;
761 case MODEL_WZRHPG54:
762 name = "WZR-HP-G54";
763 break;
764 default:
765 name = "WHR-HP-G54";
766 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_HPAMP;
767 break;
770 bf = strtoul(nvram_safe_get("boardflags"), NULL, 0);
771 switch (bf) {
772 case 0x0758:
773 case 0x1758:
774 case 0x2758:
775 case 0x3758:
776 if (nvram_match("wlx_hpamp", "")) {
777 if (nvram_get_int("wl_txpwr") > 10) nvram_set("wl_txpwr", "10");
778 nvram_set("wlx_hpamp", "1");
779 nvram_set("wlx_hperx", "0");
782 n = bf;
783 if (nvram_match("wlx_hpamp", "0")) {
784 n &= ~0x2000UL;
786 else {
787 n |= 0x2000UL;
789 if (nvram_match("wlx_hperx", "0")) {
790 n |= 0x1000UL;
792 else {
793 n &= ~0x1000UL;
795 if (bf != n) {
796 sprintf(s, "0x%lX", n);
797 nvram_set("boardflags", s);
799 break;
800 default:
801 syslog(LOG_WARNING, "Unexpected: boardflag=%lX", bf);
802 break;
804 break;
805 case MODEL_WBRG54:
806 mfr = "Buffalo";
807 name = "WBR-G54";
808 break;
809 case MODEL_WBR2G54:
810 mfr = "Buffalo";
811 name = "WBR2-G54";
812 features = SUP_SES | SUP_AOSS_LED;
813 break;
814 case MODEL_WHR2A54G54:
815 mfr = "Buffalo";
816 name = "WHR2-A54G54";
817 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
818 break;
819 case MODEL_WHR3AG54:
820 mfr = "Buffalo";
821 name = "WHR3-AG54";
822 features = SUP_SES | SUP_AOSS_LED;
823 break;
824 case MODEL_WZRG54:
825 mfr = "Buffalo";
826 name = "WZR-G54";
827 features = SUP_SES | SUP_AOSS_LED;
828 break;
829 case MODEL_WZRRSG54:
830 mfr = "Buffalo";
831 name = "WZR-RS-G54";
832 features = SUP_SES | SUP_AOSS_LED;
833 break;
834 case MODEL_WVRG54NF:
835 mfr = "Buffalo";
836 name = "WVR-G54-NF";
837 features = SUP_SES;
838 break;
839 case MODEL_WZRG108:
840 mfr = "Buffalo";
841 name = "WZR-G108";
842 features = SUP_SES | SUP_AOSS_LED;
843 break;
844 case MODEL_RT390W:
845 mfr = "Fuji";
846 name = "RT390W";
847 break;
848 case MODEL_WR850GV1:
849 mfr = "Motorola";
850 name = "WR850G v1";
851 features = SUP_NONVE;
852 break;
853 case MODEL_WR850GV2:
854 mfr = "Motorola";
855 name = "WR850G v2/v3";
856 features = SUP_NONVE;
857 break;
858 case MODEL_WL500GP:
859 mfr = "Asus";
860 name = "WL-500gP";
861 features = SUP_SES;
862 #ifdef TCONFIG_USB
863 nvram_set("usb_ohci", "-1");
864 #endif
865 if (!nvram_match("t_fix1", (char *)name)) {
866 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3"); // set to "vlan0 eth2" by DD-WRT; default: vlan0 eth1
868 break;
869 case MODEL_WL500W:
870 mfr = "Asus";
871 name = "WL-500W";
872 features = SUP_SES | SUP_80211N;
873 #ifdef TCONFIG_USB
874 nvram_set("usb_ohci", "-1");
875 #endif
876 break;
877 case MODEL_WL500GE:
878 mfr = "Asus";
879 name = "WL-550gE";
880 // features = ?
881 #ifdef TCONFIG_USB
882 nvram_set("usb_uhci", "-1");
883 #endif
884 break;
885 case MODEL_WX6615GT:
886 mfr = "SparkLAN";
887 name = "WX-6615GT";
888 // features = ?
889 break;
890 case MODEL_MN700:
891 mfr = "Microsoft";
892 name = "MN-700";
893 break;
894 case MODEL_WR100:
895 mfr = "Viewsonic";
896 name = "WR100";
897 break;
898 case MODEL_WLA2G54L:
899 mfr = "Buffalo";
900 name = "WLA2-G54L";
901 if (!nvram_match("t_fix1", (char *)name)) {
902 nvram_set("lan_ifnames", "vlan0 eth1 eth2");
903 nvram_set("wl_ifname", "eth1");
904 nvram_set("wan_ifname", "none");
906 break;
907 case MODEL_TM2300:
908 mfr = "Dell";
909 name = "TrueMobile 2300";
910 break;
918 #ifndef WL_BSS_INFO_VERSION
919 #error WL_BSS_INFO_VERSION
920 #endif
921 #if WL_BSS_INFO_VERSION >= 108
923 case MODEL_WRH54G:
924 mfr = "Linksys";
925 name = "WRH54G";
927 nvram_set("opo", "12");
928 break;
930 case MODEL_WHRG125:
931 mfr = "Buffalo";
932 name = "WHR-G125";
933 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
935 nvram_set("opo", "0x0008");
936 nvram_set("ag0", "0x0C");
937 break;
938 #ifdef CONFIG_BCMWL5
939 case MODEL_RTN10:
940 mfr = "Asus";
941 name = "RT-N10";
942 features = SUP_SES | SUP_80211N;
943 if (!nvram_match("t_fix1", (char *)name)) {
944 nvram_set("lan_ifnames", "vlan0 eth1");
945 nvram_set("wan_ifnameX", "vlan1");
946 nvram_set("wl_ifname", "eth1");
948 break;
949 case MODEL_RTN10U:
950 mfr = "Asus";
951 name = "RT-N10U";
952 features = SUP_SES | SUP_80211N;
953 #ifdef TCONFIG_USB
954 nvram_set("usb_uhci", "-1");
955 #endif
956 if (!nvram_match("t_fix1", (char *)name)) {
957 nvram_set("lan_ifnames", "vlan0 eth1");
958 nvram_set("wan_ifnameX", "vlan1");
959 nvram_set("wl_ifname", "eth1");
961 break;
962 case MODEL_RTN12:
963 mfr = "Asus";
964 name = "RT-N12";
965 features = SUP_SES | SUP_BRAU | 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");
971 break;
972 case MODEL_RTN16:
973 mfr = "Asus";
974 name = "RT-N16";
975 features = SUP_SES | SUP_80211N | SUP_1000ET;
976 #ifdef TCONFIG_USB
977 nvram_set("usb_uhci", "-1");
978 #endif
979 if (!nvram_match("t_fix1", (char *)name)) {
980 nvram_set("lan_ifnames", "vlan1 eth1");
981 nvram_set("wan_ifnameX", "vlan2");
982 nvram_set("wl_ifname", "eth1");
983 nvram_set("vlan_enable", "1");
985 break;
986 case MODEL_RTN66U:
987 mfr = "Asus";
988 name = "RT-N66U";
989 features = SUP_SES | SUP_80211N | SUP_1000ET;
990 #ifdef TCONFIG_USB
991 nvram_set("usb_uhci", "-1");
992 #if defined(LINUX26) && defined(TCONFIG_USB_EXTRAS)
993 if (nvram_get_int("usb_mmc") == -1) nvram_set("usb_mmc", "0");
994 #endif
995 #endif
996 if (!nvram_match("t_fix1", (char *)name)) {
997 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
998 nvram_set("wan_ifnameX", "vlan2");
999 nvram_set("wl_ifnames", "eth1 eth2");
1000 nvram_set("landevs", "vlan1 wl0 wl1");
1001 nvram_set("wandevs", "vlan2");
1002 #if defined(LINUX26) && defined(TCONFIG_USB)
1003 nvram_set("usb_noled", "1-1.4"); /* SD/MMC Card */
1004 #endif
1006 break;
1007 case MODEL_WNR3500L:
1008 mfr = "Netgear";
1009 name = "WNR3500L/U/v2";
1010 features = SUP_SES | SUP_AOSS_LED | SUP_80211N | SUP_1000ET;
1011 if (!nvram_match("t_fix1", (char *)name)) {
1012 nvram_set("sromrev", "3");
1013 nvram_set("lan_ifnames", "vlan1 eth1");
1014 nvram_set("wan_ifnameX", "vlan2");
1015 nvram_set("wl_ifname", "eth1");
1017 break;
1018 case MODEL_WNR2000v2:
1019 mfr = "Netgear";
1020 name = "WNR2000 v2";
1021 features = SUP_SES | SUP_AOSS_LED | SUP_80211N;
1022 if (!nvram_match("t_fix1", (char *)name)) {
1023 nvram_set("lan_ifnames", "vlan0 eth1");
1024 nvram_set("wan_ifnameX", "vlan1");
1025 nvram_set("wl_ifname", "eth1");
1027 break;
1028 case MODEL_F7D3301:
1029 case MODEL_F7D3302:
1030 case MODEL_F7D4301:
1031 case MODEL_F7D4302:
1032 case MODEL_F5D8235v3:
1033 mfr = "Belkin";
1034 features = SUP_SES | SUP_80211N;
1035 switch (model) {
1036 case MODEL_F7D3301:
1037 name = "Share Max N300 (F7D3301/F7D7301) v1";
1038 break;
1039 case MODEL_F7D3302:
1040 name = "Share N300 (F7D3302/F7D7302) v1";
1041 break;
1042 case MODEL_F7D4301:
1043 name = "Play Max / N600 HD (F7D4301/F7D8301) v1";
1044 break;
1045 case MODEL_F7D4302:
1046 name = "Play N600 (F7D4302/F7D8302) v1";
1047 break;
1048 case MODEL_F5D8235v3:
1049 name = "N F5D8235-4 v3";
1050 break;
1052 #ifdef TCONFIG_USB
1053 nvram_set("usb_uhci", "-1");
1054 #endif
1055 if (!nvram_match("t_fix1", (char *)name)) {
1056 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1057 nvram_set("wan_ifnameX", "vlan2");
1058 nvram_set("landevs", "vlan1 wl0 wl1");
1059 nvram_set("wandevs", "vlan2");
1061 break;
1062 case MODEL_WRT160Nv3:
1063 // same as M10, M20, WRT310Nv2, E1000v1
1064 mfr = "Linksys";
1065 name = nvram_safe_get("boot_hw_model");
1066 if (strcmp(name, "E100") == 0)
1067 name = "E1000 v1";
1068 features = SUP_SES | SUP_80211N | SUP_WHAM_LED;
1069 if (!nvram_match("t_fix1", (char *)name)) {
1070 nvram_set("lan_ifnames", "vlan1 eth1");
1071 nvram_set("wan_ifnameX", "vlan2");
1072 nvram_set("wl_ifname", "eth1");
1074 break;
1075 case MODEL_WRT320N:
1076 mfr = "Linksys";
1077 name = nvram_match("boardrev", "0x1307") ? "E2000" : "WRT320N";
1078 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1079 if (!nvram_match("t_fix1", (char *)name)) {
1080 nvram_set("lan_ifnames", "vlan1 eth1");
1081 nvram_set("wan_ifnameX", "vlan2");
1082 nvram_set("wl_ifname", "eth1");
1084 break;
1085 case MODEL_WRT610Nv2:
1086 mfr = "Linksys";
1087 name = nvram_match("boot_hw_model", "E300") ? "E3000" : "WRT610N v2";
1088 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1089 #ifdef TCONFIG_USB
1090 nvram_set("usb_uhci", "-1");
1091 #endif
1092 if (!nvram_match("t_fix1", (char *)name)) {
1093 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1094 nvram_set("wan_ifnameX", "vlan2");
1095 nvram_set("wl_ifname", "eth1");
1097 break;
1098 case MODEL_E4200:
1099 mfr = "Linksys";
1100 name = "E4200 v1";
1101 features = SUP_SES | SUP_80211N | SUP_1000ET;
1102 #ifdef TCONFIG_USB
1103 nvram_set("usb_uhci", "-1");
1104 #endif
1105 if (!nvram_match("t_fix1", (char *)name)) {
1106 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1107 nvram_set("wan_ifnameX", "vlan2");
1108 nvram_set("wl_ifname", "eth1");
1110 break;
1111 #endif // CONFIG_BCMWL5
1112 case MODEL_WL500GPv2:
1113 mfr = "Asus";
1114 name = "WL-500gP v2";
1115 features = SUP_SES;
1116 #ifdef TCONFIG_USB
1117 nvram_set("usb_uhci", "-1");
1118 #endif
1119 break;
1120 case MODEL_WL520GU:
1121 mfr = "Asus";
1122 name = "WL-520GU";
1123 features = SUP_SES;
1124 #ifdef TCONFIG_USB
1125 nvram_set("usb_uhci", "-1");
1126 #endif
1127 break;
1128 case MODEL_WL500GD:
1129 mfr = "Asus";
1130 name = "WL-500g Deluxe";
1131 // features = SUP_SES;
1132 #ifdef TCONFIG_USB
1133 nvram_set("usb_ohci", "-1");
1134 #endif
1135 if (!nvram_match("t_fix1", (char *)name)) {
1136 nvram_set("wl_ifname", "eth1");
1137 nvram_set("lan_ifnames", "vlan0 eth1");
1138 nvram_set("wan_ifnameX", "vlan1");
1139 nvram_unset("wl0gpio0");
1141 break;
1142 case MODEL_DIR320:
1143 mfr = "D-Link";
1144 name = "DIR-320";
1145 features = SUP_SES;
1146 if (!nvram_match("t_fix1", (char *)name)) {
1147 nvram_set("wan_ifnameX", "vlan1");
1148 nvram_set("wl_ifname", "eth1");
1150 break;
1151 case MODEL_H618B:
1152 mfr = "ZTE";
1153 name = "ZXV10 H618B";
1154 features = SUP_SES | SUP_AOSS_LED;
1155 break;
1156 case MODEL_WL1600GL:
1157 mfr = "Ovislink";
1158 name = "WL1600GL";
1159 features = SUP_SES;
1160 break;
1161 #endif // WL_BSS_INFO_VERSION >= 108
1162 case MODEL_WZRG300N:
1163 mfr = "Buffalo";
1164 name = "WZR-G300N";
1165 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_80211N;
1166 break;
1167 case MODEL_WRT160Nv1:
1168 case MODEL_WRT300N:
1169 mfr = "Linksys";
1170 name = (model == MODEL_WRT300N) ? "WRT300N v1" : "WRT160N v1";
1171 features = SUP_SES | SUP_80211N;
1172 if (!nvram_match("t_fix1", (char *)name)) {
1173 nvram_set("wan_ifnameX", "eth1");
1174 nvram_set("lan_ifnames", "eth0 eth2");
1176 break;
1177 case MODEL_WRT310Nv1:
1178 mfr = "Linksys";
1179 name = "WRT310N v1";
1180 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1181 if (!nvram_match("t_fix1", (char *)name)) {
1182 nvram_set("lan_ifnames", "vlan1 eth1");
1183 nvram_set("wan_ifnameX", "vlan2");
1184 nvram_set("wl_ifname", "eth1");
1186 break;
1189 if (name) {
1190 nvram_set("t_fix1", name);
1191 sprintf(s, "%s %s", mfr, name);
1193 else {
1194 snprintf(s, sizeof(s), "%s %d/%s/%s/%s/%s", mfr, check_hw_type(),
1195 nvram_safe_get("boardtype"), nvram_safe_get("boardnum"), nvram_safe_get("boardrev"), nvram_safe_get("boardflags"));
1196 s[64] = 0;
1198 nvram_set("t_model_name", s);
1200 nvram_set("pa0maxpwr", "400"); // allow Tx power up tp 400 mW, needed for ND only
1202 sprintf(s, "0x%lX", features);
1203 nvram_set("t_features", s);
1206 note: set wan_ifnameX if wan_ifname needs to be overriden
1209 if (nvram_is_empty("wan_ifnameX")) {
1210 #if 1
1211 nvram_set("wan_ifnameX", ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
1212 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1");
1213 #else
1214 p = nvram_safe_get("wan_ifname");
1215 if ((*p == 0) || (nvram_match("wl_ifname", p))) {
1216 p = ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
1217 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1";
1219 nvram_set("wan_ifnameX", p);
1220 #endif
1223 nvram_set("wl_hwaddr", ""); // zzz- when disabling wireless, we must get null wireless mac ??
1225 //!!TB - do not force country code here to allow nvram override
1226 //nvram_set("wl_country", "JP");
1227 //nvram_set("wl_country_code", "JP");
1228 nvram_set("wan_get_dns", "");
1229 nvram_set("wan_get_domain", "");
1230 nvram_set("ppp_get_ip", "");
1231 nvram_set("action_service", "");
1232 nvram_set("jffs2_format", "0");
1233 nvram_set("rrules_radio", "-1");
1234 nvram_unset("https_crt_gen");
1235 nvram_unset("log_wmclear");
1236 #ifdef TCONFIG_IPV6
1237 nvram_set("ipv6_get_dns", "");
1238 #endif
1239 #ifdef TCONFIG_MEDIA_SERVER
1240 nvram_unset("ms_rescan");
1241 #endif
1242 if (nvram_get_int("http_id_gen") == 1) nvram_unset("http_id");
1244 nvram_unset("sch_rboot_last");
1245 nvram_unset("sch_rcon_last");
1246 nvram_unset("sch_c1_last");
1247 nvram_unset("sch_c2_last");
1248 nvram_unset("sch_c3_last");
1250 nvram_set("brau_state", "");
1251 if ((features & SUP_BRAU) == 0) nvram_set("script_brau", "");
1252 if ((features & SUP_SES) == 0) nvram_set("sesx_script", "");
1254 if ((features & SUP_1000ET) == 0) nvram_set("jumbo_frame_enable", "0");
1256 // compatibility with old versions
1257 if (nvram_match("wl_net_mode", "disabled")) {
1258 nvram_set("wl_radio", "0");
1259 nvram_set("wl_net_mode", "mixed");
1262 return 0;
1265 /* Get the special files from nvram and copy them to disc.
1266 * These were files saved with "nvram setfile2nvram <filename>".
1267 * Better hope that they were saved with full pathname.
1269 static void load_files_from_nvram(void)
1271 char *name, *cp;
1272 int ar_loaded = 0;
1273 char buf[NVRAM_SPACE];
1275 if (nvram_getall(buf, sizeof(buf)) != 0)
1276 return;
1278 for (name = buf; *name; name += strlen(name) + 1) {
1279 if (strncmp(name, "FILE:", 5) == 0) { /* This special name marks a file to get. */
1280 if ((cp = strchr(name, '=')) == NULL)
1281 continue;
1282 *cp = 0;
1283 syslog(LOG_INFO, "Loading file '%s' from nvram", name + 5);
1284 nvram_nvram2file(name, name + 5);
1285 if (memcmp(".autorun", cp - 8, 9) == 0)
1286 ++ar_loaded;
1289 /* Start any autorun files that may have been loaded into one of the standard places. */
1290 if (ar_loaded != 0)
1291 run_nvscript(".autorun", NULL, 3);
1294 #if defined(LINUX26) && defined(TCONFIG_USB)
1295 static inline void tune_min_free_kbytes(void)
1297 struct sysinfo info;
1299 memset(&info, 0, sizeof(struct sysinfo));
1300 sysinfo(&info);
1301 if (info.totalram >= 55 * 1024 * 1024) {
1302 // If we have 64MB+ RAM, tune min_free_kbytes
1303 // to reduce page allocation failure errors.
1304 f_write_string("/proc/sys/vm/min_free_kbytes", "8192", 0, 0);
1307 #endif
1309 static void sysinit(void)
1311 static int noconsole = 0;
1312 static const time_t tm = 0;
1313 int hardware;
1314 int i;
1315 DIR *d;
1316 struct dirent *de;
1317 char s[256];
1318 char t[256];
1320 mount("proc", "/proc", "proc", 0, NULL);
1321 mount("tmpfs", "/tmp", "tmpfs", 0, NULL);
1323 #ifdef LINUX26
1324 mount("devfs", "/dev", "tmpfs", MS_MGC_VAL | MS_NOATIME, NULL);
1325 mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3));
1326 mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1));
1327 mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
1328 mkdir("/dev/shm", 0777);
1329 mkdir("/dev/pts", 0777);
1330 mknod("/dev/pts/ptmx", S_IRWXU|S_IFCHR, makedev(5, 2));
1331 mknod("/dev/pts/0", S_IRWXU|S_IFCHR, makedev(136, 0));
1332 mknod("/dev/pts/1", S_IRWXU|S_IFCHR, makedev(136, 1));
1333 mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
1334 #endif
1336 if (console_init()) noconsole = 1;
1338 stime(&tm);
1340 static const char *mkd[] = {
1341 "/tmp/etc", "/tmp/var", "/tmp/home", "/tmp/mnt",
1342 "/tmp/splashd", //!!Victek
1343 "/tmp/share", "/var/webmon", // !!TB
1344 "/var/log", "/var/run", "/var/tmp", "/var/lib", "/var/lib/misc",
1345 "/var/spool", "/var/spool/cron", "/var/spool/cron/crontabs",
1346 "/tmp/var/wwwext", "/tmp/var/wwwext/cgi-bin", // !!TB - CGI support
1347 NULL
1349 umask(0);
1350 for (i = 0; mkd[i]; ++i) {
1351 mkdir(mkd[i], 0755);
1353 mkdir("/var/lock", 0777);
1354 mkdir("/var/tmp/dhcp", 0777);
1355 mkdir("/home/root", 0700);
1356 chmod("/tmp", 0777);
1357 f_write("/etc/hosts", NULL, 0, 0, 0644); // blank
1358 f_write("/etc/fstab", NULL, 0, 0, 0644); // !!TB - blank
1359 simple_unlock("cron");
1360 simple_unlock("firewall");
1361 simple_unlock("restrictions");
1362 umask(022);
1364 if ((d = opendir("/rom/etc")) != NULL) {
1365 while ((de = readdir(d)) != NULL) {
1366 if (de->d_name[0] == '.') continue;
1367 snprintf(s, sizeof(s), "%s/%s", "/rom/etc", de->d_name);
1368 snprintf(t, sizeof(t), "%s/%s", "/etc", de->d_name);
1369 symlink(s, t);
1371 closedir(d);
1373 symlink("/proc/mounts", "/etc/mtab");
1375 #ifdef TCONFIG_SAMBASRV
1376 if ((d = opendir("/usr/codepages")) != NULL) {
1377 while ((de = readdir(d)) != NULL) {
1378 if (de->d_name[0] == '.') continue;
1379 snprintf(s, sizeof(s), "/usr/codepages/%s", de->d_name);
1380 snprintf(t, sizeof(t), "/usr/share/%s", de->d_name);
1381 symlink(s, t);
1383 closedir(d);
1385 #endif
1387 #ifdef LINUX26
1388 eval("hotplug2", "--coldplug");
1389 start_hotplug2();
1391 static const char *dn[] = {
1392 "null", "zero", "random", "urandom", "full", "ptmx", "nvram",
1393 NULL
1395 for (i = 0; dn[i]; ++i) {
1396 snprintf(s, sizeof(s), "/dev/%s", dn[i]);
1397 chmod(s, 0666);
1399 chmod("/dev/gpio", 0660);
1400 #endif
1402 set_action(ACT_IDLE);
1404 for (i = 0; defenv[i]; ++i) {
1405 putenv(defenv[i]);
1408 if (!noconsole) {
1409 printf("\n\nHit ENTER for console...\n\n");
1410 run_shell(1, 0);
1413 check_bootnv();
1415 #ifdef TCONFIG_IPV6
1416 // disable IPv6 by default on all interfaces
1417 f_write_string("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1", 0, 0);
1418 #endif
1420 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++) {
1421 signal(fatalsigs[i], handle_fatalsigs);
1423 signal(SIGCHLD, handle_reap);
1425 #ifdef CONFIG_BCMWL5
1426 // ctf must be loaded prior to any other modules
1427 if (nvram_invmatch("ctf_disable", "1"))
1428 modprobe("ctf");
1429 #endif
1431 #ifdef TCONFIG_EMF
1432 modprobe("emf");
1433 modprobe("igs");
1434 #endif
1436 switch (hardware = check_hw_type()) {
1437 case HW_BCM4785:
1438 modprobe("bcm57xx");
1439 break;
1440 default:
1441 modprobe("et");
1442 break;
1445 modprobe("wl");
1447 config_loopback();
1449 eval("nvram", "defaults", "--initcheck");
1450 init_nvram();
1452 // set the packet size
1453 if (nvram_get_int("jumbo_frame_enable")) {
1454 // only set the size here - 'enable' flag is set by the driver
1455 // eval("et", "robowr", "0x40", "0x01", "0x1F"); // (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)
1456 eval("et", "robowr", "0x40", "0x05", nvram_safe_get("jumbo_frame_size"));
1459 klogctl(8, NULL, nvram_get_int("console_loglevel"));
1461 #if defined(LINUX26) && defined(TCONFIG_USB)
1462 tune_min_free_kbytes();
1463 #endif
1464 setup_conntrack();
1465 set_host_domain_name();
1467 set_tz();
1469 eval("buttons");
1471 if (!noconsole) xstart("console");
1473 i = nvram_get_int("sesx_led");
1474 led(LED_AMBER, (i & 1) != 0);
1475 led(LED_WHITE, (i & 2) != 0);
1476 led(LED_AOSS, (i & 4) != 0);
1477 led(LED_BRIDGE, (i & 8) != 0);
1478 led(LED_DIAG, 1);
1481 int init_main(int argc, char *argv[])
1483 int state, i;
1484 sigset_t sigset;
1486 // AB - failsafe?
1487 nvram_unset("debug_rc_svc");
1489 sysinit();
1491 sigemptyset(&sigset);
1492 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++) {
1493 sigaddset(&sigset, initsigs[i]);
1495 sigprocmask(SIG_BLOCK, &sigset, NULL);
1497 #if defined(DEBUG_NOISY)
1498 nvram_set("debug_logeval", "1");
1499 nvram_set("debug_cprintf", "1");
1500 nvram_set("debug_cprintf_file", "1");
1501 nvram_set("debug_ddns", "1");
1502 #endif
1504 start_jffs2();
1506 state = SIGUSR2; /* START */
1508 for (;;) {
1509 TRACE_PT("main loop signal/state=%d\n", state);
1511 switch (state) {
1512 case SIGUSR1: /* USER1: service handler */
1513 exec_service();
1514 break;
1516 case SIGHUP: /* RESTART */
1517 case SIGINT: /* STOP */
1518 case SIGQUIT: /* HALT */
1519 case SIGTERM: /* REBOOT */
1520 led(LED_DIAG, 1);
1521 unlink("/var/notice/sysup");
1523 run_nvscript("script_shut", NULL, 10);
1525 stop_services();
1526 stop_wan();
1527 stop_lan();
1528 stop_vlan();
1529 stop_syslog();
1531 if ((state == SIGTERM /* REBOOT */) ||
1532 (state == SIGQUIT /* HALT */)) {
1533 remove_storage_main(1);
1534 stop_usb();
1536 shutdn(state == SIGTERM /* REBOOT */);
1537 exit(0);
1539 if (state == SIGINT /* STOP */) {
1540 break;
1543 // SIGHUP (RESTART) falls through
1545 case SIGUSR2: /* START */
1546 SET_LED(RELEASE_WAN_CONTROL);
1547 start_syslog();
1549 load_files_from_nvram();
1551 int fd = -1;
1552 fd = file_lock("usb"); // hold off automount processing
1553 start_usb();
1555 run_nvscript("script_init", NULL, 2);
1557 file_unlock(fd); // allow to process usb hotplug events
1558 #ifdef TCONFIG_USB
1560 * On RESTART some partitions can stay mounted if they are busy at the moment.
1561 * In that case USB drivers won't unload, and hotplug won't kick off again to
1562 * remount those drives that actually got unmounted. Make sure to remount ALL
1563 * partitions here by simulating hotplug event.
1565 if (state == SIGHUP /* RESTART */)
1566 add_remove_usbhost("-1", 1);
1567 #endif
1569 create_passwd();
1570 start_vlan();
1571 start_lan();
1572 start_arpbind();
1573 start_wan(BOOT);
1574 start_services();
1575 start_wl();
1577 #ifdef CONFIG_BCMWL5
1578 if (wds_enable()) {
1579 /* Restart NAS one more time - for some reason without
1580 * this the new driver doesn't always bring WDS up.
1582 stop_nas();
1583 start_nas();
1585 #else
1586 #ifdef TCONFIG_VLAN
1587 if (wl_security_on()) {
1588 stop_nas();
1589 start_nas();
1591 #endif /* TCONFIG_VLAN */
1592 #endif
1594 syslog(LOG_INFO, "%s: Tomato %s", nvram_safe_get("t_model_name"), tomato_version);
1596 led(LED_DIAG, 0);
1597 notice_set("sysup", "");
1598 break;
1601 chld_reap(0); /* Periodically reap zombies. */
1602 check_services();
1603 sigwait(&sigset, &state);
1606 return 0;
1609 int reboothalt_main(int argc, char *argv[])
1611 int reboot = (strstr(argv[0], "reboot") != NULL);
1612 puts(reboot ? "Rebooting..." : "Shutting down...");
1613 fflush(stdout);
1614 sleep(1);
1615 kill(1, reboot ? SIGTERM : SIGQUIT);
1617 /* In the case we're hung, we'll get stuck and never actually reboot.
1618 * The only way out is to pull power.
1619 * So after 'reset_wait' seconds (default: 20), forcibly crash & restart.
1621 if (fork() == 0) {
1622 int wait = nvram_get_int("reset_wait") ? : 20;
1623 if ((wait < 10) || (wait > 120)) wait = 10;
1625 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
1626 sleep(wait);
1627 puts("Still running... Doing machine reset.");
1628 fflush(stdout);
1629 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
1630 sleep(1);
1631 f_write("/proc/sysrq-trigger", "b", 1, 0 , 0); /* machine reset */
1634 return 0;