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

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

take out this set from the static ram

Line 
1 /*
2  * sysinit-gateworx.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  * System Initialisation for Avila Gateworks and compatible Routers
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <limits.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <syslog.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <termios.h>
35 #include <sys/klog.h>
36 #include <sys/types.h>
37 #include <sys/mount.h>
38 #include <sys/reboot.h>
39 #include <sys/stat.h>
40 #include <sys/sysmacros.h>
41 #include <sys/time.h>
42 #include <sys/utsname.h>
43 #include <sys/wait.h>
44
45 #include <bcmnvram.h>
46 #include <shutils.h>
47 #include <utils.h>
48 #include <arpa/inet.h>
49 #include <sys/socket.h>
50 #include <linux/if.h>
51 #include <linux/sockios.h>
52 #include <linux/mii.h>
53
54 static int detect(char *devicename)
55 {
56         char devcall[128];
57         int res;
58
59         sprintf(devcall, "/sbin/lspci|/bin/grep \"%s\"|/bin/wc -l", devicename);
60         FILE *in = popen(devcall, "rb");
61
62         fscanf(in, "%d", &res);
63         pclose(in);
64         return res > 0 ? 1 : 0;
65 }
66
67 #ifndef HAVE_TONZE
68 #ifndef HAVE_NOP8670
69
70 /*void checkupdate( void )
71 {
72     int res, res2 = 0;
73     FILE *in =
74         popen( "/bin/cat /dev/mtdblock/0|/bin/grep NewMedia|wc -l", "rb" );
75     fscanf( in, "%d", &res );
76     pclose( in );
77     if( res == 0 )
78     {
79         in = popen( "/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.02\"|wc -l",
80                     "rb" );
81         fscanf( in, "%d", &res2 );
82         pclose( in );
83         if( res2 == 0 )
84         {
85             in = popen( "/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.04\"|wc -l",
86                         "rb" );
87             fscanf( in, "%d", &res2 );
88             pclose( in );
89             if( res2 == 1 || res2 == 7 )        // 7 is the result for debug
90                 // info enabled reboot builds
91             {
92                 fprintf( stderr, "updating avila type 2 redboot\n" );
93                 eval( "tar", "-xaf", "/usr/lib/firmware/redboot.tg7", "-C",
94                       "/tmp" );
95                 eval( "mtd", "-r", "-f", "write", "/tmp/avila-rb.bin",
96                       "RedBoot" );
97                 return;
98             }
99         }
100     }
101     if( res == 1 )
102     {
103         in = popen( "/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.03\"|wc -l",
104                     "rb" );
105         fscanf( in, "%d", &res2 );
106         pclose( in );
107     }
108     if( res2 == 1 )             // redboot update is needed
109     {
110         in = popen( "/bin/dmesg|/bin/grep \"Memory: 64MB\"|wc -l", "rb" );
111         fscanf( in, "%d", &res );
112         pclose( in );
113         if( res == 1 )
114             res2 = 64;
115         in = popen( "/bin/dmesg|/bin/grep \"Memory: 32MB\"|wc -l", "rb" );
116         fscanf( in, "%d", &res );
117         pclose( in );
118         if( res == 1 )
119             res2 = 32;
120         in = popen( "/bin/dmesg|/bin/grep \"Memory: 128MB\"|wc -l", "rb" );
121         fscanf( in, "%d", &res );
122         pclose( in );
123         if( res == 1 )
124             res2 = 128;
125         in = popen( "/bin/dmesg|/bin/grep \"Memory: 256MB\"|wc -l", "rb" );
126         fscanf( in, "%d", &res );
127         pclose( in );
128         if( res == 1 )
129             res2 = 256;
130         fprintf( stderr, "updating redboot %d MB\n", res2 );
131         char fname[64];
132
133         sprintf( fname, "/tmp/rb-%d.bin", res2 );
134         eval( "tar", "-xaf", "/usr/lib/firmware/redboot.tg7", "-C", "/tmp" );
135         eval( "mtd", "-r", "-f", "write", fname, "RedBoot" );
136     }
137 }
138 */
139 #endif
140 #endif
141 void start_sysinit(void)
142 {
143         struct utsname name;
144         struct stat tmp_stat;
145         time_t tm = 0;
146
147         unlink("/etc/nvram/.lock");
148         cprintf("sysinit() proc\n");
149         /*
150          * /proc
151          */
152         mount("proc", "/proc", "proc", MS_MGC_VAL, NULL);
153         // system2 ("/etc/convert");
154         mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
155         cprintf("sysinit() tmp\n");
156
157         /*
158          * /tmp
159          */
160         mount("ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL);
161         mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
162         mount("devpts", "/proc/bus/usb", "usbfs", MS_MGC_VAL, NULL);
163         /*
164          * eval("mount","/etc/www.fs","/www","-t","squashfs","-o","loop");
165          * eval("mount","/etc/modules.fs","/lib/modules","-t","squashfs","-o","loop");
166          * eval("mount","/etc/usr.fs","/usr","-t","squashfs","-o","loop");
167          */
168         eval("mkdir", "/tmp/www");
169         eval("mknod", "/dev/gpio", "c", "127", "0");
170         eval("ledtool", "1", "1");      // blink the led 4 times
171         eval("mknod", "/dev/nvram", "c", "229", "0");
172         eval("mknod", "/dev/ppp", "c", "108", "0");
173         eval("mknod", "/dev/rtc", "c", "254", "0");
174         eval("mknod", "/dev/crypto", "c", "10", "70");
175         eval("mount", "-o", "remount,rw", "/");
176
177         unlink("/tmp/nvram/.lock");
178         eval("mkdir", "/tmp/nvram");
179         // #ifdef HAVE_REGISTER
180         // #else
181         // eval ("/bin/tar", "-xzf", "/dev/mtdblock/3", "-C", "/");
182         // #endif
183         // mkdir ("/usr/local/nvram", 0777);
184         // unlink ("/tmp/nvram/.lock");
185         // eval ("mkdir", "/tmp/nvram");
186         // eval ("cp", "/etc/nvram/nvram.db", "/tmp/nvram");
187         // eval ("cp", "/etc/nvram/offsets.db", "/tmp/nvram");
188         cprintf("sysinit() var\n");
189
190         /*
191          * /var
192          */
193         mkdir("/tmp/var", 0777);
194         mkdir("/var/lock", 0777);
195         mkdir("/var/log", 0777);
196         mkdir("/var/run", 0777);
197         mkdir("/var/tmp", 0777);
198
199         if (!nvram_match("disable_watchdog", "1"))
200                 eval("watchdog");       // system watchdog
201
202         cprintf("sysinit() setup console\n");
203
204         /*
205          * Setup console
206          */
207
208         cprintf("sysinit() klogctl\n");
209         klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel")));
210         cprintf("sysinit() get router\n");
211
212         /*
213          * Modules
214          */
215         uname(&name);
216 #ifndef HAVE_TONZE
217 #ifndef HAVE_NOP8670
218 //    checkupdate(  );
219 #endif
220 #endif
221         nvram_set("intel_eth", "0");
222         if (detect("82541"))    // Intel Gigabit
223         {
224                 nvram_set("intel_eth", "1");
225                 insmod("e1000");
226         }
227 #ifndef HAVE_NOWIFI
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_pci", rate);
234         } else {
235                 insmod("ath_pci");
236         }
237 #endif
238 #ifdef HAVE_MADWIFI_MIMO
239         insmod("ath_mimo_pci");
240 #endif
241
242 #if 1
243         insmod("ixp400th");
244         insmod("ixp400");
245         system2("cat /usr/lib/firmware/IxNpeMicrocode.dat > /dev/IxNpe");
246         insmod("ixp400_eth");
247         eval("ifconfig", "ixp0", "0.0.0.0", "up");
248         eval("ifconfig", "ixp1", "0.0.0.0", "up");
249         insmod("ocf");
250         insmod("cryptodev");
251         // insmod("ixp4xx", "init_crypto=0");
252 #else
253         // eval ("mknod", "/dev/IxNpe","c","10","184");
254         system2("cat /usr/lib/firmware/NPE-B > /dev/misc/ixp4xx_ucode");
255         system2("cat /usr/lib/firmware/NPE-C > /dev/misc/ixp4xx_ucode");
256 #endif
257
258         // insmod("ath_pci", "rfkill=0", "autocreate=none");
259
260         /*
261          * if (ifexists ("wifi0")) eval ("ifconfig", "wifi0", "up"); if (ifexists
262          * ("wifi1")) eval ("ifconfig", "wifi1", "up"); if (ifexists ("wifi2"))
263          * eval ("ifconfig", "wifi2", "up"); if (ifexists ("wifi3")) eval
264          * ("ifconfig", "wifi3", "up"); if (ifexists ("wifi4")) eval ("ifconfig",
265          * "wifi4", "up"); if (ifexists ("wifi5")) eval ("ifconfig", "wifi5",
266          * "up");
267          */
268
269         // insmod("ipv6");
270
271         insmod("ad7418");       // temp / voltage sensor
272         /*
273          * Configure mac addresses by reading data from eeprom
274          */
275         // char *filename =
276         // "/sys/devices/platform/IXP4XX-I2C.0/i2c-0/0-0051/eeprom"; /*
277         // bank2=0x100 */
278 #ifdef HAVE_NOP8670
279
280         char filename[64];
281
282         sprintf(filename, "/dev/mtdblock/%d", getMTD("RedBoot config"));
283         FILE *file = fopen(filename, "r");
284
285         if (file) {
286                 eval("ifconfig", "ixp0", "0.0.0.0", "down");
287                 eval("ifconfig", "ixp1", "0.0.0.0", "down");
288                 unsigned char buf[16];
289                 int i;
290
291                 fseek(file, 0x422, SEEK_SET);
292                 fread(&buf[0], 6, 1, file);
293                 char mac[16];
294                 unsigned int copy[16];
295
296                 for (i = 0; i < 6; i++)
297                         copy[i] = buf[i] & 0xff;
298                 sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", copy[0], copy[1],
299                         copy[2], copy[3], copy[4], copy[5]);
300                 fprintf(stderr, "configure IXP0 to %s\n", mac);
301                 eval("ifconfig", "ixp0", "hw", "ether", mac);
302                 fseek(file, 0x43b, SEEK_SET);
303                 fread(&buf[6], 6, 1, file);
304                 for (i = 0; i < 12; i++)
305                         copy[i] = buf[i] & 0xff;
306                 sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", copy[6], copy[7],
307                         copy[8], copy[9], copy[10], copy[11]);
308                 fprintf(stderr, "configure IXP1 to %s\n", mac);
309                 eval("ifconfig", "ixp1", "hw", "ether", mac);
310                 fclose(file);
311                 eval("ifconfig", "ixp0", "0.0.0.0", "up");
312                 eval("ifconfig", "ixp1", "0.0.0.0", "up");
313         }
314 #else
315         char *filename = "/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0051/eeprom";  /* bank2=0x100
316                                                                                                  */
317         FILE *file = fopen(filename, "r");
318
319         if (file) {
320                 unsigned char buf[16];
321
322                 fread(&buf[0], 16, 1, file);
323                 char mac[16];
324
325                 unsigned int copy[16];
326                 int i;
327
328                 for (i = 0; i < 12; i++)
329                         copy[i] = buf[i] & 0xff;
330                 sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", copy[0], copy[1],
331                         copy[2], copy[3], copy[4], copy[5]);
332                 eval("ifconfig", "ixp0", "hw", "ether", mac);
333                 sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", copy[6], copy[7],
334                         copy[8], copy[9], copy[10], copy[11]);
335                 eval("ifconfig", "ixp1", "hw", "ether", mac);
336
337                 fclose(file);
338         }
339 #endif
340         eval("ifconfig", "ixp0", "0.0.0.0", "up");
341         eval("ifconfig", "ixp1", "0.0.0.0", "up");
342         if (getRouterBrand() == ROUTER_BOARD_GATEWORX_GW2345)   // lets load
343                 // the spi
344                 // drivers
345                 // for this
346                 // switch
347         {
348                 insmod("spi-algo-bit");
349                 if (nvram_match("DD_BOARD", "Avila GW2355"))
350                         insmod("spi-ixp4xx-gw2355");
351                 else
352                         insmod("spi-ixp4xx");
353                 insmod("ks8995m");
354                 sleep(1);
355                 system2("echo R01=01 > /proc/driver/KS8995M");  // enable switch
356         }
357
358         char filename2[64];
359
360         sprintf(filename2, "/dev/mtdblock/%d", getMTD("RedBoot"));
361         file = fopen(filename2, "r");
362         if (file) {
363                 fseek(file, 0x1f800, SEEK_SET);
364                 unsigned int signature;
365
366                 fread(&signature, 4, 1, file);
367                 if (signature == 0x20021103) {
368                         fprintf(stderr, "Compex WP188 detected\n");
369                         eval("ifconfig", "ixp0", "0.0.0.0", "down");
370                         eval("ifconfig", "ixp1", "0.0.0.0", "down");
371                         unsigned char buf[16];
372
373                         fseek(file, 0x1f810, SEEK_SET);
374                         fread(&buf[0], 6, 1, file);
375                         char mac[16];
376                         int i;
377
378                         unsigned int copy[16];
379
380                         for (i = 0; i < 12; i++)
381                                 copy[i] = buf[i] & 0xff;
382
383                         sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", copy[0],
384                                 copy[1], copy[2], copy[3], copy[4], copy[5]);
385                         fprintf(stderr, "configure IXP0 to %s\n", mac);
386                         eval("ifconfig", "ixp0", "hw", "ether", mac);
387                         fseek(file, 0x1f818, SEEK_SET);
388                         fread(&buf[6], 6, 1, file);
389                         for (i = 0; i < 12; i++)
390                                 copy[i] = buf[i] & 0xff;
391                         sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x", copy[6],
392                                 copy[7], copy[8], copy[9], copy[10], copy[11]);
393                         fprintf(stderr, "configure IXP1 to %s\n", mac);
394                         eval("ifconfig", "ixp1", "hw", "ether", mac);
395                         eval("ifconfig", "ixp0", "0.0.0.0", "up");
396                         eval("ifconfig", "ixp1", "0.0.0.0", "up");
397                 } else {
398                         eval("insmod", "avila-ide");
399                 }
400                 fclose(file);
401         }
402 #ifdef HAVE_MAKSAT
403         if (nvram_match("DD_BOARD2", "ADI Engineering Pronghorn Metro"))
404 #else
405         if (nvram_match("DD_BOARD", "ADI Engineering Pronghorn Metro"))
406 #endif
407         {
408                 fprintf(stderr, "Pronghorn Metro detected\n");
409                 eval("setmac", "-f", "/dev/mtdblock/7", "-n", "1", "-i", "0",
410                      "-r", "npe_eth0_esa");
411                 eval("setmac", "-f", "/dev/mtdblock/7", "-n", "1", "-i", "1",
412                      "-r", "npe_eth1_esa");
413                 struct ifreq ifr;
414                 int s;
415
416                 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
417                         char eabuf[32];
418
419                         strncpy(ifr.ifr_name, "ixp0", IFNAMSIZ);
420                         ioctl(s, SIOCGIFHWADDR, &ifr);
421                         nvram_set("et0macaddr_safe",
422                                   ether_etoa((unsigned char *)ifr.ifr_hwaddr.
423                                              sa_data, eabuf));
424                         close(s);
425                 }
426         }
427 #ifdef HAVE_WG302V1
428         eval("setmac", "-f", "/dev/mtdblock/7", "-n", "1", "-i", "0", "-r",
429              "zcom_npe_esa");
430         struct ifreq ifr;
431         int s;
432
433         if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
434                 char eabuf[32];
435
436                 strncpy(ifr.ifr_name, "ixp0", IFNAMSIZ);
437                 ioctl(s, SIOCGIFHWADDR, &ifr);
438                 nvram_set("et0macaddr_safe",
439                           ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data,
440                                      eabuf));
441                 close(s);
442         }
443 #else
444 #ifdef HAVE_WG302
445         eval("setmac", "-f", "/dev/mtdblock/7", "-n", "1", "-i", "0", "-r",
446              "npe_eth0_esa");
447         struct ifreq ifr;
448         int s;
449
450         if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
451                 char eabuf[32];
452
453                 strncpy(ifr.ifr_name, "ixp0", IFNAMSIZ);
454                 ioctl(s, SIOCGIFHWADDR, &ifr);
455                 nvram_set("et0macaddr_safe",
456                           ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data,
457                                      eabuf));
458                 close(s);
459         }
460 #endif
461 #endif
462 #ifdef HAVE_TONZE
463         {
464                 struct ifreq ifr;
465                 int s;
466
467                 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
468                         char eabuf[32];
469
470                         strncpy(ifr.ifr_name, "ixp0", IFNAMSIZ);
471                         ioctl(s, SIOCGIFHWADDR, &ifr);
472                         nvram_set("et0macaddr_safe",
473                                   ether_etoa((unsigned char *)ifr.ifr_hwaddr.
474                                              sa_data, eabuf));
475                         close(s);
476                 }
477         }
478 #endif
479
480 #ifdef HAVE_CAMBRIA
481         if (nvram_match("DD_BOARD", "Cambria GW2358-4")
482             || nvram_match("DD_BOARD2", "Cambria GW2358-4")) {
483                 insmod("8250_gw2358");
484                 system2("echo 0 >/proc/sys/dev/wifi0/ledpin");
485                 system2("echo 1 >/proc/sys/dev/wifi0/softled");
486                 system2("echo 1 >/proc/sys/dev/wifi1/ledpin");
487                 system2("echo 1 >/proc/sys/dev/wifi1/softled");
488                 system2("echo 2 >/proc/sys/dev/wifi2/ledpin");
489                 system2("echo 1 >/proc/sys/dev/wifi2/softled");
490                 system2("echo 3 >/proc/sys/dev/wifi3/ledpin");
491                 system2("echo 1 >/proc/sys/dev/wifi3/softled");
492         }
493         if (nvram_match("DD_BOARD", "Cambria GW2350")
494             || nvram_match("DD_BOARD2", "Cambria GW2350")) {
495                 insmod("8250_gw2350");
496         }
497 #endif
498
499         /*
500          * Set a sane date
501          */
502         stime(&tm);
503         nvram_set("wl0_ifname", "ath0");
504 #ifndef HAVE_NOP8670
505         eval("hwclock", "-s");
506 #endif
507         nvram_set("use_crypto", "0");
508         cprintf("done\n");
509         eval("/bin/tar", "-xzf", "/dev/mtdblock/4", "-C", "/");
510         FILE *in = fopen("/tmp/nvram/nvram.db", "rb");
511
512         if (in != NULL) {
513                 fclose(in);
514                 eval("/usr/sbin/convertnvram");
515                 eval("/sbin/mtd", "erase", "nvram");
516                 nvram_commit();
517         }
518         return;
519 }
520
521 int check_cfe_nv(void)
522 {
523         nvram_set("portprio_support", "0");
524         return 0;
525 }
526
527 int check_pmon_nv(void)
528 {
529         return 0;
530 }
531
532 void start_overclocking(void)
533 {
534 }
535
536 void enable_dtag_vlan(int enable)
537 {
538
539 }
Note: See TracBrowser for help on using the browser.