Enable AOSS LED
[tomato.git] / release / src / router / rc / init.c
blobaee34a82d15a959656e5d97fc5552f5e0b0c1e7b
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 case MODEL_WL330GE:
381 if (nvram_match("vlan1ports", "0 5u")) // 520GU or 330GE or WL500GE?
382 dirty |= check_nv("vlan1ports", "0 5");
383 else if (nvram_match("vlan1ports", "4 5u"))
384 dirty |= check_nv("vlan1ports", "4 5");
385 break;
386 case MODEL_WL500GD:
387 dirty |= check_nv("vlan0ports", "1 2 3 4 5*");
388 dirty |= check_nv("vlan1ports", "0 5");
389 break;
390 case MODEL_DIR320:
391 case MODEL_H618B:
392 dirty |= (nvram_get("vlan2ports") != NULL);
393 nvram_unset("vlan2ports");
394 dirty |= check_nv("vlan1ports", "0 5");
395 break;
396 case MODEL_WRT310Nv1:
397 dirty |= check_nv("vlan1ports", "1 2 3 4 8*");
398 dirty |= check_nv("vlan2ports", "0 8");
399 break;
400 case MODEL_WL1600GL:
401 dirty |= check_nv("vlan0ports", "0 1 2 3 5*");
402 dirty |= check_nv("vlan1ports", "4 5");
403 break;
404 #ifdef CONFIG_BCMWL5
405 case MODEL_WNR3500L:
406 case MODEL_WRT320N:
407 case MODEL_WNR3500LV2:
408 case MODEL_RTN16:
409 case MODEL_RTN66U:
410 dirty |= check_nv("vlan1ports", "4 3 2 1 8*");
411 dirty |= check_nv("vlan2ports", "0 8");
412 break;
413 case MODEL_W1800R:
414 dirty |= check_nv("vlan1ports", "1 2 3 4 8*");
415 dirty |= check_nv("vlan2ports", "0 8");
416 break;
417 case MODEL_RTN53:
418 dirty |= check_nv("vlan2ports", "0 1 2 3 5*");
419 dirty |= check_nv("vlan1ports", "4 5");
420 break;
421 case MODEL_RTN53A1:
422 dirty |= check_nv("vlan1ports", "4 5");
423 dirty |= check_nv("vlan2ports", "3 2 1 0 5*");
424 break;
425 case MODEL_WNR2000v2:
426 dirty |= check_nv("vlan1ports", "4 3 2 1 5*");
427 dirty |= check_nv("vlan2ports", "0 5");
428 break;
429 case MODEL_HG320:
430 case MODEL_H218N:
431 case MODEL_RG200E_CA:
432 dirty |= check_nv("vlan1ports", "1 2 3 4 5*");
433 dirty |= check_nv("vlan2ports", "0 5");
434 break;
435 case MODEL_RTN10:
436 dirty |= check_nv("vlan1ports", "4 5");
437 break;
438 case MODEL_RTN10U:
439 case MODEL_CW5358U:
440 dirty |= check_nv("vlan0ports", "1 2 3 4 5*");
441 dirty |= check_nv("vlan1ports", "0 5");
442 break;
443 case MODEL_RTN10P:
444 case MODEL_RTN12:
445 case MODEL_RTN12B1:
446 dirty |= check_nv("vlan0ports", "3 2 1 0 5*");
447 dirty |= check_nv("vlan1ports", "4 5");
448 break;
449 case MODEL_WRT610Nv2:
450 case MODEL_F5D8235v3:
451 dirty |= check_nv("vlan1ports", "1 2 3 4 8*");
452 dirty |= check_nv("vlan2ports", "0 8");
453 break;
454 case MODEL_F7D3301:
455 case MODEL_F7D4301:
456 dirty |= check_nv("vlan1ports", "3 2 1 0 8*");
457 dirty |= check_nv("vlan2ports", "4 8");
458 break;
459 case MODEL_E900:
460 case MODEL_E1500:
461 case MODEL_E1550:
462 case MODEL_E2500:
463 case MODEL_F7D3302:
464 case MODEL_F7D4302:
465 case MODEL_DIR620C1:
466 dirty |= check_nv("vlan1ports", "0 1 2 3 5*");
467 dirty |= check_nv("vlan2ports", "4 5");
468 break;
469 case MODEL_E1000v2:
470 case MODEL_L600N:
471 dirty |= check_nv("vlan1ports", "1 2 3 4 5*");
472 dirty |= check_nv("vlan2ports", "0 5");
473 break;
474 case MODEL_RTN15U:
475 case MODEL_E3200:
476 case MODEL_E4200:
477 case MODEL_WNDR4000:
478 dirty |= check_nv("vlan1ports", "0 1 2 3 8*");
479 dirty |= check_nv("vlan2ports", "4 8");
480 break;
481 case MODEL_WRT160Nv3:
482 if (nvram_match("vlan1ports", "1 2 3 4 5*")) {
483 // fix lan port numbering on CSE41, CSE51
484 dirty |= check_nv("vlan1ports", "4 3 2 1 5*");
486 else if (nvram_match("vlan1ports", "1 2 3 4 8*")) {
487 // WRT310Nv2 ?
488 dirty |= check_nv("vlan1ports", "4 3 2 1 8*");
490 break;
491 #endif
494 return dirty;
497 static void check_bootnv(void)
499 int dirty;
500 int hardware;
501 int model;
502 char mac[18];
504 model = get_model();
505 dirty = check_nv("wl0_leddc", "0x640000") | check_nv("wl1_leddc", "0x640000");
507 switch (model) {
508 case MODEL_WTR54GS:
509 dirty |= check_nv("vlan0hwname", "et0");
510 dirty |= check_nv("vlan1hwname", "et0");
511 break;
512 case MODEL_WBRG54:
513 dirty |= check_nv("wl0gpio0", "130");
514 break;
515 case MODEL_WR850GV1:
516 case MODEL_WR850GV2:
517 // need to cleanup some variables...
518 if ((nvram_get("t_model") == NULL) && (nvram_get("MyFirmwareVersion") != NULL)) {
519 nvram_unset("MyFirmwareVersion");
520 nvram_set("restore_defaults", "1");
522 break;
523 case MODEL_WL330GE:
524 dirty |= check_nv("wl0gpio1", "0x02");
525 break;
526 case MODEL_WL500W:
527 /* fix WL500W mac adresses for WAN port */
528 if (invalid_mac(nvram_get("et1macaddr"))) {
529 strcpy(mac, nvram_safe_get("et0macaddr"));
530 inc_mac(mac, 1);
531 dirty |= check_nv("et1macaddr", mac);
533 dirty |= check_nv("wl0gpio0", "0x88");
534 break;
535 case MODEL_WL500GP:
536 dirty |= check_nv("sdram_init", "0x0009"); // 32MB; defaults: 0x000b, 0x0009
537 dirty |= check_nv("wl0gpio0", "136");
538 break;
539 case MODEL_WL500GPv2:
540 case MODEL_WL520GU:
541 dirty |= check_nv("wl0gpio1", "136");
542 break;
543 case MODEL_WL500GD:
544 dirty |= check_nv("vlan0hwname", "et0");
545 dirty |= check_nv("vlan1hwname", "et0");
546 dirty |= check_nv("boardflags", "0x00000100"); // set BFL_ENETVLAN
547 nvram_unset("wl0gpio0");
548 break;
549 case MODEL_DIR320:
550 if (strlen(nvram_safe_get("et0macaddr")) == 12 ||
551 strlen(nvram_safe_get("il0macaddr")) == 12) {
552 dirty |= find_dir320_mac_addr();
554 if (nvram_get("vlan2hwname") != NULL) {
555 nvram_unset("vlan2hwname");
556 dirty = 1;
558 dirty |= check_nv("wandevs", "vlan1");
559 dirty |= check_nv("vlan1hwname", "et0");
560 dirty |= check_nv("wl0gpio0", "8");
561 dirty |= check_nv("wl0gpio1", "0");
562 dirty |= check_nv("wl0gpio2", "0");
563 dirty |= check_nv("wl0gpio3", "0");
564 case MODEL_WL1600GL:
565 if (invalid_mac(nvram_get("et0macaddr"))) {
566 dirty |= find_sercom_mac_addr();
568 break;
569 case MODEL_WRT160Nv1:
570 case MODEL_WRT310Nv1:
571 case MODEL_WRT300N:
572 dirty |= check_nv("wl0gpio0", "8");
573 break;
574 #ifdef CONFIG_BCMWL5
575 case MODEL_WNR3500L:
576 dirty |= check_nv("boardflags", "0x00000710"); // needed to enable USB
577 dirty |= check_nv("vlan2hwname", "et0");
578 dirty |= check_nv("ledbh0", "7");
579 break;
580 case MODEL_WNR3500LV2:
581 dirty |= check_nv("vlan2hwname", "et0");
582 break;
583 case MODEL_WNR2000v2:
584 dirty |= check_nv("ledbh5", "8");
585 break;
586 case MODEL_WRT320N:
587 dirty |= check_nv("reset_gpio", "5");
588 dirty |= check_nv("ledbh0", "136");
589 dirty |= check_nv("ledbh1", "11");
590 /* fall through, same as RT-N16 */
591 case MODEL_RTN16:
592 dirty |= check_nv("vlan2hwname", "et0");
593 break;
594 case MODEL_HG320:
595 case MODEL_RG200E_CA:
596 case MODEL_H218N:
597 dirty |= check_nv("vlan1hwname", "et0");
598 dirty |= check_nv("vlan2hwname", "et0");
599 dirty |= check_nv("reset_gpio", "30");
600 break;
601 case MODEL_WRT610Nv2:
602 dirty |= check_nv("vlan2hwname", "et0");
603 dirty |= check_nv("pci/1/1/ledbh2", "8");
604 dirty |= check_nv("sb/1/ledbh1", "8");
605 if (invalid_mac(nvram_get("pci/1/1/macaddr"))) {
606 strcpy(mac, nvram_safe_get("et0macaddr"));
607 inc_mac(mac, 3);
608 dirty |= check_nv("pci/1/1/macaddr", mac);
610 break;
611 case MODEL_F7D3301:
612 case MODEL_F7D3302:
613 case MODEL_F7D4301:
614 case MODEL_F7D4302:
615 case MODEL_F5D8235v3:
616 if (nvram_match("sb/1/macaddr", nvram_safe_get("et0macaddr"))) {
617 strcpy(mac, nvram_safe_get("et0macaddr"));
618 inc_mac(mac, 2);
619 dirty |= check_nv("sb/1/macaddr", mac);
620 inc_mac(mac, 1);
621 dirty |= check_nv("pci/1/1/macaddr", mac);
623 case MODEL_E4200:
624 dirty |= check_nv("vlan2hwname", "et0");
625 if (strncasecmp(nvram_safe_get("pci/1/1/macaddr"), "00:90:4c", 8) == 0 ||
626 strncasecmp(nvram_safe_get("sb/1/macaddr"), "00:90:4c", 8) == 0) {
627 strcpy(mac, nvram_safe_get("et0macaddr"));
628 inc_mac(mac, 2);
629 dirty |= check_nv("sb/1/macaddr", mac);
630 inc_mac(mac, 1);
631 dirty |= check_nv("pci/1/1/macaddr", mac);
633 break;
634 case MODEL_WNDR4000:
635 dirty |= check_nv("vlan2hwname", "et0");
636 if (strncasecmp(nvram_safe_get("pci/1/1/macaddr"), "84:1B:5E", 8) == 0 ||
637 strncasecmp(nvram_safe_get("sb/1/macaddr"), "84:1B:5E", 8) == 0) {
638 strcpy(mac, nvram_safe_get("et0macaddr"));
639 inc_mac(mac, 2);
640 dirty |= check_nv("sb/1/macaddr", mac);
641 inc_mac(mac, 1);
642 dirty |= check_nv("pci/1/1/macaddr", mac);
644 break;
645 case MODEL_E900:
646 case MODEL_E1000v2:
647 case MODEL_E1500:
648 case MODEL_E1550:
649 case MODEL_E2500:
650 case MODEL_E3200:
651 case MODEL_WRT160Nv3:
652 case MODEL_L600N:
653 case MODEL_DIR620C1:
654 dirty |= check_nv("vlan2hwname", "et0");
655 break;
656 #endif
658 case MODEL_WRT54G:
659 if (strncmp(nvram_safe_get("pmon_ver"), "CFE", 3) != 0) return;
661 hardware = check_hw_type();
662 if (!nvram_get("boardtype") ||
663 !nvram_get("boardnum") ||
664 !nvram_get("boardflags") ||
665 !nvram_get("clkfreq") ||
666 !nvram_get("os_flash_addr") ||
667 !nvram_get("dl_ram_addr") ||
668 !nvram_get("os_ram_addr") ||
669 !nvram_get("scratch") ||
670 !nvram_get("et0macaddr") ||
671 ((hardware != HW_BCM4704_BCM5325F) && (!nvram_get("vlan0ports") || !nvram_get("vlan0hwname")))) {
672 _dprintf("Unable to find critical settings, erasing NVRAM\n");
673 mtd_erase("nvram");
674 goto REBOOT;
677 dirty |= check_nv("aa0", "3");
678 dirty |= check_nv("wl0gpio0", "136");
679 dirty |= check_nv("wl0gpio2", "0");
680 dirty |= check_nv("wl0gpio3", "0");
681 dirty |= check_nv("cctl", "0");
682 dirty |= check_nv("ccode", "0");
684 switch (hardware) {
685 case HW_BCM5325E:
686 /* Lower the DDR ram drive strength , the value will be stable for all boards
687 Latency 3 is more stable for all ddr 20050420 by honor */
688 dirty |= check_nv("sdram_init", "0x010b");
689 dirty |= check_nv("sdram_config", "0x0062");
690 if (!nvram_match("debug_clkfix", "0")) {
691 dirty |= check_nv("clkfreq", "216");
693 if (dirty) {
694 nvram_set("sdram_ncdl", "0x0");
696 dirty |= check_nv("pa0itssit", "62");
697 dirty |= check_nv("pa0b0", "0x15eb");
698 dirty |= check_nv("pa0b1", "0xfa82");
699 dirty |= check_nv("pa0b2", "0xfe66");
700 //dirty |= check_nv("pa0maxpwr", "0x4e");
701 break;
702 case HW_BCM5352E: // G v4, GS v3, v4
703 dirty |= check_nv("sdram_init", "0x010b");
704 dirty |= check_nv("sdram_config", "0x0062");
705 if (dirty) {
706 nvram_set("sdram_ncdl", "0x0");
708 dirty |= check_nv("pa0itssit", "62");
709 dirty |= check_nv("pa0b0", "0x168b");
710 dirty |= check_nv("pa0b1", "0xfabf");
711 dirty |= check_nv("pa0b2", "0xfeaf");
712 //dirty |= check_nv("pa0maxpwr", "0x4e");
713 break;
714 case HW_BCM5354G:
715 dirty |= check_nv("pa0itssit", "62");
716 dirty |= check_nv("pa0b0", "0x1326");
717 dirty |= check_nv("pa0b1", "0xFB51");
718 dirty |= check_nv("pa0b2", "0xFE87");
719 //dirty |= check_nv("pa0maxpwr", "0x4e");
720 break;
721 case HW_BCM4704_BCM5325F:
722 // nothing to do
723 break;
724 default:
725 dirty |= check_nv("pa0itssit", "62");
726 dirty |= check_nv("pa0b0", "0x170c");
727 dirty |= check_nv("pa0b1", "0xfa24");
728 dirty |= check_nv("pa0b2", "0xfe70");
729 //dirty |= check_nv("pa0maxpwr", "0x48");
730 break;
732 break;
734 } // switch (model)
736 dirty |= init_vlan_ports();
738 if (dirty) {
739 nvram_commit();
740 REBOOT: // do a simple reboot
741 sync();
742 reboot(RB_AUTOBOOT);
743 exit(0);
747 static int init_nvram(void)
749 unsigned long features;
750 int model;
751 const char *mfr;
752 const char *name;
753 const char *ver;
754 char s[256];
755 unsigned long bf;
756 unsigned long n;
758 model = get_model();
759 sprintf(s, "%d", model);
760 nvram_set("t_model", s);
762 mfr = "Broadcom";
763 name = NULL;
764 ver = NULL;
765 features = 0;
766 switch (model) {
767 case MODEL_WRT54G:
768 mfr = "Linksys";
769 name = "WRT54G/GS/GL";
770 switch (check_hw_type()) {
771 case HW_BCM4712:
772 nvram_set("gpio2", "adm_eecs");
773 nvram_set("gpio3", "adm_eesk");
774 nvram_unset("gpio4");
775 nvram_set("gpio5", "adm_eedi");
776 nvram_set("gpio6", "adm_rc");
777 break;
778 case HW_BCM4702:
779 nvram_unset("gpio2");
780 nvram_unset("gpio3");
781 nvram_unset("gpio4");
782 nvram_unset("gpio5");
783 nvram_unset("gpio6");
784 break;
785 case HW_BCM5352E:
786 nvram_set("opo", "0x0008");
787 nvram_set("ag0", "0x02");
788 // drop
789 default:
790 nvram_set("gpio2", "ses_led");
791 nvram_set("gpio3", "ses_led2");
792 nvram_set("gpio4", "ses_button");
793 features = SUP_SES | SUP_WHAM_LED;
794 break;
796 break;
797 case MODEL_WTR54GS:
798 mfr = "Linksys";
799 name = "WTR54GS";
800 if (!nvram_match("t_fix1", (char *)name)) {
801 nvram_set("lan_ifnames", "vlan0 eth1");
802 nvram_set("gpio2", "ses_button");
803 nvram_set("reset_gpio", "7");
805 nvram_set("pa0itssit", "62");
806 nvram_set("pa0b0", "0x1542");
807 nvram_set("pa0b1", "0xfacb");
808 nvram_set("pa0b2", "0xfec7");
809 //nvram_set("pa0maxpwr", "0x4c");
810 features = SUP_SES;
811 break;
812 case MODEL_WRTSL54GS:
813 mfr = "Linksys";
814 name = "WRTSL54GS";
815 features = SUP_SES | SUP_WHAM_LED;
816 break;
817 case MODEL_WHRG54S:
818 mfr = "Buffalo";
819 name = "WHR-G54S";
820 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
821 break;
822 case MODEL_WHRHPG54:
823 case MODEL_WZRRSG54HP:
824 case MODEL_WZRHPG54:
825 mfr = "Buffalo";
826 features = SUP_SES | SUP_AOSS_LED | SUP_HPAMP;
827 switch (model) {
828 case MODEL_WZRRSG54HP:
829 name = "WZR-RS-G54HP";
830 break;
831 case MODEL_WZRHPG54:
832 name = "WZR-HP-G54";
833 break;
834 default:
835 name = "WHR-HP-G54";
836 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_HPAMP;
837 break;
840 bf = strtoul(nvram_safe_get("boardflags"), NULL, 0);
841 switch (bf) {
842 case 0x0758:
843 case 0x1758:
844 case 0x2758:
845 case 0x3758:
846 if (nvram_match("wlx_hpamp", "")) {
847 if (nvram_get_int("wl_txpwr") > 10) nvram_set("wl_txpwr", "10");
848 nvram_set("wlx_hpamp", "1");
849 nvram_set("wlx_hperx", "0");
852 n = bf;
853 if (nvram_match("wlx_hpamp", "0")) {
854 n &= ~0x2000UL;
856 else {
857 n |= 0x2000UL;
859 if (nvram_match("wlx_hperx", "0")) {
860 n |= 0x1000UL;
862 else {
863 n &= ~0x1000UL;
865 if (bf != n) {
866 sprintf(s, "0x%lX", n);
867 nvram_set("boardflags", s);
869 break;
870 default:
871 syslog(LOG_WARNING, "Unexpected: boardflag=%lX", bf);
872 break;
874 break;
875 case MODEL_WBRG54:
876 mfr = "Buffalo";
877 name = "WBR-G54";
878 break;
879 case MODEL_WBR2G54:
880 mfr = "Buffalo";
881 name = "WBR2-G54";
882 features = SUP_SES | SUP_AOSS_LED;
883 break;
884 case MODEL_WHR2A54G54:
885 mfr = "Buffalo";
886 name = "WHR2-A54G54";
887 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
888 break;
889 case MODEL_WHR3AG54:
890 mfr = "Buffalo";
891 name = "WHR3-AG54";
892 features = SUP_SES | SUP_AOSS_LED;
893 break;
894 case MODEL_WZRG54:
895 mfr = "Buffalo";
896 name = "WZR-G54";
897 features = SUP_SES | SUP_AOSS_LED;
898 break;
899 case MODEL_WZRRSG54:
900 mfr = "Buffalo";
901 name = "WZR-RS-G54";
902 features = SUP_SES | SUP_AOSS_LED;
903 break;
904 case MODEL_WVRG54NF:
905 mfr = "Buffalo";
906 name = "WVR-G54-NF";
907 features = SUP_SES;
908 break;
909 case MODEL_WZRG108:
910 mfr = "Buffalo";
911 name = "WZR-G108";
912 features = SUP_SES | SUP_AOSS_LED;
913 break;
914 case MODEL_RT390W:
915 mfr = "Fuji";
916 name = "RT390W";
917 break;
918 case MODEL_WR850GV1:
919 mfr = "Motorola";
920 name = "WR850G v1";
921 features = SUP_NONVE;
922 break;
923 case MODEL_WR850GV2:
924 mfr = "Motorola";
925 name = "WR850G v2/v3";
926 features = SUP_NONVE;
927 break;
928 case MODEL_WL500GP:
929 mfr = "Asus";
930 name = "WL-500gP";
931 features = SUP_SES;
932 #ifdef TCONFIG_USB
933 nvram_set("usb_ohci", "-1");
934 #endif
935 if (!nvram_match("t_fix1", (char *)name)) {
936 nvram_set("lan_ifnames", "vlan0 eth1 eth2 eth3"); // set to "vlan0 eth2" by DD-WRT; default: vlan0 eth1
938 break;
939 case MODEL_WL500W:
940 mfr = "Asus";
941 name = "WL-500W";
942 features = SUP_SES | SUP_80211N;
943 #ifdef TCONFIG_USB
944 nvram_set("usb_ohci", "-1");
945 #endif
946 break;
947 case MODEL_WL500GE:
948 mfr = "Asus";
949 name = "WL-550gE";
950 // features = ?
951 #ifdef TCONFIG_USB
952 nvram_set("usb_uhci", "-1");
953 #endif
954 break;
955 case MODEL_WX6615GT:
956 mfr = "SparkLAN";
957 name = "WX-6615GT";
958 // features = ?
959 break;
960 case MODEL_MN700:
961 mfr = "Microsoft";
962 name = "MN-700";
963 break;
964 case MODEL_WR100:
965 mfr = "Viewsonic";
966 name = "WR100";
967 break;
968 case MODEL_WLA2G54L:
969 mfr = "Buffalo";
970 name = "WLA2-G54L";
971 if (!nvram_match("t_fix1", (char *)name)) {
972 nvram_set("lan_ifnames", "vlan0 eth1 eth2");
973 nvram_set("wl_ifname", "eth1");
974 nvram_set("wan_ifname", "none");
976 break;
977 case MODEL_TM2300:
978 mfr = "Dell";
979 name = "TrueMobile 2300";
980 break;
988 #ifndef WL_BSS_INFO_VERSION
989 #error WL_BSS_INFO_VERSION
990 #endif
991 #if WL_BSS_INFO_VERSION >= 108
993 case MODEL_WRH54G:
994 mfr = "Linksys";
995 name = "WRH54G";
997 nvram_set("opo", "12");
998 break;
1000 case MODEL_WHRG125:
1001 mfr = "Buffalo";
1002 name = "WHR-G125";
1003 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU;
1005 nvram_set("opo", "0x0008");
1006 nvram_set("ag0", "0x0C");
1007 break;
1008 #ifdef CONFIG_BCMWL5
1009 case MODEL_L600N:
1010 mfr = "Rosewill";
1011 name = "L600N";
1012 features = SUP_SES | SUP_80211N;
1013 if (!nvram_match("t_fix1", (char *)name)) {
1014 #ifdef TCONFIG_USBAP
1015 nvram_set("wl1_hwaddr", nvram_safe_get("0:macaddr"));
1016 nvram_set("ehciirqt", "3");
1017 nvram_set("qtdc_pid", "48407");
1018 nvram_set("qtdc_vid", "2652");
1019 nvram_set("qtdc0_ep", "4");
1020 nvram_set("qtdc0_sz", "0");
1021 nvram_set("qtdc1_ep", "18");
1022 nvram_set("qtdc1_sz", "10");
1023 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1024 nvram_set("landevs", "vlan1 wl0 wl1");
1025 nvram_set("wl0_ifname", "wl0");
1026 nvram_set("wl1_ifname", "wl1");
1027 #else
1028 nvram_set("lan_ifnames", "vlan1 eth1");
1029 nvram_set("landevs", "vlan1 wl0");
1030 #endif
1031 nvram_set("wl_ifname", "eth1");
1032 nvram_set("wan_ifnameX", "vlan2");
1033 nvram_set("wandevs", "vlan2");
1035 break;
1036 case MODEL_DIR620C1:
1037 mfr = "D-Link";
1038 name = "Dir-620 C1";
1039 features = SUP_SES | SUP_80211N;
1040 if (!nvram_match("t_fix1", (char *)name)) {
1041 #ifdef TCONFIG_USBAP
1042 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1043 nvram_set("landevs", "vlan1 wl0 wl1");
1044 nvram_set("wl0_ifname", "eth1");
1045 nvram_set("wl1_ifname", "eth2");
1046 #else
1047 nvram_set("lan_ifnames", "vlan1 eth1");
1048 nvram_set("landevs", "vlan1 wl0");
1049 #endif
1050 nvram_set("wan_ifnameX", "vlan2");
1051 nvram_set("wl_ifname", "eth1");
1054 break;
1055 case MODEL_CW5358U:
1056 mfr = "Catchtech";
1057 name = "CW-5358U";
1058 features = SUP_SES | SUP_80211N;
1059 #ifdef TCONFIG_USB
1060 nvram_set("usb_uhci", "-1");
1061 #endif
1062 if (!nvram_match("t_fix1", (char *)name)) {
1063 nvram_set("lan_ifnames", "vlan0 eth1");
1064 nvram_set("wan_ifnameX", "vlan1");
1065 nvram_set("wl_ifname", "eth1");
1067 break;
1068 case MODEL_HG320:
1069 mfr = "FiberHome";
1070 name = "HG320";
1071 features = SUP_SES | SUP_80211N;
1072 #ifdef TCONFIG_USB
1073 nvram_set("usb_uhci", "-1");
1074 #endif
1075 if (!nvram_match("t_fix1", (char *)name)) {
1076 nvram_set("lan_ifnames", "vlan1 eth1");
1077 nvram_set("wan_ifname", "vlan2");
1078 nvram_set("wan_ifnames", "vlan2");
1079 nvram_set("wan_ifnameX", "vlan2");
1080 nvram_set("wl_ifname", "eth1");
1082 break;
1083 case MODEL_RG200E_CA:
1084 mfr = "ChinaNet";
1085 name = "RG200E-CA";
1086 features = SUP_SES | SUP_80211N;
1087 #ifdef TCONFIG_USB
1088 nvram_set("usb_uhci", "-1");
1089 #endif
1090 if (!nvram_match("t_fix1", (char *)name)) {
1091 nvram_set("lan_ifnames", "vlan1 eth1");
1092 nvram_set("wan_ifname", "vlan2");
1093 nvram_set("wan_ifnames", "vlan2");
1094 nvram_set("wan_ifnameX", "vlan2");
1095 nvram_set("wl_ifname", "eth1");
1097 break;
1098 case MODEL_H218N:
1099 mfr = "ZTE";
1100 name = "H218N";
1101 features = SUP_SES | SUP_80211N;
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");
1107 nvram_set("wan_ifname", "vlan2");
1108 nvram_set("wan_ifnames", "vlan2");
1109 nvram_set("wan_ifnameX", "vlan2");
1110 nvram_set("wl_ifname", "eth1");
1112 break;
1113 case MODEL_RTN10:
1114 case MODEL_RTN10P:
1115 mfr = "Asus";
1116 name = nvram_match("boardrev", "0x1153") ? "RT-N10P" : "RT-N10";
1117 features = SUP_SES | SUP_80211N;
1118 if (!nvram_match("t_fix1", (char *)name)) {
1119 nvram_set("lan_ifnames", "vlan0 eth1");
1120 nvram_set("wan_ifnameX", "vlan1");
1121 nvram_set("wl_ifname", "eth1");
1123 break;
1124 case MODEL_RTN10U:
1125 mfr = "Asus";
1126 name = "RT-N10U";
1127 features = SUP_SES | SUP_80211N;
1128 #ifdef TCONFIG_USB
1129 nvram_set("usb_uhci", "-1");
1130 #endif
1131 if (!nvram_match("t_fix1", (char *)name)) {
1132 nvram_set("lan_ifnames", "vlan0 eth1");
1133 nvram_set("wan_ifnameX", "vlan1");
1134 nvram_set("wl_ifname", "eth1");
1136 break;
1137 case MODEL_RTN12:
1138 mfr = "Asus";
1139 name = "RT-N12";
1140 features = SUP_SES | SUP_BRAU | SUP_80211N;
1141 if (!nvram_match("t_fix1", (char *)name)) {
1142 nvram_set("lan_ifnames", "vlan0 eth1");
1143 nvram_set("wan_ifnameX", "vlan1");
1144 nvram_set("wl_ifname", "eth1");
1146 break;
1147 case MODEL_RTN12B1:
1148 mfr = "Asus";
1149 name = "RT-N12 B1";
1150 features = SUP_80211N;
1151 if (!nvram_match("t_fix1", (char *)name)) {
1152 nvram_set("lan_ifnames", "vlan0 eth1");
1153 nvram_set("wan_ifnameX", "vlan1");
1154 nvram_set("wl_ifname", "eth1");
1156 break;
1157 case MODEL_RTN15U:
1158 mfr = "Asus";
1159 name = "RT-N15U";
1160 features = SUP_SES | SUP_80211N | SUP_1000ET;
1161 #ifdef TCONFIG_USB
1162 nvram_set("usb_uhci", "-1");
1163 #endif
1164 if (!nvram_match("t_fix1", (char *)name)) {
1165 nvram_set("lan_ifnames", "vlan1 eth1");
1166 nvram_set("wan_iface", "vlan2");
1167 nvram_set("wan_ifname", "vlan2");
1168 nvram_set("wan_ifnameX", "vlan2");
1169 nvram_set("wan_ifnames", "vlan2");
1170 nvram_set("wl_ifname", "eth1");
1172 break;
1173 case MODEL_RTN16:
1174 mfr = "Asus";
1175 name = "RT-N16";
1176 features = SUP_SES | SUP_80211N | SUP_1000ET;
1177 #ifdef TCONFIG_USB
1178 nvram_set("usb_uhci", "-1");
1179 #endif
1180 if (!nvram_match("t_fix1", (char *)name)) {
1181 nvram_set("lan_ifnames", "vlan1 eth1");
1182 nvram_set("wan_ifnameX", "vlan2");
1183 nvram_set("wl_ifname", "eth1");
1184 nvram_set("vlan_enable", "1");
1186 break;
1187 case MODEL_RTN53:
1188 case MODEL_RTN53A1:
1189 mfr = "Asus";
1190 name = nvram_match("boardrev", "0x1446") ? "RT-N53 A1" : "RT-N53";
1191 features = SUP_SES | SUP_80211N;
1192 #if defined(LINUX26) && defined(TCONFIG_USBAP)
1193 if (nvram_get_int("usb_storage") == 1) nvram_set("usb_storage", "-1");
1194 #endif
1195 if (!nvram_match("t_fix1", (char *)name)) {
1196 #ifdef TCONFIG_USBAP
1197 nvram_set("wl1_hwaddr", nvram_safe_get("0:macaddr"));
1198 nvram_set("ehciirqt", "3");
1199 nvram_set("qtdc_pid", "48407");
1200 nvram_set("qtdc_vid", "2652");
1201 nvram_set("qtdc0_ep", "4");
1202 nvram_set("qtdc0_sz", "0");
1203 nvram_set("qtdc1_ep", "18");
1204 nvram_set("qtdc1_sz", "10");
1205 nvram_set("lan_ifnames", "vlan2 eth1 eth2");
1206 nvram_set("landevs", "vlan2 wl0 wl1");
1207 nvram_set("wl1_ifname", "eth2");
1208 #else
1209 nvram_set("lan_ifnames", "vlan2 eth1");
1210 nvram_set("landevs", "vlan2 wl0");
1211 #endif
1212 nvram_set("lan_ifname", "br0");
1213 nvram_set("wl_ifname", "eth1");
1214 nvram_set("wl0_ifname", "eth1");
1215 nvram_set("wan_ifnameX", "vlan1");
1216 nvram_set("wandevs", "vlan1");
1217 nvram_unset("vlan0ports");
1219 break;
1220 case MODEL_RTN66U:
1221 mfr = "Asus";
1222 #ifdef TCONFIG_AC66U
1223 name = "RT-AC66U";
1224 features = SUP_SES | SUP_80211N | SUP_1000ET | SUP_80211AC;
1225 #else
1226 name = "RT-N66U";
1227 features = SUP_SES | SUP_80211N | SUP_1000ET;
1228 #if defined(LINUX26) && defined(TCONFIG_MICROSD)
1229 if (nvram_get_int("usb_mmc") == -1) nvram_set("usb_mmc", "1");
1230 #endif
1231 #endif
1233 #ifdef TCONFIG_USB
1234 nvram_set("usb_uhci", "-1");
1235 #endif
1236 if (!nvram_match("t_fix1", (char *)name)) {
1237 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1238 nvram_set("wan_ifnameX", "vlan2");
1239 nvram_set("wl_ifnames", "eth1 eth2");
1240 #ifdef TCONFIG_AC66U
1241 nvram_set("wl_ifname", "eth1");
1242 nvram_set("wl0_ifname", "eth1");
1243 nvram_set("wl1_ifname", "eth2");
1244 #endif
1245 nvram_set("landevs", "vlan1 wl0 wl1");
1246 nvram_set("wandevs", "vlan2");
1247 #ifndef TCONFIG_AC66U
1248 #if defined(LINUX26) && defined(TCONFIG_USB)
1249 nvram_set("usb_noled", "1-1.4"); /* SD/MMC Card */
1250 #endif
1251 #else
1252 nvram_set("wl1_bw_cap","7");
1253 nvram_set("wl1_chanspec","36/80");
1254 nvram_set("wl0_bw_cap","3");
1255 nvram_set("wl0_chanspec","1l");
1256 nvram_set("blink_5g_interface","eth2");
1258 // fix WL mac`s
1259 strcpy(s, nvram_safe_get("et0macaddr"));
1260 inc_mac(s, +2);
1261 nvram_set("wl0_hwaddr", s);
1262 inc_mac(s, +1);
1263 nvram_set("wl1_hwaddr", s);
1265 // bcm4360ac_defaults
1266 nvram_set("pci/2/1/aa2g", "0");
1267 nvram_set("pci/2/1/aa5g", "7");
1268 nvram_set("pci/2/1/aga0", "71");
1269 nvram_set("pci/2/1/aga1", "71");
1270 nvram_set("pci/2/1/aga2", "71");
1271 nvram_set("pci/2/1/agbg0", "133");
1272 nvram_set("pci/2/1/agbg1", "133");
1273 nvram_set("pci/2/1/agbg2", "133");
1274 nvram_set("pci/2/1/antswitch", "0");
1275 nvram_set("pci/2/1/cckbw202gpo", "0");
1276 nvram_set("pci/2/1/cckbw20ul2gpo", "0");
1277 nvram_set("pci/2/1/dot11agofdmhrbw202gpo", "0");
1278 nvram_set("pci/2/1/femctrl", "3");
1279 nvram_set("pci/2/1/papdcap2g", "0");
1280 nvram_set("pci/2/1/tworangetssi2g", "0");
1281 nvram_set("pci/2/1/pdgain2g", "4");
1282 nvram_set("pci/2/1/epagain2g", "0");
1283 nvram_set("pci/2/1/tssiposslope2g", "1");
1284 nvram_set("pci/2/1/gainctrlsph", "0");
1285 nvram_set("pci/2/1/papdcap5g", "0");
1286 nvram_set("pci/2/1/tworangetssi5g", "0");
1287 nvram_set("pci/2/1/pdgain5g", "4");
1288 nvram_set("pci/2/1/epagain5g", "0");
1289 nvram_set("pci/2/1/tssiposslope5g", "1");
1290 nvram_set("pci/2/1/maxp2ga0", "76");
1291 nvram_set("pci/2/1/maxp2ga1", "76");
1292 nvram_set("pci/2/1/maxp2ga2", "76");
1293 nvram_set("pci/2/1/mcsbw202gpo", "0");
1294 nvram_set("pci/2/1/mcsbw402gpo", "0");
1295 nvram_set("pci/2/1/measpower", "0x7f");
1296 nvram_set("pci/2/1/measpower1", "0x7f");
1297 nvram_set("pci/2/1/measpower2", "0x7f");
1298 nvram_set("pci/2/1/noiselvl2ga0", "31");
1299 nvram_set("pci/2/1/noiselvl2ga1", "31");
1300 nvram_set("pci/2/1/noiselvl2ga2", "31");
1301 nvram_set("pci/2/1/noiselvl5gha0", "31");
1302 nvram_set("pci/2/1/noiselvl5gha1", "31");
1303 nvram_set("pci/2/1/noiselvl5gha2", "31");
1304 nvram_set("pci/2/1/noiselvl5gla0", "31");
1305 nvram_set("pci/2/1/noiselvl5gla1", "31");
1306 nvram_set("pci/2/1/noiselvl5gla2", "31");
1307 nvram_set("pci/2/1/noiselvl5gma0", "31");
1308 nvram_set("pci/2/1/noiselvl5gma1", "31");
1309 nvram_set("pci/2/1/noiselvl5gma2", "31");
1310 nvram_set("pci/2/1/noiselvl5gua0", "31");
1311 nvram_set("pci/2/1/noiselvl5gua1", "31");
1312 nvram_set("pci/2/1/noiselvl5gua2", "31");
1313 nvram_set("pci/2/1/ofdmlrbw202gpo", "0");
1314 nvram_set("pci/2/1/pa2ga0", "0xfe72,0x14c0,0xfac7");
1315 nvram_set("pci/2/1/pa2ga1", "0xfe80,0x1472,0xfabc");
1316 nvram_set("pci/2/1/pa2ga2", "0xfe82,0x14bf,0xfad9");
1317 nvram_set("pci/2/1/pcieingress_war", "15");
1318 nvram_set("pci/2/1/phycal_tempdelta", "255");
1319 nvram_set("pci/2/1/rawtempsense", "0x1ff");
1320 nvram_set("pci/2/1/rxchain", "7");
1321 nvram_set("pci/2/1/rxgainerr2g", "0xffff");
1322 nvram_set("pci/2/1/rxgainerr5g", "0xffff,0xffff,0xffff,0xffff");
1323 nvram_set("pci/2/1/rxgains2gelnagaina0", "0");
1324 nvram_set("pci/2/1/rxgains2gelnagaina1", "0");
1325 nvram_set("pci/2/1/rxgains2gelnagaina2", "0");
1326 nvram_set("pci/2/1/rxgains2gtrelnabypa0", "0");
1327 nvram_set("pci/2/1/rxgains2gtrelnabypa1", "0");
1328 nvram_set("pci/2/1/rxgains2gtrelnabypa2", "0");
1329 nvram_set("pci/2/1/rxgains2gtrisoa0", "0");
1330 nvram_set("pci/2/1/rxgains2gtrisoa1", "0");
1331 nvram_set("pci/2/1/rxgains2gtrisoa2", "0");
1332 nvram_set("pci/2/1/sar2g", "18");
1333 nvram_set("pci/2/1/sar5g", "15");
1334 nvram_set("pci/2/1/sromrev", "11");
1335 nvram_set("pci/2/1/subband5gver", "0x4");
1336 nvram_set("pci/2/1/tempcorrx", "0x3f");
1337 nvram_set("pci/2/1/tempoffset", "255");
1338 nvram_set("pci/2/1/temps_hysteresis", "15");
1339 nvram_set("pci/2/1/temps_period", "15");
1340 nvram_set("pci/2/1/tempsense_option", "0x3");
1341 nvram_set("pci/2/1/tempsense_slope", "0xff");
1342 nvram_set("pci/2/1/tempthresh", "255");
1343 nvram_set("pci/2/1/txchain", "7");
1344 nvram_set("pci/2/1/ledbh0", "2");
1345 nvram_set("pci/2/1/ledbh1", "5");
1346 nvram_set("pci/2/1/ledbh2", "4");
1347 nvram_set("pci/2/1/ledbh3", "11");
1348 nvram_set("pci/2/1/ledbh10", "7");
1350 //force EU country for eth2
1351 nvram_set("pci/2/1/ccode", "EU");
1352 #endif // TCONFIG_AC66U
1354 break;
1355 #ifdef CONFIG_BCMWL6
1356 case MODEL_W1800R:
1357 mfr = "Tenda";
1358 name = "W1800R";
1359 features = SUP_SES | SUP_80211N | SUP_1000ET | SUP_80211AC;
1360 #ifdef TCONFIG_USB
1361 nvram_set("usb_uhci", "-1");
1362 #endif
1363 if (!nvram_match("t_fix1", (char *)name)) {
1364 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1365 nvram_set("wan_ifnameX", "vlan2");
1366 nvram_set("wl_ifnames", "eth1 eth2");
1367 nvram_set("wl_ifname", "eth1");
1368 nvram_set("wl0_ifname", "eth2");
1369 nvram_set("wl1_ifname", "eth1");
1370 nvram_set("wl0_bw_cap","7");
1371 nvram_set("wl0_chanspec","36/80");
1372 nvram_set("wl1_bw_cap","3");
1373 nvram_set("wl1_chanspec","1l");
1374 nvram_set("blink_5g_interface","eth1");
1375 //nvram_set("landevs", "vlan1 wl0 wl1");
1376 //nvram_set("wandevs", "vlan2");
1378 // fix WL mac`s
1379 strcpy(s, nvram_safe_get("et0macaddr"));
1380 nvram_set("wl0_hwaddr", nvram_safe_get("0:macaddr"));
1381 nvram_set("wl1_hwaddr", nvram_safe_get("1:macaddr"));
1383 break;
1384 #endif // CONFIG_BCMWL6
1385 case MODEL_WNR3500L:
1386 mfr = "Netgear";
1387 name = "WNR3500L/U/v2";
1388 features = SUP_SES | SUP_AOSS_LED | SUP_80211N | SUP_1000ET;
1389 if (!nvram_match("t_fix1", (char *)name)) {
1390 nvram_set("sromrev", "3");
1391 nvram_set("lan_ifnames", "vlan1 eth1");
1392 nvram_set("wan_ifnameX", "vlan2");
1393 nvram_set("wl_ifname", "eth1");
1395 break;
1396 case MODEL_WNR3500LV2:
1397 mfr = "Netgear";
1398 name = "WNR3500L v2";
1399 features = SUP_SES | SUP_AOSS_LED | SUP_80211N | SUP_1000ET;
1400 if (!nvram_match("t_fix1", (char *)name)) {
1401 nvram_set("lan_ifnames", "vlan1 eth1");
1402 nvram_set("wan_ifnameX", "vlan2");
1403 nvram_set("wl_ifname", "eth1");
1405 break;
1406 case MODEL_WNR2000v2:
1407 mfr = "Netgear";
1408 name = "WNR2000 v2";
1409 features = SUP_SES | SUP_AOSS_LED | SUP_80211N;
1410 if (!nvram_match("t_fix1", (char *)name)) {
1411 nvram_set("lan_ifnames", "vlan0 eth1");
1412 nvram_set("wan_ifnameX", "vlan1");
1413 nvram_set("wl_ifname", "eth1");
1415 break;
1416 case MODEL_F7D3301:
1417 case MODEL_F7D3302:
1418 case MODEL_F7D4301:
1419 case MODEL_F7D4302:
1420 case MODEL_F5D8235v3:
1421 mfr = "Belkin";
1422 features = SUP_SES | SUP_80211N;
1423 switch (model) {
1424 case MODEL_F7D3301:
1425 name = "Share Max N300 (F7D3301/F7D7301) v1";
1426 break;
1427 case MODEL_F7D3302:
1428 name = "Share N300 (F7D3302/F7D7302) v1";
1429 break;
1430 case MODEL_F7D4301:
1431 name = "Play Max / N600 HD (F7D4301/F7D8301) v1";
1432 break;
1433 case MODEL_F7D4302:
1434 name = "Play N600 (F7D4302/F7D8302) v1";
1435 break;
1436 case MODEL_F5D8235v3:
1437 name = "N F5D8235-4 v3";
1438 break;
1440 #ifdef TCONFIG_USB
1441 nvram_set("usb_uhci", "-1");
1442 #endif
1443 if (!nvram_match("t_fix1", (char *)name)) {
1444 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1445 nvram_set("wan_ifnameX", "vlan2");
1446 nvram_set("landevs", "vlan1 wl0 wl1");
1447 nvram_set("wandevs", "vlan2");
1449 break;
1450 case MODEL_E900:
1451 case MODEL_E1500:
1452 mfr = "Linksys";
1453 name = nvram_safe_get("boot_hw_model");
1454 ver = nvram_safe_get("boot_hw_ver");
1455 features = SUP_SES | SUP_80211N;
1456 if (!nvram_match("t_fix1", (char *)name)) {
1457 nvram_set("lan_ifnames", "vlan1 eth1");
1458 nvram_set("wan_ifnameX", "vlan2");
1459 nvram_set("wl_ifname", "eth1");
1461 break;
1462 case MODEL_E1550:
1463 mfr = "Linksys";
1464 name = nvram_safe_get("boot_hw_model");
1465 ver = nvram_safe_get("boot_hw_ver");
1466 features = SUP_SES | SUP_80211N;
1467 #ifdef TCONFIG_USB
1468 nvram_set("usb_uhci", "-1");
1469 #endif
1470 if (!nvram_match("t_fix1", (char *)name)) {
1471 nvram_set("lan_ifnames", "vlan1 eth1");
1472 nvram_set("wan_ifnameX", "vlan2");
1473 nvram_set("wl_ifname", "eth1");
1475 break;
1476 case MODEL_E2500:
1477 mfr = "Linksys";
1478 name = nvram_safe_get("boot_hw_model");
1479 ver = nvram_safe_get("boot_hw_ver");
1480 features = SUP_SES | SUP_80211N;
1481 #if defined(LINUX26) && defined(TCONFIG_USBAP)
1482 if (nvram_get_int("usb_storage") == 1) nvram_set("usb_storage", "-1");
1483 #endif
1484 if (!nvram_match("t_fix1", (char *)name)) {
1485 #ifdef TCONFIG_USBAP
1486 nvram_set("wl1_hwaddr", nvram_safe_get("0:macaddr"));
1487 nvram_set("ehciirqt", "3");
1488 nvram_set("qtdc_pid", "48407");
1489 nvram_set("qtdc_vid", "2652");
1490 nvram_set("qtdc0_ep", "4");
1491 nvram_set("qtdc0_sz", "0");
1492 nvram_set("qtdc1_ep", "18");
1493 nvram_set("qtdc1_sz", "10");
1494 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1495 nvram_set("landevs", "vlan1 wl0 wl1");
1496 nvram_set("wl0_ifname", "eth1");
1497 nvram_set("wl1_ifname", "eth2");
1498 #else
1499 nvram_set("lan_ifnames", "vlan1 eth1");
1500 nvram_set("landevs", "vlan1 wl0");
1501 #endif
1502 nvram_set("wan_ifnameX", "vlan2");
1503 nvram_set("wl_ifname", "eth1");
1506 break;
1507 case MODEL_E3200:
1508 mfr = "Linksys";
1509 name = nvram_safe_get("boot_hw_model");
1510 ver = nvram_safe_get("boot_hw_ver");
1511 features = SUP_SES | SUP_80211N | SUP_1000ET;
1512 #ifdef TCONFIG_USB
1513 nvram_set("usb_uhci", "-1");
1514 #endif
1515 if (!nvram_match("t_fix1", (char *)name)) {
1516 #ifdef TCONFIG_USBAP
1517 nvram_set("wl1_hwaddr", nvram_safe_get("usb/0xBD17/macaddr"));
1518 nvram_set("ehciirqt", "3");
1519 nvram_set("qtdc_pid", "48407");
1520 nvram_set("qtdc_vid", "2652");
1521 nvram_set("qtdc0_ep", "4");
1522 nvram_set("qtdc0_sz", "0");
1523 nvram_set("qtdc1_ep", "18");
1524 nvram_set("qtdc1_sz", "10");
1525 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1526 nvram_set("landevs", "vlan1 wl0 wl1");
1527 nvram_set("wl0_ifname", "eth1");
1528 nvram_set("wl1_ifname", "eth2");
1529 #else
1530 nvram_set("lan_ifnames", "vlan1 eth1");
1531 nvram_set("landevs", "vlan1 wl0");
1532 #endif
1533 nvram_set("wl_ifname", "eth1");
1534 nvram_set("wan_ifnameX", "vlan2");
1536 break;
1537 case MODEL_E1000v2:
1538 case MODEL_WRT160Nv3:
1539 // same as M10, M20, WRT310Nv2, E1000v1
1540 mfr = "Linksys";
1541 name = nvram_safe_get("boot_hw_model");
1542 ver = nvram_safe_get("boot_hw_ver");
1543 if (nvram_match("boot_hw_model", "E100")){
1544 name = "E1000";
1546 if (nvram_match("boot_hw_model", "M10") || nvram_match("boot_hw_model", "M20")){
1547 mfr = "Cisco";
1549 features = SUP_SES | SUP_80211N | SUP_WHAM_LED;
1550 if (!nvram_match("t_fix1", (char *)name)) {
1551 nvram_set("lan_ifnames", "vlan1 eth1");
1552 nvram_set("wan_ifnameX", "vlan2");
1553 nvram_set("wl_ifname", "eth1");
1555 break;
1556 case MODEL_WRT320N:
1557 mfr = "Linksys";
1558 name = nvram_match("boardrev", "0x1307") ? "E2000" : "WRT320N";
1559 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1560 if (!nvram_match("t_fix1", (char *)name)) {
1561 nvram_set("lan_ifnames", "vlan1 eth1");
1562 nvram_set("wan_ifnameX", "vlan2");
1563 nvram_set("wl_ifname", "eth1");
1565 break;
1566 case MODEL_WRT610Nv2:
1567 mfr = "Linksys";
1568 name = nvram_match("boot_hw_model", "E300") ? "E3000" : "WRT610N v2";
1569 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1570 #ifdef TCONFIG_USB
1571 nvram_set("usb_uhci", "-1");
1572 #endif
1573 if (!nvram_match("t_fix1", (char *)name)) {
1574 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1575 nvram_set("wan_ifnameX", "vlan2");
1576 nvram_set("wl_ifname", "eth1");
1578 break;
1579 case MODEL_E4200:
1580 mfr = "Linksys";
1581 name = "E4200 v1";
1582 features = SUP_SES | SUP_80211N | SUP_1000ET;
1583 #ifdef TCONFIG_USB
1584 nvram_set("usb_uhci", "-1");
1585 #endif
1586 if (!nvram_match("t_fix1", (char *)name)) {
1587 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1588 nvram_set("wan_ifnameX", "vlan2");
1589 nvram_set("wl_ifname", "eth1");
1591 break;
1592 case MODEL_WNDR4000:
1593 mfr = "Netgear";
1594 name = "WNDR4000";
1595 features = SUP_SES | SUP_80211N | SUP_1000ET;
1596 #ifdef TCONFIG_USB
1597 nvram_set("usb_uhci", "-1");
1598 #endif
1599 if (!nvram_match("t_fix1", (char *)name)) {
1600 nvram_set("lan_ifnames", "vlan1 eth1 eth2");
1601 nvram_set("wan_ifnameX", "vlan2");
1602 nvram_set("wl_ifname", "eth1");
1604 break;
1605 #endif // CONFIG_BCMWL5
1607 case MODEL_WL330GE:
1608 mfr = "Asus";
1609 name = "WL-330gE";
1610 // The 330gE has only one wired port which can act either as WAN or LAN.
1611 // Failsafe mode is to have it start as a LAN port so you can get an IP
1612 // address via DHCP and access the router config page.
1613 if (!nvram_match("t_fix1", (char *)name)) {
1614 nvram_set("wl_ifname", "eth1");
1615 nvram_set("lan_ifnames", "eth1");
1616 nvram_set("wan_ifnameX", "eth0");
1617 nvram_set("wan_islan", "1");
1618 nvram_set("wan_proto", "disabled");
1620 break;
1621 case MODEL_WL500GPv2:
1622 mfr = "Asus";
1623 name = "WL-500gP v2";
1624 features = SUP_SES;
1625 #ifdef TCONFIG_USB
1626 nvram_set("usb_uhci", "-1");
1627 #endif
1628 break;
1629 case MODEL_WL520GU:
1630 mfr = "Asus";
1631 name = "WL-520GU";
1632 features = SUP_SES;
1633 #ifdef TCONFIG_USB
1634 nvram_set("usb_uhci", "-1");
1635 #endif
1636 break;
1637 case MODEL_WL500GD:
1638 mfr = "Asus";
1639 name = "WL-500g Deluxe";
1640 // features = SUP_SES;
1641 #ifdef TCONFIG_USB
1642 nvram_set("usb_ohci", "-1");
1643 #endif
1644 if (!nvram_match("t_fix1", (char *)name)) {
1645 nvram_set("wl_ifname", "eth1");
1646 nvram_set("lan_ifnames", "vlan0 eth1");
1647 nvram_set("wan_ifnameX", "vlan1");
1648 nvram_unset("wl0gpio0");
1650 break;
1651 case MODEL_DIR320:
1652 mfr = "D-Link";
1653 name = "DIR-320";
1654 features = SUP_SES;
1655 if (!nvram_match("t_fix1", (char *)name)) {
1656 nvram_set("wan_ifnameX", "vlan1");
1657 nvram_set("wl_ifname", "eth1");
1659 break;
1660 case MODEL_H618B:
1661 mfr = "ZTE";
1662 name = "ZXV10 H618B";
1663 features = SUP_SES | SUP_AOSS_LED;
1664 break;
1665 case MODEL_WL1600GL:
1666 mfr = "Ovislink";
1667 name = "WL1600GL";
1668 features = SUP_SES;
1669 break;
1670 #endif // WL_BSS_INFO_VERSION >= 108
1671 case MODEL_WZRG300N:
1672 mfr = "Buffalo";
1673 name = "WZR-G300N";
1674 features = SUP_SES | SUP_AOSS_LED | SUP_BRAU | SUP_80211N;
1675 break;
1676 case MODEL_WRT160Nv1:
1677 case MODEL_WRT300N:
1678 mfr = "Linksys";
1679 name = (model == MODEL_WRT300N) ? "WRT300N v1" : "WRT160N v1";
1680 features = SUP_SES | SUP_80211N;
1681 if (!nvram_match("t_fix1", (char *)name)) {
1682 nvram_set("wan_ifnameX", "eth1");
1683 nvram_set("lan_ifnames", "eth0 eth2");
1685 break;
1686 case MODEL_WRT310Nv1:
1687 mfr = "Linksys";
1688 name = "WRT310N v1";
1689 features = SUP_SES | SUP_80211N | SUP_WHAM_LED | SUP_1000ET;
1690 if (!nvram_match("t_fix1", (char *)name)) {
1691 nvram_set("lan_ifnames", "vlan1 eth1");
1692 nvram_set("wan_ifnameX", "vlan2");
1693 nvram_set("wl_ifname", "eth1");
1695 break;
1698 if (name) {
1699 nvram_set("t_fix1", name);
1700 if (ver && strcmp(ver, "")) {
1701 sprintf(s, "%s %s v%s", mfr, name, ver);
1702 } else {
1703 sprintf(s, "%s %s", mfr, name);
1706 else {
1707 snprintf(s, sizeof(s), "%s %d/%s/%s/%s/%s", mfr, check_hw_type(),
1708 nvram_safe_get("boardtype"), nvram_safe_get("boardnum"), nvram_safe_get("boardrev"), nvram_safe_get("boardflags"));
1709 s[64] = 0;
1711 nvram_set("t_model_name", s);
1713 nvram_set("pa0maxpwr", "400"); // allow Tx power up tp 400 mW, needed for ND only
1715 sprintf(s, "0x%lX", features);
1716 nvram_set("t_features", s);
1719 note: set wan_ifnameX if wan_ifname needs to be overriden
1722 if (nvram_is_empty("wan_ifnameX")) {
1723 #if 1
1724 nvram_set("wan_ifnameX", ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
1725 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1");
1726 #else
1727 p = nvram_safe_get("wan_ifname");
1728 if ((*p == 0) || (nvram_match("wl_ifname", p))) {
1729 p = ((strtoul(nvram_safe_get("boardflags"), NULL, 0) & BFL_ENETVLAN) ||
1730 (check_hw_type() == HW_BCM4712)) ? "vlan1" : "eth1";
1732 nvram_set("wan_ifnameX", p);
1733 #endif
1736 //!!TB - do not force country code here to allow nvram override
1737 //nvram_set("wl_country", "JP");
1738 //nvram_set("wl_country_code", "JP");
1739 nvram_set("wan_get_dns", "");
1740 nvram_set("wan_get_domain", "");
1741 nvram_set("ppp_get_ip", "");
1742 nvram_set("action_service", "");
1743 nvram_set("jffs2_format", "0");
1744 nvram_set("rrules_radio", "-1");
1745 nvram_unset("https_crt_gen");
1746 nvram_unset("log_wmclear");
1747 #ifdef TCONFIG_IPV6
1748 nvram_set("ipv6_get_dns", "");
1749 #endif
1750 #ifdef TCONFIG_MEDIA_SERVER
1751 nvram_unset("ms_rescan");
1752 #endif
1753 if (nvram_get_int("http_id_gen") == 1) nvram_unset("http_id");
1755 nvram_unset("sch_rboot_last");
1756 nvram_unset("sch_rcon_last");
1757 nvram_unset("sch_c1_last");
1758 nvram_unset("sch_c2_last");
1759 nvram_unset("sch_c3_last");
1761 nvram_set("brau_state", "");
1762 if ((features & SUP_BRAU) == 0) nvram_set("script_brau", "");
1763 if ((features & SUP_SES) == 0) nvram_set("sesx_script", "");
1765 if ((features & SUP_1000ET) == 0) nvram_set("jumbo_frame_enable", "0");
1767 // compatibility with old versions
1768 if (nvram_match("wl_net_mode", "disabled")) {
1769 nvram_set("wl_radio", "0");
1770 nvram_set("wl_net_mode", "mixed");
1773 return 0;
1776 /* Get the special files from nvram and copy them to disc.
1777 * These were files saved with "nvram setfile2nvram <filename>".
1778 * Better hope that they were saved with full pathname.
1780 static void load_files_from_nvram(void)
1782 char *name, *cp;
1783 int ar_loaded = 0;
1784 char buf[NVRAM_SPACE];
1786 if (nvram_getall(buf, sizeof(buf)) != 0)
1787 return;
1789 for (name = buf; *name; name += strlen(name) + 1) {
1790 if (strncmp(name, "FILE:", 5) == 0) { /* This special name marks a file to get. */
1791 if ((cp = strchr(name, '=')) == NULL)
1792 continue;
1793 *cp = 0;
1794 syslog(LOG_INFO, "Loading file '%s' from nvram", name + 5);
1795 nvram_nvram2file(name, name + 5);
1796 if (memcmp(".autorun", cp - 8, 9) == 0)
1797 ++ar_loaded;
1800 /* Start any autorun files that may have been loaded into one of the standard places. */
1801 if (ar_loaded != 0)
1802 run_nvscript(".autorun", NULL, 3);
1805 #if defined(LINUX26) && defined(TCONFIG_USB)
1806 static inline void tune_min_free_kbytes(void)
1808 struct sysinfo info;
1810 memset(&info, 0, sizeof(struct sysinfo));
1811 sysinfo(&info);
1812 if (info.totalram >= 55 * 1024 * 1024) {
1813 // If we have 64MB+ RAM, tune min_free_kbytes
1814 // to reduce page allocation failure errors.
1815 f_write_string("/proc/sys/vm/min_free_kbytes", "8192", 0, 0);
1818 #endif
1820 static void sysinit(void)
1822 static int noconsole = 0;
1823 static const time_t tm = 0;
1824 int hardware;
1825 int i;
1826 DIR *d;
1827 struct dirent *de;
1828 char s[256];
1829 char t[256];
1831 mount("proc", "/proc", "proc", 0, NULL);
1832 mount("tmpfs", "/tmp", "tmpfs", 0, NULL);
1834 #ifdef LINUX26
1835 mount("devfs", "/dev", "tmpfs", MS_MGC_VAL | MS_NOATIME, NULL);
1836 mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3));
1837 mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1));
1838 mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
1839 mkdir("/dev/shm", 0777);
1840 mkdir("/dev/pts", 0777);
1841 mknod("/dev/pts/ptmx", S_IRWXU|S_IFCHR, makedev(5, 2));
1842 mknod("/dev/pts/0", S_IRWXU|S_IFCHR, makedev(136, 0));
1843 mknod("/dev/pts/1", S_IRWXU|S_IFCHR, makedev(136, 1));
1844 mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
1845 #endif
1847 if (console_init()) noconsole = 1;
1849 stime(&tm);
1851 static const char *mkd[] = {
1852 "/tmp/etc", "/tmp/var", "/tmp/home", "/tmp/mnt",
1853 "/tmp/splashd", //!!Victek
1854 "/tmp/share", "/var/webmon", // !!TB
1855 "/var/log", "/var/run", "/var/tmp", "/var/lib", "/var/lib/misc",
1856 "/var/spool", "/var/spool/cron", "/var/spool/cron/crontabs",
1857 "/tmp/var/wwwext", "/tmp/var/wwwext/cgi-bin", // !!TB - CGI support
1858 NULL
1860 umask(0);
1861 for (i = 0; mkd[i]; ++i) {
1862 mkdir(mkd[i], 0755);
1864 mkdir("/var/lock", 0777);
1865 mkdir("/var/tmp/dhcp", 0777);
1866 mkdir("/home/root", 0700);
1867 chmod("/tmp", 0777);
1868 f_write("/etc/hosts", NULL, 0, 0, 0644); // blank
1869 f_write("/etc/fstab", NULL, 0, 0, 0644); // !!TB - blank
1870 simple_unlock("cron");
1871 simple_unlock("firewall");
1872 simple_unlock("restrictions");
1873 umask(022);
1875 if ((d = opendir("/rom/etc")) != NULL) {
1876 while ((de = readdir(d)) != NULL) {
1877 if (de->d_name[0] == '.') continue;
1878 snprintf(s, sizeof(s), "%s/%s", "/rom/etc", de->d_name);
1879 snprintf(t, sizeof(t), "%s/%s", "/etc", de->d_name);
1880 symlink(s, t);
1882 closedir(d);
1884 symlink("/proc/mounts", "/etc/mtab");
1886 #ifdef TCONFIG_SAMBASRV
1887 if ((d = opendir("/usr/codepages")) != NULL) {
1888 while ((de = readdir(d)) != NULL) {
1889 if (de->d_name[0] == '.') continue;
1890 snprintf(s, sizeof(s), "/usr/codepages/%s", de->d_name);
1891 snprintf(t, sizeof(t), "/usr/share/%s", de->d_name);
1892 symlink(s, t);
1894 closedir(d);
1896 #endif
1898 #ifdef LINUX26
1899 eval("hotplug2", "--coldplug");
1900 start_hotplug2();
1902 static const char *dn[] = {
1903 "null", "zero", "random", "urandom", "full", "ptmx", "nvram",
1904 NULL
1906 for (i = 0; dn[i]; ++i) {
1907 snprintf(s, sizeof(s), "/dev/%s", dn[i]);
1908 chmod(s, 0666);
1910 chmod("/dev/gpio", 0660);
1911 #endif
1913 set_action(ACT_IDLE);
1915 for (i = 0; defenv[i]; ++i) {
1916 putenv(defenv[i]);
1919 if (!noconsole) {
1920 printf("\n\nHit ENTER for console...\n\n");
1921 run_shell(1, 0);
1924 check_bootnv();
1926 #ifdef TCONFIG_IPV6
1927 // disable IPv6 by default on all interfaces
1928 f_write_string("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1", 0, 0);
1929 #endif
1931 for (i = 0; i < sizeof(fatalsigs) / sizeof(fatalsigs[0]); i++) {
1932 signal(fatalsigs[i], handle_fatalsigs);
1934 signal(SIGCHLD, handle_reap);
1936 #ifdef CONFIG_BCMWL5
1937 // ctf must be loaded prior to any other modules
1938 if (nvram_invmatch("ctf_disable", "1"))
1939 modprobe("ctf");
1940 #endif
1942 #ifdef TCONFIG_EMF
1943 modprobe("emf");
1944 modprobe("igs");
1945 #endif
1947 switch (hardware = check_hw_type()) {
1948 case HW_BCM4785:
1949 modprobe("bcm57xx");
1950 break;
1951 default:
1952 modprobe("et");
1953 break;
1956 load_wl();
1958 config_loopback();
1960 eval("nvram", "defaults", "--initcheck");
1961 init_nvram();
1963 // set the packet size
1964 if (nvram_get_int("jumbo_frame_enable")) {
1965 // only set the size here - 'enable' flag is set by the driver
1966 // eval("et", "robowr", "0x40", "0x01", "0x1F"); // (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)
1967 eval("et", "robowr", "0x40", "0x05", nvram_safe_get("jumbo_frame_size"));
1970 klogctl(8, NULL, nvram_get_int("console_loglevel"));
1972 #if defined(LINUX26) && defined(TCONFIG_USB)
1973 tune_min_free_kbytes();
1974 #endif
1975 setup_conntrack();
1976 set_host_domain_name();
1978 set_tz();
1980 eval("buttons");
1982 #ifdef CONFIG_BCMWL6
1983 eval("blink_5g");
1984 #endif
1986 if (!noconsole) xstart("console");
1988 i = nvram_get_int("sesx_led");
1989 led(LED_AMBER, (i & 1) != 0);
1990 led(LED_WHITE, (i & 2) != 0);
1991 led(LED_AOSS, (i & 4) != 0);
1992 led(LED_BRIDGE, (i & 8) != 0);
1993 led(LED_DIAG, 1);
1996 int init_main(int argc, char *argv[])
1998 int state, i;
1999 sigset_t sigset;
2001 // AB - failsafe?
2002 nvram_unset("debug_rc_svc");
2004 sysinit();
2006 sigemptyset(&sigset);
2007 for (i = 0; i < sizeof(initsigs) / sizeof(initsigs[0]); i++) {
2008 sigaddset(&sigset, initsigs[i]);
2010 sigprocmask(SIG_BLOCK, &sigset, NULL);
2012 #if defined(DEBUG_NOISY)
2013 nvram_set("debug_logeval", "1");
2014 nvram_set("debug_cprintf", "1");
2015 nvram_set("debug_cprintf_file", "1");
2016 nvram_set("debug_ddns", "1");
2017 #endif
2019 start_jffs2();
2021 state = SIGUSR2; /* START */
2023 for (;;) {
2024 TRACE_PT("main loop signal/state=%d\n", state);
2026 switch (state) {
2027 case SIGUSR1: /* USER1: service handler */
2028 exec_service();
2029 break;
2031 case SIGHUP: /* RESTART */
2032 case SIGINT: /* STOP */
2033 case SIGQUIT: /* HALT */
2034 case SIGTERM: /* REBOOT */
2035 led(LED_DIAG, 1);
2036 unlink("/var/notice/sysup");
2038 if( nvram_match( "webmon_bkp", "1" ) )
2039 xstart( "/usr/sbin/webmon_bkp", "hourly" ); // make a copy before halt/reboot router
2041 run_nvscript("script_shut", NULL, 10);
2043 stop_services();
2044 stop_wan();
2045 stop_lan();
2046 stop_vlan();
2047 stop_syslog();
2049 if ((state == SIGTERM /* REBOOT */) ||
2050 (state == SIGQUIT /* HALT */)) {
2051 remove_storage_main(1);
2052 stop_usb();
2054 shutdn(state == SIGTERM /* REBOOT */);
2055 exit(0);
2057 if (state == SIGINT /* STOP */) {
2058 break;
2061 // SIGHUP (RESTART) falls through
2063 case SIGUSR2: /* START */
2064 SET_LED(RELEASE_WAN_CONTROL);
2065 start_syslog();
2067 load_files_from_nvram();
2069 int fd = -1;
2070 fd = file_lock("usb"); // hold off automount processing
2071 start_usb();
2073 xstart("/usr/sbin/mymotd", "init");
2074 run_nvscript("script_init", NULL, 2);
2076 file_unlock(fd); // allow to process usb hotplug events
2077 #ifdef TCONFIG_USB
2079 * On RESTART some partitions can stay mounted if they are busy at the moment.
2080 * In that case USB drivers won't unload, and hotplug won't kick off again to
2081 * remount those drives that actually got unmounted. Make sure to remount ALL
2082 * partitions here by simulating hotplug event.
2084 if (state == SIGHUP /* RESTART */)
2085 add_remove_usbhost("-1", 1);
2086 #endif
2088 create_passwd();
2089 start_vlan();
2090 start_lan();
2091 start_arpbind();
2092 start_wan(BOOT);
2093 start_services();
2094 start_wl();
2096 #ifdef CONFIG_BCMWL5
2097 if (wds_enable()) {
2098 /* Restart NAS one more time - for some reason without
2099 * this the new driver doesn't always bring WDS up.
2101 stop_nas();
2102 start_nas();
2104 #endif
2106 syslog(LOG_INFO, "%s: Tomato %s", nvram_safe_get("t_model_name"), tomato_version);
2108 // System is up, set LEDs. Set “WHITE” LED to GREEN (power LED). Note that AMBER / DIAG = amber for power LED (across all HW?).
2109 led(LED_DIAG, LED_OFF);
2110 led(LED_WHITE, LED_ON);
2111 led(LED_AOSS, LED_ON);
2112 notice_set("sysup", "");
2113 break;
2116 chld_reap(0); /* Periodically reap zombies. */
2117 check_services();
2118 sigwait(&sigset, &state);
2121 return 0;
2124 int reboothalt_main(int argc, char *argv[])
2126 int reboot = (strstr(argv[0], "reboot") != NULL);
2127 puts(reboot ? "Rebooting..." : "Shutting down...");
2128 fflush(stdout);
2129 sleep(1);
2130 kill(1, reboot ? SIGTERM : SIGQUIT);
2132 /* In the case we're hung, we'll get stuck and never actually reboot.
2133 * The only way out is to pull power.
2134 * So after 'reset_wait' seconds (default: 20), forcibly crash & restart.
2136 if (fork() == 0) {
2137 int wait = nvram_get_int("reset_wait") ? : 20;
2138 if ((wait < 10) || (wait > 120)) wait = 10;
2140 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
2141 sleep(wait);
2142 puts("Still running... Doing machine reset.");
2143 fflush(stdout);
2144 f_write("/proc/sysrq-trigger", "s", 1, 0 , 0); /* sync disks */
2145 sleep(1);
2146 f_write("/proc/sysrq-trigger", "b", 1, 0 , 0); /* machine reset */
2149 return 0;