1#ifndef NOUSB
2#include <stdio.h>
3#include <stdlib.h>
4#include <bcmnvram.h>
5
6
7#define XSTR(s) STR(s)
8#define STR(s) #s
9
10typedef struct nvmap
11{
12	char *sid;
13	char *fid;
14	char *name;
15} nvmap;
16
17struct nvmap maps[] = {
18{ "BRIPAddress", "IPRouters", "currentip"},
19{ "general.log", "ProductID", "productid" },
20{ "General", "x_Username", "username" },
21{ "General", "x_Password", "password" },
22{ "LANHostConfig", "x_LANDHCPClient", "lan_proto"},
23{ "LANHostConfig", "IPRouters", "lan_ipaddr"},
24{ "LANHostConfig", "SubnetMask", "lan_netmask"},
25{ "LANHostConfig", "x_Gateway", "lan_gateway"},
26{ "LANHostConfig", "DHCPServerConfigurable", "dhcp_proto"},
27{ "LANHostConfig", "DomainName", "dhcp_domain"},
28{ "LANHostConfig", "MinAddress", "dhcp_start"},
29{ "LANHostConfig", "MaxAddress", "dhcp_end"},
30{ "LANHostConfig", "LeaseTime", "dhcp_leases"},
31{ "WLANConfig11b", "SSID", "wl_ssid" },
32{ "WLANConfig11b", "x_APMode", "wl_wdsmode_x"},
33{ "WLANConfig11b", "Channel", "wl_channel"},
34{ "WLANConfig11b", "x_BRApply", "wl_wdsapply_x"},
35{ "WLANConfig11b", "x_BRestrict", "wl_lazywds"},
36{ "WLANConfig11b", "RBRListCount", "wl_wds_num_x"},
37{ "WLANConfig11b", "RBRList", "wl_wdslist_x"},
38{ "WLANConfig11b", "DataRateAll", "wl_rate"},
39{ "WLANConfig11b", "x_Mode11g", "wl_gmode_x"},
40{ "WLANConfig11b", "x_GProtection", "wl_gmode_protection_x"},
41{ "WLANConfig11b", "AuthenticationMethod", "wl_authmode_x"},
42{ "WLANConfig11b", "WEPType", "wl_weptype_x"},
43{ "WLANConfig11b", "x_Phrase", "wl_wpapsk"},
44{ "WLANConfig11b", "WEPKey1", "wl_wepkey1"},
45{ "WLANConfig11b", "WEPKey2", "wl_wepkey2"},
46{ "WLANConfig11b", "WEPKey3", "wl_wepkey3"},
47{ "WLANConfig11b", "WEPKey4", "wl_wepkey4"},
48{ "WLANConfig11b", "WEPDefaultKey", "wl_wepkey"},
49{ "WLANConfig11b", "x_BlockBCSSID", "wl_closed"},
50{ "WLANConfig11b", "DataRate", "wl_rateset_x"},
51{ "WLANConfig11b", "x_Rekey", "wl_rekey"},
52{ "WLANConfig11b", "x_Frag", "wl_frag"},
53{ "WLANConfig11b", "x_RTS", "wl_rts"},
54{ "WLANConfig11b", "x_DTIM", "wl_dtim"},
55{ "WLANConfig11b", "x_Beacon", "wl_beacon"},
56{ "WLANConfig11b", "x_FrameBurst", "wl_framburst_x"},
57{ "WLANConfig11b", "x_BRhwaddr", "wl_wds"},
58
59{ "DeviceSecurity11a", "ACLMethod", "wl_macmode_x"},
60{ "DeviceSecurity11a", "ACLListCount", "wl_maclist_num_x"},
61{ "DeviceSecurity11a", "MacAddr", "wl_maclist_x"},
62{ "WLANAuthentication11a", "ExAuthDBIPAddr", "wl_radius_ipaddr"},
63{ "WLANAuthentication11a", "ExAuthDBPortNumber", "wl_radius_port"},
64{ "WLANAuthentication11a", "ExAuthDBPassword", "wl_radius_key"},
65
66
67{ "Storage", "StorageApps", "st_apps"},
68{ "Storage", "SharedCount", "sh_num"},
69{ "Storage", "SharedPath", "sh_path"},
70{ "Storage", "SharedName", "sh_name"},
71{ "Storage", "SharedEnable", "sh_rright_x"},
72{ "Storage", "WriteEnable", "sh_wright_x"},
73{ "Storage", "UserCount", "acc_num"},
74{ "Storage", "UserName", "acc_username"},
75{ "Storage", "UserPassword", "acc_password"},
76{ "Storage", "x_SharedPath", "sh_path_x"},
77{ "Storage", "x_SharedName", "sh_name_x"},
78{ "Storage", "x_SharedComment", "sh_comment_x"},
79{ "Storage", "x_SharedMaxUser", "sh_max_user_x"},
80{ "Storage", "x_AccAny", "sh_accuser_x0"},
81{ "Storage", "x_AccAny_Share", "sh_accuser_share_x0"},
82{ "Storage", "x_AccAny_Write", "sh_accuser_write_x0"},
83{ "Storage", "x_AccUser1", "sh_accuser_x1"},
84{ "Storage", "x_AccUser1_Share", "sh_accuser_share_x1"},
85{ "Storage", "x_AccUser1_Write", "sh_accuser_write_x1"},
86{ "Storage", "x_AccUser2", "sh_accuser_x2"},
87{ "Storage", "x_AccUser2_Share", "sh_accuser_share_x2"},
88{ "Storage", "x_AccUser2_Write", "sh_accuser_write_x2"},
89{ "Storage", "x_AccUser3", "sh_accuser_x3"},
90{ "Storage", "x_AccUser3_Share", "sh_accuser_share_x3"},
91{ "Storage", "x_AccUser3_Write", "sh_accuser_write_x3"},
92{ "Storage", "x_AccUser4", "sh_accuser_x4"},
93{ "Storage", "x_AccUser4_Share", "sh_accuser_share_x4"},
94{ "Storage", "x_AccUser4_Write", "sh_accuser_write_x4"},
95{ "Storage", "x_AccUser5", "sh_accuser_x5"},
96{ "Storage", "x_AccUser5_Share", "sh_accuser_share_x5"},
97{ "Storage", "x_AccUser5_Write", "sh_accuser_write_x5"},
98{ "Storage", "x_AccUser6", "sh_accuser_x6"},
99{ "Storage", "x_AccUser6_Share", "sh_accuser_share_x6"},
100{ "Storage", "x_AccUser6_Write", "sh_accuser_write_x6"},
101{ NULL, NULL, NULL }};
102
103char *nvram_get_i(char *name, int idx);
104char *mac_conv(char *mac_name, int idx, char *buf);
105
106/* This function is used to map nvram value from asus to Broadcom */
107void readFromNvram()
108{
109	char tmpstr[32], tmpstr1[32], macbuf[18];
110	char list[2048];
111	int i, num;
112
113	printf("read from nvram\n");
114
115	/* Wireless Section */
116	/* GMODE */
117	strcpy(tmpstr, nvram_get("wl_gmode_x"));
118
119	if (strcmp(tmpstr, "1")==0)
120	{
121		nvram_set("wl0_gmode", XSTR(GMODE_PERFORMANCE));
122	}
123	else if (strcmp(tmpstr, "2")==0)
124	{
125		nvram_set("wl0_gmode", XSTR(GMODE_LRS));
126	}
127	else if (strcmp(tmpstr, "3")==0)
128	{
129		nvram_set("wl0_gmode", XSTR(GMODE_B_DEFERRED));
130	}
131	else
132	{
133		nvram_set("wl0_gmode", XSTR(GMODE_AUTO));
134	}
135
136	if (nvram_match("wl_gmode_protection_x", "0")==0)
137		nvram_set("wl0_gmode_protection", "off");
138	else nvram_set("wl0_gmode_protection", "auto");
139
140	/* Authentication and Encryption */
141	strcpy(tmpstr, nvram_get("wl_authmode_x"));
142	strcpy(tmpstr1, nvram_get("wl_weptype_x"));
143
144	if (strcmp(tmpstr, "1")==0) /* Shared */
145	{
146		nvram_set("wl0_auth", "1");
147		nvram_set("wl0_auth_mode", "disabled");
148		nvram_set("wl0_wep", "wep");
149	}
150	else if (strcmp(tmpstr, "2")==0) /* WPA-PSK */
151	{
152		nvram_set("wl0_auth", "0");
153		nvram_set("wl0_auth_mode", "psk");
154
155		if(strcmp(tmpstr1, "1")==0) nvram_set("wl0_wep", "aes");
156		else if (strcmp(tmpstr1, "2")==0) nvram_set("wl0_wep", "tkip+aes");
157		else nvram_set("wl0_wep", "tkip");
158	}
159	else if (strcmp(tmpstr, "3")==0) /* WPA */
160	{
161		nvram_set("wl0_auth", "0");
162		nvram_set("wl0_auth_mode", "wpa");
163
164		if(strcmp(tmpstr1, "1")==0) nvram_set("wl0_wep", "aes");
165		else if (strcmp(tmpstr1, "2")==0) nvram_set("wl0_wep", "tkip+aes");
166		else nvram_set("wl0_wep", "tkip");
167
168	}
169	else if (strcmp(tmpstr, "4")==0) /* Radius */
170	{
171		nvram_set("wl0_auth", "0");
172		nvram_set("wl0_auth_mode", "radius");
173
174
175		if(strcmp(tmpstr1, "1")==0) nvram_set("wl0_wep", "wep");
176		else if (strcmp(tmpstr1, "2")==0) nvram_set("wl0_wep", "wep");
177		else nvram_set("wl0_wep", "");
178	}
179	else /* Open or Shared */
180	{
181		nvram_set("wl0_auth", "0");
182		nvram_set("wl0_auth_mode", "disabled");
183
184		if(strcmp(tmpstr1, "1")==0) nvram_set("wl0_wep", "wep");
185		else if (strcmp(tmpstr1, "2")==0) nvram_set("wl0_wep", "wep");
186		else nvram_set("wl0_wep", "");
187	}
188
189
190	/* WDS control */
191	if (nvram_match("wl_wds_x", "1")) nvram_set("wl0_mode", "wds");
192	else nvram_set("wl0_mode", "ap");
193
194	if (nvram_match("wl_lazywds", "1")) nvram_set("wl0_lazywds", "1");
195	else nvram_set("wl0_lazywds", "0");
196
197	if (nvram_match("wl_wdsapply_x", "1"))
198	{
199		num = atoi(nvram_get("wl_wdslist_num_x"));
200		list[0]=0;
201
202		for(i=0;i<num;i++)
203		{
204			sprintf(list, "%s %s", list, mac_conv("wl_wdslist_x", i, macbuf));
205		}
206		nvram_set("wl0_maclist", list);
207	}
208
209	/* Mac filter */
210	strcpy(tmpstr, nvram_get("wl_macmode_x"));
211
212	if (strcmp(tmpstr, "Accept")==0) /* allow */
213	{
214		nvram_set("wl0_macmode", "allow");
215	}
216	else if (strcmp(tmpstr, "Deny")==0) /* deny */
217	{
218		nvram_set("wl0_macmode", "deny");
219	}
220
221	if (!strcmp(tmpstr, "Disable"))
222	{
223		num = atoi(nvram_get("wl_maclist_num_x"));
224		list[0]=0;
225
226		for(i=0;i<num;i++)
227		{
228			sprintf(list, "%s;%s", list, mac_conv("wl_maclist_x", i, macbuf));
229		}
230
231		nvram_set("wl0_maclist", list);
232	}
233
234	/* Direct copy value */
235
236	nvram_set("wl0_ssid", nvram_get("wl_ssid"));
237	nvram_set("wl0_channel", nvram_get("wl_channel"));
238	nvram_set("wl0_country", nvram_get("wl_country"));
239	nvram_set("wl0_rate", nvram_get("wl_rate"));
240	nvram_set("wl0_frag", nvram_get("wl_frag"));
241	nvram_set("wl0_rts", nvram_get("wl_rts"));
242	nvram_set("wl0_dtim", nvram_get("wl_dtim"));
243	nvram_set("wl0_bcn", nvram_get("wl_bcn"));
244	nvram_set("wl0_plcphdr", nvram_get("wl_plcphdr"));
245	nvram_set("wl0_key", nvram_get("wl_key"));
246	nvram_set("wl0_key1", nvram_get("wl_key1"));
247	nvram_set("wl0_key2", nvram_get("wl_key2"));
248	nvram_set("wl0_key3", nvram_get("wl_key3"));
249	nvram_set("wl0_key4", nvram_get("wl_key4"));
250
251	/* LAN Section */
252
253	/* Storage Section */
254}
255
256void findNVRAMName(char *serviceId, char *field, char *name)
257{
258    int idx;
259
260    idx = 0;
261
262    //printf("find : %s %s\n", serviceId, field);
263
264    while(maps[idx].sid!=NULL)
265    {
266        if( strcmp(serviceId, maps[idx].sid) == 0)
267	{
268           if(strcmp(field, maps[idx].fid) == 0) break;
269	   //else if(strstr(field, maps[idx].fid)) break;
270	}
271        idx ++;
272    }
273
274    if (maps[idx].sid==NULL)
275    {
276        strcpy(name, field);
277    }
278    else
279    {
280        sprintf(name, maps[idx].name);
281
282 	//printf("nvram name: %s\n", name);
283    }
284}
285
286char *findpattern(char *target, char *pattern)
287{
288	char *find;
289	int len;
290
291	printf("find : %s %s\n", target, pattern);
292	if ((find=strstr(target, pattern)))
293	{
294		len = strlen(pattern);
295		if (find[len]==';' || find[len]==0)
296		{
297			return find;
298		}
299	}
300	return NULL;
301}
302
303char *strcat_r(char *dst, char *src, char *tar)
304{
305	sprintf(tar, "%s%s", dst, src);
306	return(tar);
307}
308
309
310char *nvram_get_i(char *name, int idx)
311{
312	char tmpstr1[64];
313
314	if (idx!=-1)
315	{
316		sprintf(tmpstr1, "%s%d", name, idx);
317	}
318	else
319	{
320		sprintf(tmpstr1, "%s", name);
321	}
322	//printf("get : %s %s %s\n", tmpstr2, tmpstr1, name1);
323
324	return(nvram_get(tmpstr1));
325}
326
327char *mac_conv(char *mac_name, int idx, char *buf)
328{
329	char *mac;
330	int i, j;
331
332	mac = nvram_get_i(mac_name, idx);
333
334	j=0;
335	for(i=0; i<12; i++)
336	{
337		if (i!=0&&i%2==0) buf[j++] = ':';
338		buf[j++] = mac[i];
339	}
340	buf[j] = 0;
341
342	return(buf);
343}
344
345
346int nvram_match_i(char *name, int idx, char *match) {
347	const char *value = nvram_get_i(name, idx);
348
349	printf("match %s %s\n", value, match);
350	return (value && !strcmp(value, match));
351}
352
353
354#ifndef NOUSB
355void nvram_set_i(char *name, int idx, char *value)
356{
357	char tmpstr1[64];
358
359	if (idx!=-1)
360	{
361		sprintf(tmpstr1, "%s%d", name, idx);
362	}
363	else
364	{
365		sprintf(tmpstr1, "%s", name);
366	}
367	//printf("get : %s %s %s\n", tmpstr2, tmpstr1, name1);
368
369	nvram_set(tmpstr1, value);
370}
371
372void nvram_cpy(char *name1, char *name2, int idx)
373{
374
375	char tmpstr1[289];
376
377	strcpy(tmpstr1, nvram_get_i(name2, idx));
378	nvram_set(name1, tmpstr1);
379}
380
381void nvram_cpy2(char *name1, char *name2, int idx)
382{
383
384	char tmpstr1[289];
385
386	strcpy(tmpstr1, nvram_get(name2));
387	nvram_set_i(name1, idx, tmpstr1);
388}
389
390void nvram_del_list(char *names, int *delMap)
391{
392    char oname[32], nname[32], *oval, *nval;
393    int oi, ni, di;
394
395    oi=0;
396    ni=0;
397    di=0;
398    while(1)
399    {
400        sprintf(oname, "%s%d", names, oi);
401        sprintf(nname, "%s%d", names, ni);
402
403        oval = nvram_get(oname);
404        nval = nvram_get(nname);
405
406        if (oval==NULL) break;
407
408        printf("delSharedEntry->nvram_del_list: %d %d %d %d %s\n", oi, ni, di, delMap[di], oval);
409
410        if (delMap[di]!=-1&&delMap[di]==oi)
411        {
412                oi++;
413                di++;
414        }
415        else
416        {
417                nvram_set(nname, oval);
418                ni++;
419                oi++;
420        }
421    }
422}
423
424void initSharedEntry()
425{
426    int index=atoi(nvram_safe_get("sh_num"));
427
428
429    // get index for latest shared entry
430    index--;
431
432    //nvram_set_i("sh_path", index, "");
433    //nvram_set_i("sh_name", index, "");
434    nvram_set_i("sh_comment", index, "");
435    nvram_set_i("sh_commentb", index, "");
436    nvram_set_i("sh_max_user", index, "12");
437    nvram_set_i("sh_acc_user", index, "");
438    nvram_set_i("sh_rright", index, "");
439    nvram_set_i("sh_wright", index, "");
440    nvram_set_i("sh_acc_onlist_num", index, "0");
441
442    //if (nvram_match_i("sh_rright_x", index, "on"))
443        //nvram_set_i("sh_rright", index, "Guest;");
444    //else nvram_set_i("sh_rright", index, "");
445
446
447    //if (nvram_match_i("sh_wright_x", index, "on"))
448        //nvram_set_i("sh_wright", index, "Guest;");
449    //else nvram_set_i("sh_wright", index, "");
450}
451
452void delSharedEntry(int *delMap)
453{
454    int count;
455    int i, del;
456    char cstr[16];
457
458    count = atoi(nvram_safe_get("sh_num"));
459
460    nvram_del_list("sh_path", delMap);
461    nvram_del_list("sh_name", delMap);
462    nvram_del_list("sh_nameb", delMap);
463    nvram_del_list("sh_comment", delMap);
464    nvram_del_list("sh_commentb", delMap);
465    nvram_del_list("sh_rright", delMap);
466    nvram_del_list("sh_wright", delMap);
467    nvram_del_list("sh_acc_user", delMap);
468    nvram_del_list("sh_acc_onlist_num", delMap);
469
470    i=0;
471    while(delMap[i]!=-1) i++;
472
473    count-=i;
474    sprintf(cstr, "%d\n", count);
475    nvram_set("sh_num", cstr);
476}
477#endif
478
479#ifndef NOUSB
480void getSharedEntry(int index)
481{
482    char idxstr[10], idxstr1[10], tmpstr[32], tmpstr1[289], acc[32];
483    char *path;
484    char rright[128], wright[128], username[32];
485    char *ruser, *wuser;
486    int i, j, acc_num;
487
488    sprintf(idxstr, "%d", index);
489    strcpy(tmpstr1, nvram_get(strcat_r("sh_path", idxstr, tmpstr)));
490//Yau
491//    printf("path: %s  index=%d\n", tmpstr1,index);
492
493    nvram_set("sh_path_x", idxstr);
494    nvram_cpy("sh_name_x", "sh_name", index);
495    nvram_cpy("sh_nameb_x", "sh_nameb", index);
496    nvram_cpy("sh_comment_x", "sh_comment", index);
497    nvram_cpy("sh_commentb_x", "sh_commentb", index);
498    nvram_cpy("sh_max_user_x", "sh_max_user", index);
499
500    nvram_cpy("sh_acc_onlist_num_x", "sh_acc_onlist_num", index);
501    nvram_cpy("sh_acc_user_x", "sh_acc_user", index);
502    nvram_set("sh_edit_x", idxstr);
503
504}
505#endif
506
507#ifndef NOUSB
508void setSharedEntry( char *value1, char *value2, char *value3, char *value4)
509{
510    int index;
511
512    index = atoi(nvram_get("sh_edit_x"));
513//Yau
514printf("setSharedEntry:: index= %d, %s, %s\n",index,value1,value2);
515    nvram_set_i("sh_name", index, value1);
516    nvram_set_i("sh_comment",index, value2);
517    nvram_set_i("sh_nameb", index, value3);
518    nvram_set_i("sh_commentb",index, value4);
519    //nvram_cpy("sh_max_user", "sh_max_user_x", index);
520}
521
522#endif
523#endif
524