source: src/router/services/sysinit/sysinit-x86.c @ 13509

Last change on this file since 13509 was 13509, checked in by BrainSlayer, 3 years ago

generic detection method

File size: 6.5 KB
Line 
1/*
2 * sysinit-x86.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 Standard PC 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
49#include <arpa/inet.h>
50#include <sys/socket.h>
51#include <linux/if.h>
52#include <linux/sockios.h>
53#include <linux/mii.h>
54#include "devices/ethernet.c"
55
56static int getdiscindex(void)   // works only for squashfs
57{
58        int i;
59
60        for (i = 0; i < 10; i++) {
61                char dev[64];
62
63                sprintf(dev, "/dev/discs/disc%d/part2", i);
64                FILE *in = fopen(dev, "rb");
65
66                if (in == NULL)
67                        continue;       // no second partition or disc does not
68                // exist, skipping
69                char buf[4];
70
71                fread(buf, 4, 1, in);
72                if (buf[0] == 'h' && buf[1] == 's' && buf[2] == 'q'
73                    && buf[3] == 't') {
74                        fclose(in);
75                        // filesystem detected
76                        return i;
77                }
78                fclose(in);
79        }
80        return -1;
81}
82
83void start_sysinit(void)
84{
85        struct utsname name;
86        time_t tm = 0;
87
88        unlink("/etc/nvram/.lock");
89        cprintf("sysinit() proc\n");
90        /*
91         * /proc
92         */
93        mount("proc", "/proc", "proc", MS_MGC_VAL, NULL);
94        mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL);
95        mount("debugfs", "/sys/kernel/debug", "debugfs", MS_MGC_VAL, NULL);
96        cprintf("sysinit() tmp\n");
97
98        /*
99         * /tmp
100         */
101        mount("ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL);
102        mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL);
103        mount("devpts", "/proc/bus/usb", "usbfs", MS_MGC_VAL, NULL);
104        eval("mknod", "/dev/ppp", "c", "108", "0");
105        eval("mknod", "/dev/nvram", "c", "229", "0");
106        char dev[64];
107        int index = getdiscindex();
108
109        if (index == -1) {
110                fprintf(stderr,
111                        "no valid dd-wrt partition found, calling shell");
112                eval("/bin/sh");
113                exit(0);
114        }
115        // sprintf (dev, "/dev/discs/disc%d/part1", index);
116        // mount (dev, "/boot", "ext2", MS_MGC_VAL, NULL);
117
118        sprintf(dev, "/dev/discs/disc%d/part3", index);
119        // eval("insmod","block2mtd",dev);
120        // sprintf (dev, "/dev/mtdblock/0");
121        // eval ("fsck", dev); //checking nvram partition and correcting errors
122        // detect jffs
123        /*
124         * FILE *fp=fopen(dev,"rb"); int nojffs=0; if (getc(fp)!=0x85)nojffs=1;
125         * if (getc(fp)!=0x19)nojffs=1; fclose(fp);
126         */
127        // eval("mtd","erase","mtd0");
128
129        if (mount(dev, "/usr/local", "ext2", MS_MGC_VAL, NULL)) {
130                eval("/sbin/mke2fs", "-F", "-b", "1024", dev);
131                mount(dev, "/usr/local", "ext2", MS_MGC_VAL, NULL);
132                eval("/bin/tar", "-xvvjf", "/etc/local.tar.bz2", "-C", "/");
133        }
134        eval("mkdir", "-p", "/usr/local/nvram");
135
136        FILE *in = fopen("/usr/local/nvram/nvram.db", "rb");
137
138        if (in != NULL) {
139                fclose(in);
140                eval("mkdir", "/tmp/nvram");
141                eval("cp", "/etc/nvram/nvram.db", "/tmp/nvram");
142                eval("cp", "/etc/nvram/offsets.db", "/tmp/nvram");
143                eval("/usr/sbin/convertnvram");
144                nvram_commit();
145                eval("rm", "-f", "/etc/nvram/nvram.db");
146                eval("rm", "-f", "/etc/nvram/offsets.db");
147        }
148        eval("mkdir", "/tmp/www");
149
150        unlink("/tmp/nvram/.lock");
151        eval("mkdir", "/tmp/nvram");
152
153        cprintf("sysinit() var\n");
154
155        /*
156         * /var
157         */
158        mkdir("/tmp/var", 0777);
159        mkdir("/var/lock", 0777);
160        mkdir("/var/log", 0777);
161        mkdir("/var/run", 0777);
162        mkdir("/var/tmp", 0777);
163
164        if (!nvram_match("disable_watchdog", "1"))
165                eval("watchdog");       // system watchdog
166
167        cprintf("sysinit() setup console\n");
168
169        /*
170         * Setup console
171         */
172
173        cprintf("sysinit() klogctl\n");
174        klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel")));
175        cprintf("sysinit() get router\n");
176
177        /*
178         * Modules
179         */
180        uname(&name);
181
182        /*
183         * eval("insmod","md5"); eval("insmod","aes"); eval("insmod","blowfish");
184         * eval("insmod","deflate"); eval("insmod","des");
185         * eval("insmod","michael_mic"); eval("insmod","cast5");
186         * eval("insmod","crypto_null");
187         */
188
189        detect_ethernet_devices();
190        eval("ifconfig", "eth0", "0.0.0.0", "up");
191        eval("ifconfig", "eth1", "0.0.0.0", "up");
192        eval("ifconfig", "eth2", "0.0.0.0", "up");
193        eval("ifconfig", "eth3", "0.0.0.0", "up");
194
195        struct ifreq ifr;
196        int s;
197
198        if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) {
199                char eabuf[32];
200
201                strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
202                ioctl(s, SIOCGIFHWADDR, &ifr);
203                nvram_set("et0macaddr_safe",
204                          ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data,
205                                     eabuf));
206                close(s);
207        }
208#ifndef HAVE_NOWIFI
209        insmod("ath_hal");
210        if (nvram_get("rate_control") != NULL) {
211                char rate[64];
212
213                sprintf(rate, "ratectl=%s", nvram_safe_get("rate_control"));
214                eval("insmod", "ath_pci", rate);
215        } else {
216                insmod("ath_pci");
217        }
218#ifdef HAVE_MADWIFI_MIMO
219        fprintf(stderr, "load ATH 802.11n Driver\n");
220        insmod("/lib/80211n/ath_mimo_hal.ko");
221        if (nvram_get("rate_control") != NULL) {
222                char rate[64];
223
224                sprintf(rate, "ratectl=%s", nvram_safe_get("rate_control"));
225                insmod("/lib/80211n/ath_mimo_pci.ko");
226                eval("insmod", "ath_mimo_pci");
227        } else {
228                insmod("/lib/80211n/ath_mimo_pci.ko");
229        }
230#endif
231
232        /*
233         * eval ("ifconfig", "wifi0", "up"); eval ("ifconfig", "wifi1", "up");
234         * eval ("ifconfig", "wifi2", "up"); eval ("ifconfig", "wifi3", "up");
235         * eval ("ifconfig", "wifi4", "up"); eval ("ifconfig", "wifi5", "up");
236         */
237#endif
238
239        insmod("ipv6");
240        eval("mknod", "/dev/rtc", "c", "253", "0");
241#ifdef HAVE_CPUTEMP
242        // insmod("nsc_gpio");
243        // insmod("scx200_gpio");
244        // insmod("scx200_i2c");
245        // insmod("scx200_acb");
246        // insmod("lm77");
247#endif
248
249        nvram_set("wl0_ifname", "ath0");
250        eval("mknod", "/dev/crypto", "c", "10", "70");
251        /*
252         * Set a sane date
253         */
254        stime(&tm);
255        cprintf("done\n");
256        return;
257}
258
259int check_cfe_nv(void)
260{
261        nvram_set("portprio_support", "0");
262        return 0;
263}
264
265int check_pmon_nv(void)
266{
267        return 0;
268}
269
270void start_overclocking(void)
271{
272}
273
274void enable_dtag_vlan(int enable)
275{
276
277}
Note: See TracBrowser for help on using the repository browser.