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

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

take out this set from the static ram

Line 
1 /*
2  * sysinit-adm5120.c
3  *
4  * Copyright (C) 2008 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/sysmacros.h>
38 #include <sys/time.h>
39 #include <sys/utsname.h>
40 #include <sys/wait.h>
41 #include <linux/if_ether.h>
42 #include <linux/mii.h>
43 #include <linux/sockios.h>
44 #include <net/if.h>
45
46 #include <arpa/inet.h>
47 #include <sys/socket.h>
48 #include <linux/sockios.h>
49 #include <linux/mii.h>
50
51 #include <bcmnvram.h>
52 #include <shutils.h>
53 #include <utils.h>
54 #include <cymac.h>
55
56 #define sys_reboot() eval("sync"); eval("event","3","1","15")
57
58 extern void vlan_init(int num);
59
60 unsigned char toNumeric(unsigned char value)
61 {
62         if (value > ('0' - 1) && value < ('9' + 1))
63                 return value - '0';
64         if (value > ('a' - 1) && value < ('f' + 1))
65                 return value - 'a' + 10;
66         if (value > ('A' - 1) && value < ('F' + 1))
67                 return value - 'A' + 10;
68         return value;
69 }
70
71 struct mylo_eth_addr {
72         uint8_t mac[6];
73         uint8_t csum[2];
74 };
75
76 struct mylo_board_params {
77         uint32_t magic;         /* must be MYLO_MAGIC_BOARD_PARAMS */
78         uint32_t res0;
79         uint32_t res1;
80         uint32_t res2;
81         struct mylo_eth_addr addr[8];
82 };
83
84 void start_change_mac(void)
85 {
86         int i;
87         FILE *fp;
88         unsigned char os[32];
89         char mtdpath[32];
90
91         int mtd = getMTD("boot");
92
93         sprintf(mtdpath, "/dev/mtdblock/%d", mtd);
94         fp = fopen(mtdpath, "rb");
95       nexttry:;
96         fprintf(stdout,
97                 "MAC Invalid. Please enter new MAC Address: (format xx:xx:xx:xx:xx:xx)\n-->");
98         char maddr[64];
99
100         fscanf(stdin, "%s", maddr);
101         int newmac[6];
102         int ret = sscanf(maddr, "%02x:%02x:%02x:%02x:%02x:%02x", &newmac[0],
103                          &newmac[1], &newmac[2], &newmac[3], &newmac[4],
104                          &newmac[5]);
105         if (ret != 6) {
106                 fprintf(stdout, "\ninvalid format!, try again\n");
107                 goto nexttry;
108         }
109         //valid;
110         for (i = 0; i < 6; i++)
111                 sprintf(os, "%02x%02x%02x%02x%02x%02x", newmac[0] & 0xff,
112                         newmac[1] & 0xff, newmac[2] & 0xff, newmac[3] & 0xff,
113                         newmac[4] & 0xff, newmac[5] & 0xff);
114         fprintf(stderr, "new mac will be %s\n", os);
115         FILE *tmp = fopen("/tmp/boot.bin", "w+b");
116
117         fseek(fp, 0, SEEK_SET);
118         for (i = 0; i < 65536; i++)
119                 putc(getc(fp), tmp);
120         fseek(tmp, 0xff82, SEEK_SET);
121         for (i = 0; i < 12; i++)
122                 putc(os[i], tmp);
123         fclose(tmp);
124         sysprintf("mtd -f write /tmp/boot.bin boot");
125         fclose(fp);
126
127 }
128
129 void start_sysinit(void)
130 {
131         char buf[PATH_MAX];
132         struct utsname name;
133         struct stat tmp_stat;
134         time_t tm = 0;
135
136         cprintf("sysinit() proc\n");
137         /*
138          * /proc
139          */
140         mount("proc", "/proc", "proc", MS_MGC_VAL, NULL);
141         mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
142         cprintf("sysinit() tmp\n");
143
144         /*
145          * /tmp
146          */
147         mount("ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL);
148         // fix for linux kernel 2.6
149         mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
150         eval("mkdir", "/tmp/www");
151         eval("mknod", "/dev/nvram", "c", "229", "0");
152         eval("mknod", "/dev/ppp", "c", "108", "0");
153         eval("mknod", "-m", "0660", "/dev/mmc", "b", "126", "0");
154         eval("mknod", "-m", "0660", "/dev/mmc0", "b", "126", "1");
155         eval("mknod", "-m", "0660", "/dev/mmc1", "b", "126", "2");
156         eval("mknod", "-m", "0660", "/dev/mmc2", "b", "126", "3");
157         eval("mknod", "-m", "0660", "/dev/mmc3", "b", "126", "4");
158
159         eval("mkdir", "/dev/mtd");
160         eval("mknod", "/dev/mtd/0", "c", "90", "0");
161         eval("mknod", "/dev/mtd/0ro", "c", "90", "1");
162         eval("mknod", "/dev/mtd/1", "c", "90", "2");
163         eval("mknod", "/dev/mtd/1ro", "c", "90", "3");
164         eval("mknod", "/dev/mtd/2", "c", "90", "4");
165         eval("mknod", "/dev/mtd/2ro", "c", "90", "5");
166         eval("mknod", "/dev/mtd/3", "c", "90", "6");
167         eval("mknod", "/dev/mtd/3ro", "c", "90", "7");
168         eval("mknod", "/dev/mtd/4", "c", "90", "8");
169         eval("mknod", "/dev/mtd/4ro", "c", "90", "9");
170
171         cprintf("sysinit() var\n");
172
173         /*
174          * /var
175          */
176         mkdir("/tmp/var", 0777);
177         mkdir("/var/lock", 0777);
178         mkdir("/var/log", 0777);
179         mkdir("/var/run", 0777);
180         mkdir("/var/tmp", 0777);
181         cprintf("sysinit() setup console\n");
182         /*
183          * Setup console
184          */
185
186         cprintf("sysinit() klogctl\n");
187         klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel")));
188         cprintf("sysinit() get router\n");
189
190         /*
191          * Modules
192          */
193         uname(&name);
194         /*
195          * load some netfilter stuff
196          */
197 #ifndef HAVE_WP54G
198 #ifndef HAVE_NP28G
199         insmod("nf_conntrack_ftp");
200         insmod("nf_conntrack_irc");
201         insmod("nf_conntrack_netbios_ns");
202         insmod("nf_conntrack_pptp");
203         insmod("nf_conntrack_proto_gre");
204         insmod("nf_conntrack_proto_udplite");
205         insmod("nf_conntrack_tftp");
206         insmod("xt_CLASSIFY");
207         insmod("xt_MARK");
208         insmod("xt_TCPMSS");
209         insmod("xt_length");
210         insmod("xt_limit");
211         insmod("xt_multiport");
212         insmod("xt_pkttype");
213         insmod("xt_state");
214         insmod("xt_tcpmss");
215         insmod("xt_u32");
216
217         insmod("iptable_filter");
218         insmod("iptable_mangle");
219         insmod("nf_nat");
220         insmod("iptable_nat");
221         insmod("nf_nat_ftp");
222         insmod("nf_nat_irc");
223         insmod("nf_nat_pptp");
224         insmod("nf_nat_proto_gre");
225         insmod("nf_nat_tftp");
226         insmod("ipt_LOG");
227         insmod("ipt_MASQUERADE");
228         insmod("ipt_REDIRECT");
229         insmod("ipt_REJECT");
230         insmod("ipt_ULOG");
231         insmod("ipt_TRIGGER");
232         insmod("ipt_iprange");
233         insmod("ipt_ipp2p");
234         insmod("ipt_layer7");
235         insmod("ipt_webstr");
236
237         // ppp drivers
238
239         insmod("slhc");
240         insmod("ppp_generic");
241         insmod("ppp_async");
242         insmod("ppp_synctty");
243         insmod("ppp_mppe_mppc ");
244         insmod("pppox");
245         insmod("pppoe");
246 #endif
247 #endif
248         insmod("adm5120_wdt");
249         insmod("adm5120sw");
250
251         if (getRouterBrand() != ROUTER_BOARD_WP54G
252             && getRouterBrand() != ROUTER_BOARD_NP28G) {
253
254                 unsigned char mac[6];
255                 char eabuf[32];
256                 char mtdpath[32];
257
258                 memset(mac, 0, 6);
259                 FILE *fp;
260                 int mtd = getMTD("boot");
261                 int foundmac = 0;
262
263                 sprintf(mtdpath, "/dev/mtdblock/%d", mtd);
264                 fp = fopen(mtdpath, "rb");
265                 if (fp != NULL) {
266                         //check for osbridge
267                         fseek(fp, 0xff90 - 2, SEEK_SET);
268                         unsigned char os[32];
269
270                         fread(os, 32, 1, fp);
271                         if (strcmp(os, "OSBRiDGE 5XLi") == 0) {
272                                 foundmac = 1;
273                                 fprintf(stderr, "found OSBRiDGE 5XLi\n");
274                                 fseek(fp, 0xff82, SEEK_SET);
275                                 fread(os, 12, 1, fp);
276                                 int i;
277                                 int count = 0;
278
279                                 if (memcmp(os, "0050fc488130", 12) == 0) {
280                                         //force change mac
281                                         fclose(fp);
282                                         start_change_mac();
283                                         sys_reboot();
284
285                                 }
286                                 for (i = 0; i < 6; i++) {
287                                         mac[i] = toNumeric(os[count++]) * 16;
288                                         mac[i] |= toNumeric(os[count++]);
289                                 }
290                                 struct ifreq ifr;
291                                 int s;
292
293                                 if ((s =
294                                      socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
295                                         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
296                                         ioctl(s, SIOCGIFHWADDR, &ifr);
297                                         memcpy((unsigned char *)ifr.ifr_hwaddr.
298                                                sa_data, mac, 6);
299                                         ioctl(s, SIOCSIFHWADDR, &ifr);
300                                         close(s);
301                                 }
302                                 if ((s =
303                                      socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
304                                         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
305                                         ioctl(s, SIOCGIFHWADDR, &ifr);
306                                         nvram_set("et0macaddr_safe",
307                                                   ether_etoa((unsigned char *)
308                                                              ifr.
309                                                              ifr_hwaddr.sa_data,
310                                                              eabuf));
311                                         close(s);
312                                 }
313                         }
314                         if (!foundmac) {
315                                 int s = searchfor(fp, "mgmc", 0x20000 - 5);
316
317                                 if (s != -1) {
318                                         fread(mac, 6, 1, fp);
319                                         struct ifreq ifr;
320                                         int s;
321
322                                         foundmac = 1;
323                                         fprintf(stderr, "found Tonze-AP120\n");
324                                         if ((s =
325                                              socket(AF_INET, SOCK_RAW,
326                                                     IPPROTO_RAW))) {
327                                                 strncpy(ifr.ifr_name, "eth0",
328                                                         IFNAMSIZ);
329                                                 ioctl(s, SIOCGIFHWADDR, &ifr);
330                                                 memcpy((unsigned char *)
331                                                        ifr.ifr_hwaddr.sa_data,
332                                                        mac, 6);
333                                                 ioctl(s, SIOCSIFHWADDR, &ifr);
334                                                 close(s);
335                                         }
336                                         if ((s =
337                                              socket(AF_INET, SOCK_RAW,
338                                                     IPPROTO_RAW))) {
339                                                 strncpy(ifr.ifr_name, "eth0",
340                                                         IFNAMSIZ);
341                                                 ioctl(s, SIOCGIFHWADDR, &ifr);
342                                                 nvram_set("et0macaddr_safe",
343                                                           ether_etoa((unsigned
344                                                                       char *)
345                                                                      ifr.ifr_hwaddr.sa_data, eabuf));
346                                                 close(s);
347                                         }
348                                 }
349                         }
350
351                         if (foundmac == 0) {
352                                 fprintf(stderr,
353                                         "error: no valid mac address found for eth0\n");
354                         }
355                         fclose(fp);
356                 }
357         } else {
358                 struct mylo_board_params params;
359                 char mtdpath[32];
360                 FILE *fp;
361                 int mtd = getMTD("boot");
362                 int foundmac = 0;
363                 struct ifreq ifr;
364                 int s;
365                 char eabuf[32];
366
367                 sprintf(mtdpath, "/dev/mtdblock/%d", mtd);
368                 fp = fopen(mtdpath, "rb");
369                 if (fp != NULL) {
370                         fseek(fp, 0xf800, SEEK_SET);
371                         fread(&params, sizeof(params), 1, fp);
372                         fclose(fp);
373                         if (params.magic == 0x20021103) {
374                                 fprintf(stderr, "Found compex board magic!\n");
375                                 if ((s =
376                                      socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
377                                         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
378                                         ioctl(s, SIOCGIFHWADDR, &ifr);
379                                         memcpy((unsigned char *)ifr.ifr_hwaddr.
380                                                sa_data, params.addr[0].mac, 6);
381                                         ioctl(s, SIOCSIFHWADDR, &ifr);
382                                         close(s);
383                                 }
384                                 if ((s =
385                                      socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
386                                         strncpy(ifr.ifr_name, "eth1", IFNAMSIZ);
387                                         ioctl(s, SIOCGIFHWADDR, &ifr);
388                                         memcpy((unsigned char *)ifr.ifr_hwaddr.
389                                                sa_data, params.addr[1].mac, 6);
390                                         ioctl(s, SIOCSIFHWADDR, &ifr);
391                                         close(s);
392                                 }
393                                 if ((s =
394                                      socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
395                                         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
396                                         ioctl(s, SIOCGIFHWADDR, &ifr);
397                                         nvram_set("et0macaddr_safe",
398                                                   ether_etoa((unsigned char *)
399                                                              ifr.
400                                                              ifr_hwaddr.sa_data,
401                                                              eabuf));
402                                         close(s);
403                                 }
404
405                         }
406                 }
407         }
408         /*
409          * network drivers
410          */
411         insmod("ath_hal");
412         if (nvram_get("rate_control") != NULL) {
413                 char rate[64];
414
415                 sprintf(rate, "ratectl=%s", nvram_safe_get("rate_control"));
416                 eval("insmod", "ath_pci", rate);
417         } else {
418                 insmod("ath_pci");
419         }
420
421         if (!nvram_match("disable_watchdog", "1"))
422                 eval("watchdog");
423
424 #ifdef HAVE_WP54G
425         system2("echo 6 >/proc/sys/dev/wifi0/ledpin");
426         system2("echo 1 >/proc/sys/dev/wifi0/softled");
427 #endif
428         /*
429          * Set a sane date
430          */
431
432         stime(&tm);
433         nvram_set("wl0_ifname", "ath0");
434
435         return;
436 }
437
438 int check_cfe_nv(void)
439 {
440         nvram_set("portprio_support", "0");
441         return 0;
442 }
443
444 int check_pmon_nv(void)
445 {
446         return 0;
447 }
448
449 void start_overclocking(void)
450 {
451 }
452
453 char *enable_dtag_vlan(int enable)
454 {
455         return "eth2";
456 }
Note: See TracBrowser for help on using the browser.