root/src/router/services/sysinit/sysinit-dir300.c

Revision 12395, 8.8 kB (checked in by BrainSlayer, 5 months ago)

take out this set from the static ram

Line 
1 /*
2  * sysinit-dir300.c
3  *
4  * Copyright (C) 2006 Sebastian Gottschall <gottschall@dd-wrt.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * $Id:
21  */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <limits.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <syslog.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <sys/klog.h>
33 #include <sys/types.h>
34 #include <sys/mount.h>
35 #include <sys/reboot.h>
36 #include <sys/stat.h>
37 #include <sys/socket.h>
38 #include <sys/sysmacros.h>
39 #include <sys/ioctl.h>
40 #include <sys/time.h>
41 #include <sys/utsname.h>
42 #include <sys/wait.h>
43 #include <linux/if_ether.h>
44 #include <linux/mii.h>
45 #include <linux/sockios.h>
46 #include <net/if.h>
47
48 #include <arpa/inet.h>
49 #include <sys/socket.h>
50 #include <linux/sockios.h>
51 #include <linux/mii.h>
52
53 #include <bcmnvram.h>
54 #include <shutils.h>
55 #include <utils.h>
56 #include <cymac.h>
57
58 extern void vlan_init(int num);
59
60 void start_sysinit(void)
61 {
62         char buf[PATH_MAX];
63         struct utsname name;
64         struct stat tmp_stat;
65         time_t tm = 0;
66
67         unlink("/etc/nvram/.lock");
68         cprintf("sysinit() proc\n");
69         /*
70          * /proc
71          */
72         mount("proc", "/proc", "proc", MS_MGC_VAL, NULL);
73         mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
74         cprintf("sysinit() tmp\n");
75
76         /*
77          * /tmp
78          */
79         mount("ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL);
80         // fix for linux kernel 2.6
81         mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
82         eval("mkdir", "/tmp/www");
83         eval("mknod", "/dev/nvram", "c", "229", "0");
84         eval("mknod", "/dev/ppp", "c", "108", "0");
85
86         unlink("/tmp/nvram/.lock");
87         eval("mkdir", "/tmp/nvram");
88         cprintf("sysinit() var\n");
89
90         /*
91          * /var
92          */
93         mkdir("/tmp/var", 0777);
94         mkdir("/var/lock", 0777);
95         mkdir("/var/log", 0777);
96         mkdir("/var/run", 0777);
97         mkdir("/var/tmp", 0777);
98         cprintf("sysinit() setup console\n");
99         if (!nvram_match("disable_watchdog", "1"))
100                 eval("watchdog");
101         /*
102          * Setup console
103          */
104
105         cprintf("sysinit() klogctl\n");
106         klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel")));
107         cprintf("sysinit() get router\n");
108
109 #ifndef HAVE_DIR400
110         int mtd = getMTD("board_config");
111         char mtdpath[64];
112
113         sprintf(mtdpath, "/dev/mtdblock/%d", mtd);
114         FILE *fp = fopen(mtdpath, "rb");
115
116         if (fp) {
117                 fseek(fp, 0x1000, SEEK_SET);
118                 unsigned int test;
119
120                 fread(&test, 4, 1, fp);
121                 fprintf(stderr, "test pattern is %X\n", test);
122                 if (test != 0xffffffff) {
123                         fprintf(stderr,
124                                 "radio config fixup is required to clean bad stuff out of memory, otherwise the radio config cannot be detected\n");
125                         fseek(fp, 0, SEEK_SET);
126                         char *block = (char *)malloc(65536);
127
128                         fread(block, 65536, 1, fp);
129                         fclose(fp);
130                         int i;
131
132                         for (i = 0x1000; i < 65536; i++)
133                                 block[i] = 0xff;
134                         fp = fopen("/tmp/radio", "wb");
135                         fwrite(block, 65536, 1, fp);
136                         eval("mtd", "-f", "write", "/tmp/radio", "board_config");       // writes
137                         //
138                         // back
139                         // new
140                         // config
141                         // and
142                         // reboots
143                         eval("event", "5", "1", "15");
144                 }
145                 fclose(fp);
146         }
147 #else
148         if (!nvram_match("dir400preconfig", "1")) {
149                 nvram_set("dir400preconfig", "1");
150                 nvram_commit();
151                 int mtd = getMTD("fullflash");
152                 char mtdpath[64];
153                 char mac[18];
154
155                 sprintf(mtdpath, "/dev/mtdblock/%d", mtd);
156                 FILE *fp = fopen(mtdpath, "rb");
157                 int s = searchfor(fp, "lan_mac=", 512);
158
159                 if (s == -1) {
160                         fprintf(stderr, "no mac found in config\n");
161                         fclose(fp);
162                 } else {
163                         mac[17] = 0;
164                         fread(mac, 17, 1, fp);
165                         fclose(fp);
166                         mtd = getMTD("board_config");
167                         sprintf(mtdpath, "/dev/mtdblock/%d", mtd);
168                         fp = fopen(mtdpath, "rb");
169                         fseek(fp, 0, SEEK_SET);
170                         char *block = (char *)malloc(65536);
171
172                         fread(block, 65536, 1, fp);
173                         fclose(fp);
174                         unsigned char in_addr[6];
175                         int changed = 0;
176
177                         ether_atoe(mac, &in_addr[0]);
178                         if (memcmp(block + 96, &in_addr[0], 6)) {
179                                 changed++;
180                                 memcpy(block + 96, &in_addr[0], 6);     // wlan mac
181                         }
182                         in_addr[5]++;
183                         if (memcmp(block + 102, &in_addr[0], 6)) {
184                                 changed++;
185                                 memcpy(block + 102, &in_addr[0], 6);    // eth0 mac
186                         }
187                         in_addr[5]++;
188                         if (memcmp(block + 108, &in_addr[0], 6)) {
189                                 changed++;
190                                 memcpy(block + 108, &in_addr[0], 6);    // eth1 mac
191                         }
192                         in_addr[5]++;
193                         if (memcmp(block + 118, &in_addr[0], 6)) {
194                                 changed++;
195                                 memcpy(block + 118, &in_addr[0], 6);    // wlan1 mac
196                         }
197                         if (changed) {
198                                 fprintf(stderr,
199                                         "radio config needs to be adjusted, system will reboot after flashing\n");
200                                 fp = fopen("/tmp/radio", "wb");
201                                 fwrite(block, 65536, 1, fp);
202                                 fclose(fp);
203                                 eval("mtd", "-f", "write", "/tmp/radio", "board_config");       // writes
204                                 //
205                                 // back
206                                 // new
207                                 // config
208                                 // and
209                                 // reboots
210                                 eval("event", "5", "1", "15");
211                         } else {
212                                 fprintf(stderr,
213                                         "no change required, radio config remains unchanged\n");
214                         }
215                         free(block);
216                 }
217         }
218 #endif
219
220         /*
221          * Modules
222          */
223         uname(&name);
224         /*
225          * network drivers
226          */
227         insmod("ar2313");
228         insmod("ath_hal");
229         if (nvram_get("rate_control") != NULL) {
230                 char rate[64];
231
232                 sprintf(rate, "ratectl=%s", nvram_safe_get("rate_control"));
233                 eval("insmod", "ath_ahb", rate);
234         } else {
235                 insmod("ath_ahb");
236         }
237         // eval ("ifconfig", "wifi0", "up");
238         eval("ifconfig", "eth0", "up"); // wan
239         system2("echo 2 >/proc/sys/dev/wifi0/ledpin");
240         system2("echo 1 >/proc/sys/dev/wifi0/softled");
241         if (getRouterBrand() == ROUTER_BOARD_FONERA2200) {
242                 eval("ifconfig", "eth0", "up", "promisc");      // required for vlan config
243                 eval("/sbin/vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
244                 eval("/sbin/vconfig", "add", "eth0", "0");
245                 eval("/sbin/vconfig", "add", "eth0", "1");
246                 struct ifreq ifr;
247                 int s;
248
249                 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
250                         char eabuf[32];
251
252                         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
253                         ioctl(s, SIOCGIFHWADDR, &ifr);
254                         char macaddr[32];
255
256                         strcpy(macaddr,
257                                ether_etoa((unsigned char *)ifr.ifr_hwaddr.
258                                           sa_data, eabuf));
259                         nvram_set("et0macaddr", macaddr);
260 //          MAC_ADD( macaddr );
261                         ether_atoe(macaddr,
262                                    (unsigned char *)ifr.ifr_hwaddr.sa_data);
263                         strncpy(ifr.ifr_name, "vlan1", IFNAMSIZ);
264                         ioctl(s, SIOCSIFHWADDR, &ifr);
265                         close(s);
266                 }
267         } else {
268 //      system("swconfig dev eth0 set reset 1");
269 //      system("swconfig dev eth0 set enable_vlan 1");
270 #ifdef HAVE_SWCONFIG
271                 system("swconfig dev eth0 vlan 1 set ports \"0 1 2 3 5t\"");
272                 system("swconfig dev eth0 vlan 2 set ports \"4 5t\"");
273                 system("swconfig dev eth0 set apply");
274                 eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD");
275                 eval("vconfig", "add", "eth0", "1");
276                 eval("vconfig", "add", "eth0", "2");
277 //      set network.eth0_1.ports="0 1 2 3 5t"
278 #else
279                 vlan_init(0xff);        // 4 lan + 1 wan
280 #endif
281                 struct ifreq ifr;
282                 int s;
283
284                 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
285                         char eabuf[32];
286
287                         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
288                         ioctl(s, SIOCGIFHWADDR, &ifr);
289                         char macaddr[32];
290
291                         strcpy(macaddr,
292                                ether_etoa((unsigned char *)ifr.ifr_hwaddr.
293                                           sa_data, eabuf));
294                         nvram_set("et0macaddr", macaddr);
295                         // MAC_ADD (macaddr);
296                         ether_atoe(macaddr,
297                                    (unsigned char *)ifr.ifr_hwaddr.sa_data);
298                         strncpy(ifr.ifr_name, "vlan2", IFNAMSIZ);
299                         ioctl(s, SIOCSIFHWADDR, &ifr);
300                         close(s);
301                 }
302         }
303         // insmod("ipv6");
304
305         /*
306          * Set a sane date
307          */
308         stime(&tm);
309         nvram_set("wl0_ifname", "ath0");
310
311         return;
312 }
313
314 int check_cfe_nv(void)
315 {
316         nvram_set("portprio_support", "0");
317         return 0;
318 }
319
320 int check_pmon_nv(void)
321 {
322         return 0;
323 }
324
325 void start_overclocking(void)
326 {
327 }
328
329 void enable_dtag_vlan(int enable)
330 {
331
332 }
333
334 void start_fixboard(void)
335 {
336         int mtd = getMTD("board_config");
337         char mtdpath[64];
338
339         sprintf(mtdpath, "/dev/mtdblock/%d", mtd);
340         fprintf(stderr, "board config path = %s\n", mtdpath);
341         FILE *fp = fopen(mtdpath, "rb");
342
343         if (fp) {
344                 fseek(fp, 0x1000, SEEK_SET);
345                 unsigned int test;
346
347                 fread(&test, 4, 1, fp);
348                 fprintf(stderr, "test pattern is %X\n", test);
349                 if (test != 0xffffffff) {
350                         fprintf(stderr,
351                                 "radio config fixup is required to clean bad stuff out of memory, otherwise the radio config cannot be detected\n");
352                         fseek(fp, 0, SEEK_SET);
353                         char *block = (char *)malloc(65536);
354
355                         fread(block, 65536, 1, fp);
356                         fclose(fp);
357                         int i;
358
359                         for (i = 0x1000; i < 65536; i++)
360                                 block[i] = 0xff;
361                         fp = fopen("/tmp/radio", "wb");
362                         fwrite(block, 65536, 1, fp);
363                         eval("mtd", "-f", "write", "/tmp/radio", "board_config");       // writes
364                         //
365                         // back
366                         // new
367                         // config
368                         // and
369                         // reboots
370                         eval("event", "5", "1", "15");
371                 }
372                 fclose(fp);
373         }
374
375 }
Note: See TracBrowser for help on using the browser.