1/*
2
3	Tomato Firmware
4	Copyright (C) 2006-2009 Jonathan Zarate
5
6*/
7
8#include "rc.h"
9#include "interface.h"
10#include <sys/time.h>
11
12#ifdef RTCONFIG_RALINK
13#include <ralink.h>
14#endif
15
16#ifdef RTCONFIG_QCA
17#include <qca.h>
18#endif
19
20#ifdef DEBUG_RCTEST
21int test_mknode(int id);
22// used for various testing
23static int rctest_main(int argc, char *argv[])
24{
25	int on;
26
27	if (argc < 2) {
28		_dprintf("test what?\n");
29	}
30	else if (strcmp(argv[1], "rc_service")==0) {
31		notify_rc(argv[2]);
32	}
33#ifdef RTCONFIG_BCM_7114
34	else if (strcmp(argv[1], "spect")==0) {
35		start_dfs();
36	}
37#endif
38	else if(strcmp(argv[1], "get_phy_status")==0) {
39		int mask;
40		mask = atoi(argv[2]);
41		TRACE_PT("debug for phy_status %x\n", get_phy_status(mask));
42	}
43	else if(strcmp(argv[1], "get_phy_speed")==0) {
44		int mask;
45		mask = atoi(argv[2]);
46		TRACE_PT("debug for phy_speed %x\n", get_phy_speed(mask));
47	}
48	else if(strcmp(argv[1], "set_phy_ctrl")==0) {
49		int mask, ctrl;
50		mask = atoi(argv[2]);
51		ctrl = atoi(argv[3]);
52		TRACE_PT("debug for phy_speed %x\n", set_phy_ctrl(mask, ctrl));
53	}
54	else if(strcmp(argv[1], "handle_notifications")==0) {
55		handle_notifications();
56	}
57	else if(strcmp(argv[1], "check_action")==0) {
58		_dprintf("check: %d\n", check_action());
59	}
60	else if(strcmp(argv[1], "nvramhex")==0) {
61		int i;
62		char *nv;
63
64		nv = nvram_safe_get(argv[2]);
65
66		_dprintf("nvram %s(%d): ", nv, strlen(nv));
67		for(i=0;i<strlen(nv);i++) {
68			_dprintf(" %x", (unsigned char)*(nv+i));
69		}
70		_dprintf("\n");
71	}
72	else {
73		on = atoi(argv[2]);
74		_dprintf("%s %d\n", argv[1], on);
75
76		if (strcmp(argv[1], "vlan") == 0)
77		{
78			if(on) start_vlan();
79			else stop_vlan();
80		}
81		else if (strcmp(argv[1], "lan") == 0) {
82			if(on) start_lan();
83			else stop_lan();
84		}
85		else if (strcmp(argv[1], "wl") == 0) {
86			if(on)
87			{
88				start_wl();
89				lanaccess_wl();
90			}
91		}
92		else if (strcmp(argv[1], "wan") == 0) {
93			if(on) start_wan();
94			else stop_wan();
95		}
96		else if (strcmp(argv[1], "wan_port") == 0) {
97			if(on) start_wan_port();
98			else stop_wan_port();
99		}
100		else if (strcmp(argv[1], "firewall") == 0) {
101			//if(on) start_firewall();
102			//else stop_firewall();
103		}
104		else if (strcmp(argv[1], "watchdog") == 0) {
105			if(on) start_watchdog();
106			else stop_watchdog();
107		}
108#if ! (defined(RTCONFIG_QCA) || defined(RTCONFIG_RALINK))
109		else if (strcmp(argv[1], "watchdog02") == 0) {
110			if(on) start_watchdog02();
111			else stop_watchdog02();
112		}
113#endif  /* ! (RTCONFIG_QCA || RTCONFIG_RALINK) */
114		else if (strcmp(argv[1], "sw_devled") == 0) {
115			if(on) start_sw_devled();
116			else stop_sw_devled();
117		}
118#ifdef RTCONFIG_FANCTRL
119		else if (strcmp(argv[1], "phy_tempsense") == 0) {
120			if(on) start_phy_tempsense();
121			else stop_phy_tempsense();
122		}
123#endif
124#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
125		else if (strcmp(argv[1], "psta_monitor") == 0) {
126			if(on) start_psta_monitor();
127			else stop_psta_monitor();
128		}
129#endif
130#ifdef RTCONFIG_IPERF
131		else if (strcmp(argv[1], "monitor") == 0) {
132			if(on) start_monitor();
133			else stop_monitor();
134		}
135#endif
136		else if (strcmp(argv[1], "qos") == 0) {//qos test
137			if(on){
138#ifdef RTCONFIG_RALINK
139				if (module_loaded("hw_nat"))
140				{
141					modprobe_r("hw_nat");
142					sleep(1);
143#if 0
144					f_write_string("/proc/sys/net/ipv4/conf/default/force_igmp_version", "0", 0, 0);
145					f_write_string("/proc/sys/net/ipv4/conf/all/force_igmp_version", "0", 0, 0);
146#endif
147				}
148#endif
149			add_iQosRules(get_wan_ifname(wan_primary_ifunit()));
150#ifdef RTCONFIG_BWDPI
151				if(nvram_get_int("qos_type") == 1)
152					start_dpi_engine_service();
153				else
154#endif
155				start_iQos();
156			}
157			else
158			{
159#ifdef RTCONFIG_RALINK
160				if (nvram_get_int("hwnat") &&
161					/* TODO: consider RTCONFIG_DUALWAN case */
162//					!nvram_match("wan0_proto", "l2tp") &&
163//					!nvram_match("wan0_proto", "pptp") &&
164//					!(nvram_get_int("fw_pt_l2tp") || nvram_get_int("fw_pt_ipsec") &&
165//					(nvram_match("wl0_radio", "0") || nvram_get_int("wl0_mrate_x")) &&
166//					(nvram_match("wl1_radio", "0") || nvram_get_int("wl1_mrate_x")) &&
167					!module_loaded("hw_nat"))
168				{
169#if 0
170					f_write_string("/proc/sys/net/ipv4/conf/default/force_igmp_version", "2", 0, 0);
171					f_write_string("/proc/sys/net/ipv4/conf/all/force_igmp_version", "2", 0, 0);
172#endif
173
174#if defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTAC54U) || defined(RTN56UB2)
175					if (!(!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", "")))
176#endif
177					{
178						modprobe("hw_nat");
179						sleep(1);
180					}
181				}
182#endif
183#ifdef RTCONFIG_BWDPI
184				if(nvram_get_int("qos_type") == 1){
185					stop_dpi_engine_service(1);
186				}
187				else
188#endif
189				stop_iQos();
190				del_iQosRules();
191			}
192		}
193#ifdef RTCONFIG_WEBDAV
194		else if (strcmp(argv[1], "webdav") == 0) {
195			if(on)
196				start_webdav();
197		}
198#endif
199#ifdef RTCONFIG_TUNNEL
200		else if (strcmp(argv[1], "mastiff") == 0) {
201			if(on)
202				start_mastiff();
203		}
204#endif
205		else if (strcmp(argv[1], "gpiow") == 0) {
206			if(argc>=4) set_gpio(atoi(argv[2]), atoi(argv[3]));
207		}
208		else if (strcmp(argv[1], "gpior") == 0) {
209			printf("%d\n", get_gpio(atoi(argv[2])));
210		}
211		else if (strcmp(argv[1], "gpiod") == 0) {
212			if(argc>=4) gpio_dir(atoi(argv[2]), atoi(argv[3]));
213		}
214		else if (strcmp(argv[1], "init_switch") == 0) {
215			init_switch();
216		}
217		else if (strcmp(argv[1], "set_action") == 0) {
218			set_action(on);
219		}
220		else if (strcmp(argv[1], "pwr_usb") == 0) {
221			set_pwr_usb(atoi(argv[2]));
222			_dprintf("done.\n");
223		}
224		else if (strcmp(argv[1], "enc_chk") == 0) {
225        		unsigned char enc_buf[ENC_WORDS_LEN];
226        		unsigned char dec_buf[DATA_WORDS_LEN + 1];
227
228			_dprintf("get enc str:[%s]\n", enc_str(argv[2], (char *) enc_buf));
229			_dprintf("get dec str:[%s]\n", dec_str((char *) enc_buf, (char *) dec_buf));
230
231			_dprintf("done(%d)\n", strcmp(argv[2], (const char *) dec_buf));
232		}
233#ifdef RTCONFIG_BCMFA
234		else if (strcmp(argv[1], "fa_rev") == 0) {
235			_dprintf("(%d) done.\n", get_fa_rev());
236		}
237		else if (strcmp(argv[1], "fa_dump") == 0) {
238			_dprintf("(%d) done.\n", get_fa_dump());
239		}
240#endif
241		else {
242			printf("what?\n");
243		}
244	}
245	return 0;
246}
247#endif
248
249
250static int hotplug_main(int argc, char *argv[])
251{
252	if (argc >= 2) {
253		if (strcmp(argv[1], "net") == 0) {
254			hotplug_net();
255		}
256#ifdef RTCONFIG_USB
257		// !!TB - USB Support
258		else if (strcmp(argv[1], "usb") == 0) {
259			hotplug_usb();
260		}
261#ifdef LINUX26
262		else if (strcmp(argv[1], "block") == 0) {
263			hotplug_usb();
264		}
265#endif
266#endif
267	}
268	return 0;
269}
270
271typedef struct {
272	const char *name;
273	int (*main)(int argc, char *argv[]);
274} applets_t;
275
276static const applets_t applets[] = {
277	{ "preinit",			init_main				},
278	{ "init",			init_main				},
279	{ "console",			console_main				},
280#ifdef DEBUG_RCTEST
281	{ "rc",				rctest_main				},
282#endif
283	{ "ip-up",			ipup_main				},
284	{ "ip-down",			ipdown_main				},
285	{ "ip-pre-up",			ippreup_main				},
286#ifdef RTCONFIG_IPV6
287	{ "ipv6-up",			ip6up_main				},
288	{ "ipv6-down",			ip6down_main				},
289#endif
290	{ "auth-fail",			authfail_main				},
291#ifdef RTCONFIG_VPNC
292	{ "vpnc-ip-up",			vpnc_ipup_main				},
293	{ "vpnc-ip-down",		vpnc_ipdown_main			},
294	{ "vpnc-ip-pre-up",		vpnc_ippreup_main			},
295	{ "vpnc-auth-fail",		vpnc_authfail_main			},
296#endif
297#ifdef RTCONFIG_EAPOL
298	{ "wpa_cli",			wpacli_main			},
299#endif
300	{ "hotplug",			hotplug_main			},
301#ifdef RTCONFIG_BCMARM
302	{ "mtd-write",			mtd_write_main_old		},
303	{ "mtd-erase",			mtd_unlock_erase_main_old	},
304	{ "mtd-unlock",			mtd_unlock_erase_main_old	},
305#else
306	{ "mtd-write",			mtd_write_main			},
307	{ "mtd-erase",			mtd_unlock_erase_main		},
308	{ "mtd-unlock",			mtd_unlock_erase_main		},
309#endif
310	{ "watchdog",			watchdog_main			},
311#if ! (defined(RTCONFIG_QCA) || defined(RTCONFIG_RALINK))
312	{ "watchdog02",			watchdog02_main			},
313#endif  /* ! (RTCONFIG_QCA || RTCONFIG_RALINK) */
314	{ "sw_devled",			sw_devled_main			},
315#ifdef RTCONFIG_FANCTRL
316	{ "phy_tempsense",		phy_tempsense_main		},
317#endif
318#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
319	{ "psta_monitor",		psta_monitor_main		},
320#endif
321#ifdef RTCONFIG_IPERF
322	{ "monitor",			monitor_main			},
323#endif
324#ifdef RTCONFIG_QTN
325	{ "qtn_monitor",		qtn_monitor_main		},
326#endif
327#ifdef RTCONFIG_USB
328	{ "usbled",			usbled_main			},
329#endif
330	{ "ddns_updated", 		ddns_updated_main		},
331	{ "radio",			radio_main			},
332	{ "ots",			ots_main			},
333	{ "udhcpc",			udhcpc_wan			},
334	{ "udhcpc_lan",			udhcpc_lan			},
335	{ "zcip",			zcip_wan			},
336#ifdef RTCONFIG_IPV6
337	{ "dhcp6c",			dhcp6c_wan			},
338#endif
339#ifdef RTCONFIG_WPS
340	{ "wpsaide",			wpsaide_main			},
341#ifdef RTCONFIG_QCA
342	{ "get_wps_er",			get_wps_er_main			},
343#endif
344#endif
345	{ "halt",			reboothalt_main			},
346	{ "reboot",			reboothalt_main			},
347	{ "ntp", 			ntp_main			},
348#if defined(RTCONFIG_RALINK) || defined(RTCONFIG_EXT_RTL8365MB) || defined(RTCONFIG_EXT_RTL8370MB)
349	{ "rtkswitch",			config_rtkswitch		},
350#elif defined(RTCONFIG_QCA)
351	{ "rtkswitch",			config_rtkswitch		},
352#endif
353	{ "delay_exec",			delay_main			},
354
355	{ "wanduck",			wanduck_main			},
356#if defined(CONFIG_BCMWL5) && defined(RTCONFIG_DUALWAN)
357	{ "dualwan",			dualwan_control			},
358#endif
359	{ "tcpcheck",			tcpcheck_main			},
360	{ "autodet", 			autodet_main			},
361#ifdef RTCONFIG_QCA_PLC_UTILS
362	{ "autodet_plc", 		autodet_plc_main		},
363#endif
364#ifdef RTCONFIG_CIFS
365	{ "mount-cifs",			mount_cifs_main			},
366#endif
367#ifdef RTCONFIG_USB
368	{ "ejusb",			ejusb_main			},
369#ifdef RTCONFIG_DISK_MONITOR
370	{ "disk_monitor",		diskmon_main			},
371#endif
372	{ "disk_remove",		diskremove_main			},
373#endif
374	{ "firmware_check",		firmware_check_main		},
375#ifdef BUILD_READMEM
376	{ "readmem",			readmem_main			},
377#endif
378#ifdef RTCONFIG_HTTPS
379	{ "rsasign_check",		rsasign_check_main		},
380#endif
381	{ "service",			service_main			},
382#ifdef RTCONFIG_SPEEDTEST
383	{ "speedtest",			speedtest_main			},
384#endif
385#ifdef RTCONFIG_BWDPI
386	{ "bwdpi",			bwdpi_main			},
387	{ "bwdpi_check",		bwdpi_check_main		},
388	{ "bwdpi_wred_alive",		bwdpi_wred_alive_main		},
389	{ "bwdpi_db_10",		bwdpi_db_10_main		},
390	{ "rsasign_sig_check",		rsasign_sig_check_main		},
391#endif
392	{ "hour_monitor",		hour_monitor_main		},
393#ifdef RTCONFIG_INTERNAL_GOBI
394	{ "lteled",			lteled_main			},
395#endif
396#ifdef RTCONFIG_TR069
397	{ "dhcpc_lease",		dhcpc_lease_main		},
398#endif
399#if ((defined(RTCONFIG_USER_LOW_RSSI) && defined(RTCONFIG_BCMARM)) || defined(RTCONFIG_NEW_USER_LOW_RSSI))
400	{ "roamast",			roam_assistant_main		},
401#endif
402#ifdef RTCONFIG_DHCP_OVERRIDE
403	{ "detectWAN_arp",		detectWAN_arp_main		},
404#endif
405#if defined(RTCONFIG_KEY_GUARD)
406	{ "keyguard",			keyguard_main			},
407#endif
408	{NULL, NULL}
409};
410
411int main(int argc, char **argv)
412{
413	char *base;
414	int f;
415
416	/*
417		Make sure std* are valid since several functions attempt to close these
418		handles. If nvram_*() runs first, nvram=0, nvram gets closed. - zzz
419	*/
420
421	if ((f = open("/dev/null", O_RDWR)) < 0) {
422	}
423	else if(f < 3) {
424		dup(f);
425		dup(f);
426	}
427	else {
428		close(f);
429	}
430
431	base = strrchr(argv[0], '/');
432	base = base ? base + 1 : argv[0];
433
434#if 0
435	if (strcmp(base, "rc") == 0) {
436		if (argc < 2) return 1;
437		if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
438		if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
439		if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
440		++argv;
441		--argc;
442		base = argv[0];
443	}
444#endif
445
446#if !defined(CONFIG_BCMWL5)
447    if(getpid() != 1)
448    {
449#endif
450
451#if defined(DEBUG_NOISY)
452	if (nvram_match("debug_logrc", "1")) {
453		int i;
454
455		cprintf("[rc %d] ", getpid());
456		for (i = 0; i < argc; ++i) {
457			cprintf("%s ", argv[i]);
458		}
459		cprintf("\n");
460
461	}
462#endif
463
464#if defined(DEBUG_NOISY)
465	if (nvram_match("debug_ovrc", "1")) {
466		char tmp[256];
467		char *a[32];
468
469		realpath(argv[0], tmp);
470		if ((strncmp(tmp, "/tmp/", 5) != 0) && (argc < 32)) {
471			sprintf(tmp, "%s%s", "/tmp/", base);
472			if (f_exists(tmp)) {
473				cprintf("[rc] override: %s\n", tmp);
474				memcpy(a, argv, argc * sizeof(a[0]));
475				a[argc] = 0;
476				a[0] = tmp;
477				execvp(tmp, a);
478				exit(0);
479			}
480		}
481	}
482#endif
483#if !defined(CONFIG_BCMWL5)
484    }
485#endif
486	const applets_t *a;
487	for (a = applets; a->name; ++a) {
488		if (strcmp(base, a->name) == 0) {
489			openlog(base, LOG_PID, LOG_USER);
490			return a->main(argc, argv);
491		}
492	}
493
494
495	if(!strcmp(base, "restart_wireless")){
496		printf("restart wireless...\n");
497		restart_wireless();
498		return 0;
499	}
500#ifdef RTCONFIG_BCM_7114
501	else if(!strcmp(base, "stop_wl")){
502                stop_wl_bcm();
503                return 0;
504        }
505#endif
506	else if(!strcmp(base, "nvram_erase")){
507		nvram_set(ASUS_STOP_COMMIT, "1");
508		erase_nvram();
509		return 0;
510	}
511#ifdef RTCONFIG_USB
512	else if(!strcmp(base, "restart_usb")){
513		int f_stop = 0;
514		if(argc == 2)
515			f_stop = atoi(argv[1]);
516		printf("%s usb...\n", f_stop ? "stop" : "restart");
517		restart_usb(f_stop);
518		return 0;
519	}
520	else if(!strcmp(base, "get_apps_name")){
521		if(argc != 2){
522			printf("Usage: get_apps_name [File name]\n");
523			return 0;
524		}
525
526		return get_apps_name(argv[1]);
527	}
528#ifdef BCM_MMC
529	else if(!strcmp(base, "asus_mmc")){
530		if(argc != 3){
531			printf("Usage: asus_mmc [device_name] [action]\n");
532			return 0;
533		}
534
535		return asus_mmc(argv[1], argv[2]);
536	}
537#endif
538	else if(!strcmp(base, "asus_sd")){
539		if(argc != 3){
540			printf("Usage: asus_sd [device_name] [action]\n");
541			return 0;
542		}
543
544		return asus_sd(argv[1], argv[2]);
545	}
546	else if(!strcmp(base, "asus_lp")){
547		if(argc != 3){
548			printf("Usage: asus_lp [device_name] [action]\n");
549			return 0;
550		}
551
552		return asus_lp(argv[1], argv[2]);
553	}
554	else if(!strcmp(base, "asus_sg")){
555		if(argc != 3){
556			printf("Usage: asus_sg [device_name] [action]\n");
557			return 0;
558		}
559
560		return asus_sg(argv[1], argv[2]);
561	}
562	else if(!strcmp(base, "asus_sr")){
563		if(argc != 3){
564			printf("Usage: asus_sr [device_name] [action]\n");
565			return 0;
566		}
567
568		return asus_sr(argv[1], argv[2]);
569	}
570	else if(!strcmp(base, "asus_tty")){
571		if(argc != 3){
572			printf("Usage: asus_tty [device_name] [action]\n");
573			return 0;
574		}
575
576		return asus_tty(argv[1], argv[2]);
577	}
578	else if(!strcmp(base, "asus_usbbcm")){
579		if(argc != 3){
580			printf("Usage: asus_usbbcm [device_name] [action]\n");
581			return 0;
582		}
583
584		return asus_usbbcm(argv[1], argv[2]);
585	}
586	else if(!strcmp(base, "asus_usb_interface")){
587		if(argc != 3){
588			printf("Usage: asus_usb_interface [device_name] [action]\n");
589			return 0;
590		}
591
592		return asus_usb_interface(argv[1], argv[2]);
593	}
594	else if (!strcmp(base, "usb_notify")) {
595#if defined(RTCONFIG_APP_PREINSTALLED) || defined(RTCONFIG_APP_NETINSTALLED)
596		usb_notify();
597#endif
598
599		return 0;
600	}
601#if defined(RTCONFIG_APP_PREINSTALLED) || defined(RTCONFIG_APP_NETINSTALLED)
602	else if(!strcmp(base, "run_app_script")){
603		if(argc != 3){
604			printf("Usage: run_app_script [Package name | allpkg] [APP action]\n");
605			return 0;
606		}
607
608		if(!strcmp(argv[1], "allpkg"))
609			return run_app_script(NULL, argv[2]);
610		else
611			return run_app_script(argv[1], argv[2]);
612	}
613	else if (!strcmp(base, "chk_app_state")) {
614#define PID_FILE "/var/run/chk_app_state.pid"
615		FILE *fp;
616		char chk_value[4];
617
618		if(f_read_string(PID_FILE, chk_value, 4) > 0
619				&& atoi(chk_value) != getpid()){
620			_dprintf("Already running!\n");
621			return 0;
622		}
623
624		if((fp = fopen(PID_FILE, "w")) == NULL){
625			_dprintf("Can't open the pid file!\n");
626			return 0;
627		}
628
629		fprintf(fp, "%d", getpid());
630		fclose(fp);
631
632		memset(chk_value, 0, 4);
633		strncpy(chk_value, nvram_safe_get("apps_state_switch"), 4);
634		if(strcmp(chk_value, "")){
635			if(atoi(chk_value) != APPS_SWITCH_FINISHED && !pids("app_switch.sh")){
636				_dprintf("Don't have the switch script.\n");
637				nvram_set("apps_state_switch", "");
638			}
639
640			unlink(PID_FILE);
641			return 0;
642		}
643
644		memset(chk_value, 0, 4);
645		strncpy(chk_value, nvram_safe_get("apps_state_install"), 4);
646		if(strcmp(chk_value, "")){
647			if(atoi(chk_value) != APPS_INSTALL_FINISHED && !pids("app_install.sh")){
648				_dprintf("Don't have the install script.\n");
649				nvram_set("apps_state_install", "");
650			}
651
652			unlink(PID_FILE);
653			return 0;
654		}
655
656		memset(chk_value, 0, 4);
657		strncpy(chk_value, nvram_safe_get("apps_state_upgrade"), 4);
658		if(strcmp(chk_value, "")){
659			if(atoi(chk_value) != APPS_UPGRADE_FINISHED && !pids("app_upgrade.sh")){
660				_dprintf("Don't have the upgrade script.\n");
661				nvram_set("apps_state_upgrade", "");
662			}
663
664			unlink(PID_FILE);
665			return 0;
666		}
667
668		memset(chk_value, 0, 4);
669		strncpy(chk_value, nvram_safe_get("apps_state_enable"), 4);
670		if(strcmp(chk_value, "")){
671			if(atoi(chk_value) != APPS_ENABLE_FINISHED && !pids("app_set_enabled.sh")){
672				_dprintf("Don't have the enable script.\n");
673				nvram_set("apps_state_enable", "");
674			}
675
676			unlink(PID_FILE);
677			return 0;
678		}
679
680		unlink(PID_FILE);
681		return 0;
682	}
683#endif
684#endif
685	else if(!strcmp(base, "ATE")) {
686		if( argc == 2 || argc == 3 || argc == 4) {
687			asus_ate_command(argv[1], argv[2], argv[3]);
688		}
689		else
690			printf("ATE_ERROR\n");
691		return 0;
692	}
693#if defined(RTCONFIG_RALINK) || defined(RTCONFIG_QCA)
694	else if (!strcmp(base, "FWRITE")) {
695		if (argc == 3)
696			return FWRITE(argv[1], argv[2]);
697		else
698		return 0;
699	}
700	else if (!strcmp(base, "FREAD")) {
701		if (argc == 3)
702		{
703			unsigned int addr;
704			int len;
705			addr = strtoul(argv[1], NULL, 16);
706			if(argv[2][0] == '0' && argv[2][1] == 'x')
707				len  = (int) strtoul(argv[2], NULL, 16);
708			else
709				len  = (int) strtoul(argv[2], NULL, 10);
710
711			if(len > 0)
712				return FREAD(addr, len);
713		}
714		printf("ATE_ERROR\n");
715		return 0;
716	}
717#endif	/* RTCONFIG_RALINK || RTCONFIG_QCA */
718#if defined(RTCONFIG_RALINK)
719#if defined(RTCONFIG_HAS_5G)
720	else if (!strcmp(base, "asuscfe_5g")) {
721		if (argc == 2)
722			return asuscfe(argv[1], WIF_5G);
723		else
724			return EINVAL;
725	}
726#endif	/* RTCONFIG_HAS_5G */
727	else if (!strcmp(base, "asuscfe_2g")) {
728		if (argc == 2)
729			return asuscfe(argv[1], WIF_2G);
730		else
731			return EINVAL;
732	}
733	else if (!strcmp(base, "stainfo_2g")) {
734		return stainfo(0);
735	}
736#if defined(RTCONFIG_HAS_5G)
737	else if (!strcmp(base, "stainfo_5g")) {
738		return stainfo(1);
739	}
740#endif	/* RTCONFIG_HAS_5G */
741#ifdef RTCONFIG_DSL
742	else if(!strcmp(base, "gen_ralink_config")){
743		if(argc != 3){
744			printf("Usage: gen_ralink_config [band] [is_iNIC]\n");
745			return 0;
746		}
747		return gen_ralink_config(atoi(argv[1]), atoi(argv[2]));
748	}
749#endif
750#endif
751
752#if defined(CONFIG_BCMWL5) || defined(RTCONFIG_RALINK) || defined(RTCONFIG_QCA)
753	else if(!strcmp(base, "set_factory_mode")) {
754		set_factory_mode();
755		return 0;
756	}
757#endif
758	else if(!strcmp(base, "run_telnetd")) {
759		_start_telnetd(1);
760		return 0;
761	}
762#ifdef RTCONFIG_SSH
763	else if(!strcmp(base, "run_sshd")) {
764		start_sshd();
765		return 0;
766	}
767#endif
768#ifdef RTCONFIG_WTFAST
769	else if(!strcmp(base, "run_wtfast")) {
770		start_wtfast();
771		return 0;
772	}
773#endif
774#if defined(RTCONFIG_PPTPD) || defined(RTCONFIG_ACCEL_PPTPD)
775	else if(!strcmp(base, "run_pptpd")) {
776		start_pptpd();
777		return 0;
778	}
779#endif
780#ifdef RTCONFIG_PARENTALCTRL
781	else if(!strcmp(base, "pc")) {
782		pc_main(argc, argv);
783		return 0;
784	}
785	else if(!strcmp(base, "pc_block")) {
786		pc_block_main(argc, argv);
787		return 0;
788	}
789#endif
790#if defined(CONFIG_BCMWL5) || (defined(RTCONFIG_RALINK) && defined(RTCONFIG_WIRELESSREPEATER)) || defined(RTCONFIG_QCA)
791	else if (!strcmp(base, "wlcscan")) {
792		return wlcscan_main();
793	}
794#ifdef RTCONFIG_QTN
795	else if (!strcmp(base, "start_psta_qtn")) {
796		return start_psta_qtn();
797	}
798	else if (!strcmp(base, "start_ap_qtn")) {
799		return start_ap_qtn();
800	}
801	else if (!strcmp(base, "start_nodfs_scan_qtn")) {
802		return start_nodfs_scan_qtn();
803	}
804	else if (!strcmp(base, "start_qtn_stateless")) {
805		return gen_stateless_conf();
806	}
807	else if (!strcmp(base, "restart_qtn")){
808		return reset_qtn(0);
809	}
810#endif
811#endif
812#if defined(CONFIG_BCMWL5) && defined(RTCONFIG_DUALWAN)
813	else if (!strcmp(base, "dualwan")){
814		dualwan_control();
815	}
816#endif
817#ifdef RTCONFIG_WIRELESSREPEATER
818	else if (!strcmp(base, "wlcconnect")) {
819		return wlcconnect_main();
820	}
821#endif
822#ifdef CONFIG_BCMWL5
823	else if (!strcmp(base, "setup_dnsmq")) {
824		if(argc != 2)
825			return 0;
826
827		return setup_dnsmq(atoi(argv[1]));
828	}
829#endif
830	else if (!strcmp(base, "add_multi_routes")) {
831		return add_multi_routes();
832	}
833	else if (!strcmp(base, "led_ctrl")) {
834		return(led_control(atoi(argv[1]), atoi(argv[2])));
835	}
836#ifdef RTCONFIG_BCMARM
837#if defined(RTAC1200G) || defined(RTAC1200GP)
838	/* mtd-erase2 [device] */
839	else if (!strcmp(base, "mtd-erase2")) {
840		if (argv[1] && ((!strcmp(argv[1], "boot")) ||
841				(!strcmp(argv[1], "linux")) ||
842				(!strcmp(argv[1], "rootfs")) ||
843				(!strcmp(argv[1], "nvram")))) {
844			return mtd_erase(argv[1]);
845		} else {
846			fprintf(stderr, "usage: mtd-erase2 [device]\n");
847			return EINVAL;
848		}
849	}
850	/* mtd-write2 [path] [device] */
851	else if (!strcmp(base, "mtd-write2")) {
852		if (argc >= 3)
853			return mtd_write(argv[1], argv[2]);
854		else {
855			fprintf(stderr, "usage: mtd-write2 [path] [device]\n");
856			return EINVAL;
857		}
858	}
859#else
860	/* mtd-erase2 [device] */
861	else if (!strcmp(base, "mtd-erase2")) {
862		if (argv[1] && ((!strcmp(argv[1], "boot")) ||
863				(!strcmp(argv[1], "linux")) ||
864				(!strcmp(argv[1], "linux2")) ||
865				(!strcmp(argv[1], "rootfs")) ||
866				(!strcmp(argv[1], "rootfs2")) ||
867				(!strcmp(argv[1], "brcmnand")) ||
868				(!strcmp(argv[1], "nvram")))) {
869			return mtd_erase(argv[1]);
870		} else {
871			fprintf(stderr, "usage: mtd-erase2 [device]\n");
872			return EINVAL;
873		}
874	}
875	/* mtd-write2 [path] [device] */
876	else if (!strcmp(base, "mtd-write2")) {
877		if (argc >= 3)
878			return mtd_write(argv[1], argv[2]);
879		else {
880			fprintf(stderr, "usage: mtd-write2 [path] [device]\n");
881			return EINVAL;
882		}
883	}
884#endif
885#endif
886	else if(!strcmp(base, "test_endian")){
887		int num = 0x04030201;
888		char c = *(char *)(&num);
889
890		if(c == 0x04 || c == 0x01){
891			if(c == 0x04)
892				printf("Big.\n");
893			else
894				printf("Little.\n");
895		}
896		else
897			printf("test error!\n");
898
899		return 0;
900	}
901	else if (!strcmp(base, "free_caches")) {
902		int c;
903		unsigned int test_num;
904		char *set_value = NULL;
905		int clean_time = 1;
906		int threshold = 0;
907
908		if(argc){
909			while((c = getopt(argc, argv, "c:w:t:")) != -1){
910				switch(c){
911					case 'c': // set the clean-cache mode: 0~3.
912						test_num = strtol(optarg, NULL, 10);
913			if(test_num == LONG_MIN || test_num == LONG_MAX){
914				_dprintf("ERROR: unknown value %s...\n", optarg);
915							return 0;
916						}
917
918						if(test_num < 0 || test_num > 3){
919							_dprintf("ERROR: the value %s was over the range...\n", optarg);
920							return 0;
921						}
922
923						set_value = optarg;
924
925						break;
926					case 'w': // set the waited time for cleaning.
927						test_num = strtol(optarg, NULL, 10);
928			if(test_num < 0 || test_num == LONG_MIN || test_num == LONG_MAX){
929				_dprintf("ERROR: unknown value %s...\n", optarg);
930							return 0;
931						}
932
933						clean_time = test_num;
934
935						break;
936					case 't': // set the waited time for cleaning.
937						test_num = strtol(optarg, NULL, 10);
938			if(test_num < 0 || test_num == LONG_MIN || test_num == LONG_MAX){
939				_dprintf("ERROR: unknown value %s...\n", optarg);
940							return 0;
941						}
942
943						threshold = test_num;
944
945						break;
946					default:
947						fprintf(stderr, "Usage: free_caches [ -c clean_mode ] [ -w clean_time ] [ -t threshold ]\n");
948						break;
949				}
950			}
951		}
952
953		if(!set_value)
954			set_value = FREE_MEM_PAGE;
955
956		free_caches(set_value, clean_time, threshold);
957
958		return 0;
959	}
960#ifdef RTCONFIG_USB_MODEM
961	else if(!strcmp(base, "write_3g_ppp_conf")){
962		return write_3g_ppp_conf();
963	}
964#if (defined(RTCONFIG_JFFS2) || defined(RTCONFIG_BRCM_NAND_JFFS2) || defined(RTCONFIG_UBIFS))
965	else if(!strcmp(base, "lplus")){
966		if(argc != 3){
967			printf("Usage: %s <integer1> <integer2>.\n", argv[0]);
968			return 0;
969		}
970
971		unsigned long long int1 = strtoull(argv[1], NULL, 10);
972		unsigned long long int2 = strtoull(argv[2], NULL, 10);
973		unsigned long long total = int1+int2;
974
975		printf("%llu", total);
976
977		return 0;
978	}
979	else if(!strcmp(base, "lminus")){
980		if(argc != 3){
981			printf("Usage: %s <integer1> <integer2>.\n", argv[0]);
982			return 0;
983		}
984
985		unsigned long long int1 = strtoull(argv[1], NULL, 10);
986		unsigned long long int2 = strtoull(argv[2], NULL, 10);
987		unsigned long long total = int1-int2;
988
989		printf("%llu", total);
990
991		return 0;
992	}
993#endif
994#endif
995#ifdef RTCONFIG_TOR
996	else if (!strcmp(base, "start_tor")) {
997		start_Tor_proxy();
998		return 0;
999	}
1000#endif
1001#if ((defined(RTCONFIG_USER_LOW_RSSI) && defined(RTCONFIG_BCMARM)) || defined(RTCONFIG_NEW_USER_LOW_RSSI))
1002	else if (!strcmp(base, "start_roamast")) {
1003		start_roamast();
1004		return 0;
1005	}
1006#endif
1007#if defined(RTCONFIG_KEY_GUARD)
1008	else if (!strcmp(base, "start_keyguard")) {
1009		start_keyguard();
1010		return 0;
1011	}
1012#endif
1013	printf("Unknown applet: %s\n", base);
1014	return 0;
1015}
1016