source: src/router/services/sysinit/sysinit-gateworx.c @ 10196

Last change on this file since 10196 was 10196, checked in by BrainSlayer, 5 years ago

use alternate version here

File size: 12.6 KB
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
54static 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    // system(devcall);
61    FILE *in = popen( devcall, "rb" );
62
63    fscanf( in, "%d", &res );
64    pclose( in );
65    return res > 0 ? 1 : 0;
66}
67
68#ifndef HAVE_TONZE
69#ifndef HAVE_NOP8670
70
71void checkupdate( void )
72{
73    int res, res2 = 0;
74    FILE *in =
75        popen( "/bin/cat /dev/mtdblock/0|/bin/grep NewMedia|wc -l", "rb" );
76    fscanf( in, "%d", &res );
77    pclose( in );
78    if( res == 0 )
79    {
80        in = popen( "/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.02\"|wc -l",
81                    "rb" );
82        fscanf( in, "%d", &res2 );
83        pclose( in );
84        if( res2 == 0 )
85        {
86            in = popen( "/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.04\"|wc -l",
87                        "rb" );
88            fscanf( in, "%d", &res2 );
89            pclose( in );
90            if( res2 == 1 || res2 == 7 )        // 7 is the result for debug
91                // info enabled reboot builds
92            {
93                fprintf( stderr, "updating avila type 2 redboot\n" );
94                eval( "tar", "-xaf", "/usr/lib/firmware/redboot.tg7", "-C",
95                      "/tmp" );
96                eval( "mtd", "-r", "-f", "write", "/tmp/avila-rb.bin",
97                      "RedBoot" );
98                return;
99            }
100        }
101    }
102    if( res == 1 )
103    {
104        in = popen( "/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.03\"|wc -l",
105                    "rb" );
106        fscanf( in, "%d", &res2 );
107        pclose( in );
108    }
109    if( res2 == 1 )             // redboot update is needed
110    {
111        in = popen( "/bin/dmesg|/bin/grep \"Memory: 64MB\"|wc -l", "rb" );
112        fscanf( in, "%d", &res );
113        pclose( in );
114        if( res == 1 )
115            res2 = 64;
116        in = popen( "/bin/dmesg|/bin/grep \"Memory: 32MB\"|wc -l", "rb" );
117        fscanf( in, "%d", &res );
118        pclose( in );
119        if( res == 1 )
120            res2 = 32;
121        in = popen( "/bin/dmesg|/bin/grep \"Memory: 128MB\"|wc -l", "rb" );
122        fscanf( in, "%d", &res );
123        pclose( in );
124        if( res == 1 )
125            res2 = 128;
126        in = popen( "/bin/dmesg|/bin/grep \"Memory: 256MB\"|wc -l", "rb" );
127        fscanf( in, "%d", &res );
128        pclose( in );
129        if( res == 1 )
130            res2 = 256;
131        fprintf( stderr, "updating redboot %d MB\n", res2 );
132        char fname[64];
133
134        sprintf( fname, "/tmp/rb-%d.bin", res2 );
135        eval( "tar", "-xaf", "/usr/lib/firmware/redboot.tg7", "-C", "/tmp" );
136        eval( "mtd", "-r", "-f", "write", fname, "RedBoot" );
137    }
138}
139
140#endif
141#endif
142int start_sysinit( void )
143{
144    struct utsname name;
145    struct stat tmp_stat;
146    time_t tm = 0;
147
148    unlink( "/etc/nvram/.lock" );
149    cprintf( "sysinit() proc\n" );
150    /*
151     * /proc
152     */
153    mount( "proc", "/proc", "proc", MS_MGC_VAL, NULL );
154    // system2 ("/etc/convert");
155    mount( "sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL );
156    cprintf( "sysinit() tmp\n" );
157
158    /*
159     * /tmp
160     */
161    mount( "ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL );
162    mount( "devpts", "/dev/pts", "devpts", 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( "mknod", "/dev/nvram", "c", "229", "0" );
171    eval( "mknod", "/dev/ppp", "c", "108", "0" );
172    eval( "mknod", "/dev/rtc", "c", "254", "0" );
173    eval( "mknod", "/dev/crypto", "c", "10", "70" );
174    eval( "mount", "-o", "remount,rw", "/" );
175
176    unlink( "/tmp/nvram/.lock" );
177    eval( "mkdir", "/tmp/nvram" );
178    // #ifdef HAVE_REGISTER
179    // #else
180    // eval ("/bin/tar", "-xzf", "/dev/mtdblock/3", "-C", "/");
181    // #endif
182    // mkdir ("/usr/local/nvram", 0777);
183    // unlink ("/tmp/nvram/.lock");
184    // eval ("mkdir", "/tmp/nvram");
185    // eval ("cp", "/etc/nvram/nvram.db", "/tmp/nvram");
186    // eval ("cp", "/etc/nvram/offsets.db", "/tmp/nvram");
187    cprintf( "sysinit() var\n" );
188
189    /*
190     * /var
191     */
192    mkdir( "/tmp/var", 0777 );
193    mkdir( "/var/lock", 0777 );
194    mkdir( "/var/log", 0777 );
195    mkdir( "/var/run", 0777 );
196    mkdir( "/var/tmp", 0777 );
197
198    eval( "watchdog" );         // system watchdog
199
200    cprintf( "sysinit() setup console\n" );
201
202    /*
203     * Setup console
204     */
205
206    cprintf( "sysinit() klogctl\n" );
207    klogctl( 8, NULL, atoi( nvram_safe_get( "console_loglevel" ) ) );
208    cprintf( "sysinit() get router\n" );
209
210    /*
211     * Modules
212     */
213    uname( &name );
214#ifndef HAVE_TONZE
215#ifndef HAVE_NOP8670
216    checkupdate(  );
217#endif
218#endif
219    nvram_set( "intel_eth", "0" );
220    if( detect( "82541" ) )     // Intel Gigabit
221    {
222        nvram_set( "intel_eth", "1" );
223        insmod( "e1000" );
224    }
225
226    // system("/etc/kendin");
227
228#ifndef HAVE_NOWIFI
229    insmod( "ath_hal" );
230    if( nvram_get( "rate_control" ) != NULL )
231    {
232        char rate[64];
233
234        sprintf( rate, "ratectl=%s", nvram_safe_get( "rate_control" ) );
235        eval("insmod", "ath_pci", rate );
236    }
237    else
238    {
239        insmod( "ath_pci" );
240    }
241#endif
242#ifdef HAVE_MADWIFI_MIMO
243    insmod( "ath_mimo_pci" );
244#endif
245
246#if 1
247    insmod( "ixp400th" );
248    insmod( "ixp400" );
249    system2( "cat /usr/lib/firmware/IxNpeMicrocode.dat > /dev/IxNpe" );
250    insmod( "ixp400_eth" );
251    eval( "ifconfig", "ixp0", "0.0.0.0", "up" );
252    eval( "ifconfig", "ixp1", "0.0.0.0", "up" );
253    /*
254     * if (getRouterBrand () == ROUTER_BOARD_GATEWORX_GW2345) //lets load the
255     * spi drivers for this switch { insmod("spi-algo-bit");
256     * insmod("spi-ixp4xx"); insmod("ks8995m"); sleep (1); system ("echo
257     * R01=01 > /proc/driver/KS8995M"); // enable switch }
258     */
259    insmod( "ocf" );
260    insmod( "cryptodev" );
261    // insmod("ixp4xx", "init_crypto=0");
262#else
263    // eval ("mknod", "/dev/IxNpe","c","10","184");
264    system2( "cat /usr/lib/firmware/NPE-B > /dev/misc/ixp4xx_ucode" );
265    system2( "cat /usr/lib/firmware/NPE-C > /dev/misc/ixp4xx_ucode" );
266#endif
267
268    // insmod("ath_pci", "rfkill=0", "autocreate=none");
269
270    /*
271     * if (ifexists ("wifi0")) eval ("ifconfig", "wifi0", "up"); if (ifexists
272     * ("wifi1")) eval ("ifconfig", "wifi1", "up"); if (ifexists ("wifi2"))
273     * eval ("ifconfig", "wifi2", "up"); if (ifexists ("wifi3")) eval
274     * ("ifconfig", "wifi3", "up"); if (ifexists ("wifi4")) eval ("ifconfig",
275     * "wifi4", "up"); if (ifexists ("wifi5")) eval ("ifconfig", "wifi5",
276     * "up");
277     */
278
279    // insmod("ipv6");
280
281    insmod( "ad7418" );         // temp / voltage sensor
282    /*
283     * Configure mac addresses by reading data from eeprom
284     */
285    // char *filename =
286    // "/sys/devices/platform/IXP4XX-I2C.0/i2c-0/0-0051/eeprom"; /*
287    // bank2=0x100 */
288#ifdef HAVE_NOP8670
289
290    char filename[64];
291
292    sprintf( filename, "/dev/mtdblock/%d", getMTD( "RedBoot config" ) );
293    FILE *file = fopen( filename, "r" );
294
295    if( file )
296    {
297        eval( "ifconfig", "ixp0", "0.0.0.0", "down" );
298        eval( "ifconfig", "ixp1", "0.0.0.0", "down" );
299        unsigned char buf[16];
300
301        fseek( file, 0x422, SEEK_SET );
302        fread( &buf[0], 6, 1, file );
303        char mac[16];
304
305        sprintf( mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2],
306                 buf[3], buf[4], buf[5] );
307        fprintf( stderr, "configure IXP0 to %s\n", mac );
308        eval( "ifconfig", "ixp0", "hw", "ether", mac );
309        fseek( file, 0x43b, SEEK_SET );
310        fread( &buf[6], 6, 1, file );
311        sprintf( mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8],
312                 buf[9], buf[10], buf[11] );
313        fprintf( stderr, "configure IXP1 to %s\n", mac );
314        eval( "ifconfig", "ixp1", "hw", "ether", mac );
315        fclose( file );
316        eval( "ifconfig", "ixp0", "0.0.0.0", "up" );
317        eval( "ifconfig", "ixp1", "0.0.0.0", "up" );
318    }
319#else
320    char *filename = "/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0051/eeprom";      /* bank2=0x100
321                                                                                                 */
322    FILE *file = fopen( filename, "r" );
323
324    if( file )
325    {
326        unsigned char buf[16];
327
328        fread( &buf[0], 16, 1, file );
329        char mac[16];
330
331        sprintf( mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2],
332                 buf[3], buf[4], buf[5] );
333        eval( "ifconfig", "ixp0", "hw", "ether", mac );
334        sprintf( mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8],
335                 buf[9], buf[10], buf[11] );
336        eval( "ifconfig", "ixp1", "hw", "ether", mac );
337
338        fclose( file );
339    }
340
341#endif
342    eval( "ifconfig", "ixp0", "0.0.0.0", "up" );
343    eval( "ifconfig", "ixp1", "0.0.0.0", "up" );
344    if( getRouterBrand(  ) == ROUTER_BOARD_GATEWORX_GW2345 )    // lets load
345        // the spi
346        // drivers
347        // for this
348        // switch
349    {
350        insmod( "spi-algo-bit" );
351        insmod( "spi-ixp4xx" );
352        insmod( "ks8995m" );
353        sleep( 1 );
354        system( "echo R01=01 > /proc/driver/KS8995M" ); // enable switch
355    }
356    char filename2[64];
357
358    sprintf( filename2, "/dev/mtdblock/%d", getMTD( "RedBoot" ) );
359    file = fopen( filename2, "r" );
360    if( file )
361    {
362        fseek( file, 0x1f800, SEEK_SET );
363        unsigned int signature;
364
365        fread( &signature, 4, 1, file );
366        if( signature == 0x20021103 )
367        {
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
377            sprintf( mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1],
378                     buf[2], buf[3], buf[4], buf[5] );
379            fprintf( stderr, "configure IXP0 to %s\n", mac );
380            eval( "ifconfig", "ixp0", "hw", "ether", mac );
381            fseek( file, 0x1f818, SEEK_SET );
382            fread( &buf[6], 6, 1, file );
383            sprintf( mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7],
384                     buf[8], buf[9], buf[10], buf[11] );
385            fprintf( stderr, "configure IXP1 to %s\n", mac );
386            eval( "ifconfig", "ixp1", "hw", "ether", mac );
387            eval( "ifconfig", "ixp0", "0.0.0.0", "up" );
388            eval( "ifconfig", "ixp1", "0.0.0.0", "up" );
389        }
390        else
391        {
392            eval( "insmod", "avila-ide" );
393        }
394        fclose( file );
395    }
396#ifdef HAVE_MAKSAT
397    if( nvram_match( "DD_BOARD2", "ADI Engineering Pronghorn Metro" ) )
398#else
399    if( nvram_match( "DD_BOARD", "ADI Engineering Pronghorn Metro" ) )
400#endif
401    {
402        fprintf( stderr, "Pronghorn Metro detected\n" );
403        eval( "setmac", "-f", "/dev/mtdblock/7", "-n", "1", "-i", "0", "-r",
404              "npe_eth0_esa" );
405        eval( "setmac", "-f", "/dev/mtdblock/7", "-n", "1", "-i", "1", "-r",
406              "npe_eth1_esa" );
407        struct ifreq ifr;
408        int s;
409
410        if( ( s = socket( AF_INET, SOCK_RAW, IPPROTO_RAW ) ) )
411        {
412            char eabuf[32];
413
414            strncpy( ifr.ifr_name, "ixp0", IFNAMSIZ );
415            ioctl( s, SIOCGIFHWADDR, &ifr );
416            nvram_set( "et0macaddr_safe",
417                       ether_etoa( ( unsigned char * )ifr.ifr_hwaddr.sa_data,
418                                   eabuf ) );
419            close( s );
420        }
421    }
422#ifdef HAVE_TONZE
423    {
424        struct ifreq ifr;
425        int s;
426
427        if( ( s = socket( AF_INET, SOCK_RAW, IPPROTO_RAW ) ) )
428        {
429            char eabuf[32];
430
431            strncpy( ifr.ifr_name, "ixp0", IFNAMSIZ );
432            ioctl( s, SIOCGIFHWADDR, &ifr );
433            nvram_set( "et0macaddr_safe",
434                       ether_etoa( ( unsigned char * )ifr.ifr_hwaddr.sa_data,
435                                   eabuf ) );
436            close( s );
437        }
438    }
439#endif
440
441    /*
442     * Set a sane date
443     */
444    stime( &tm );
445    nvram_set( "wl0_ifname", "ath0" );
446#ifndef HAVE_NOP8670
447    eval( "hwclock", "-s" );
448#endif
449    nvram_set( "use_crypto", "0" );
450    cprintf( "done\n" );
451    eval( "/bin/tar", "-xzf", "/dev/mtdblock/4", "-C", "/" );
452    FILE *in = fopen( "/tmp/nvram/nvram.db", "rb" );
453
454    if( in != NULL )
455    {
456        fclose( in );
457        eval( "/usr/sbin/convertnvram" );
458        eval( "/sbin/mtd", "erase", "nvram" );
459        nvram_commit(  );
460    }
461    return 0;
462}
463
464int check_cfe_nv( void )
465{
466    nvram_set( "portprio_support", "0" );
467    return 0;
468}
469
470int check_pmon_nv( void )
471{
472    return 0;
473}
474
475void start_overclocking( void )
476{
477}
478void enable_dtag_vlan( int enable )
479{
480
481}
Note: See TracBrowser for help on using the repository browser.