| 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 | |
|---|
| 49 | static int |
|---|
| 50 | detect (char *devicename) |
|---|
| 51 | { |
|---|
| 52 | char devcall[128]; |
|---|
| 53 | int res; |
|---|
| 54 | sprintf (devcall, "/sbin/lspci|/bin/grep \"%s\"|/bin/wc -l", devicename); |
|---|
| 55 | //system(devcall); |
|---|
| 56 | FILE *in = popen (devcall, "rb"); |
|---|
| 57 | fscanf (in, "%d", &res); |
|---|
| 58 | pclose (in); |
|---|
| 59 | return res > 0 ? 1 : 0; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | #ifndef HAVE_TONZE |
|---|
| 63 | #ifndef HAVE_NOP8670 |
|---|
| 64 | |
|---|
| 65 | void |
|---|
| 66 | checkupdate (void) |
|---|
| 67 | { |
|---|
| 68 | int res, res2 = 0; |
|---|
| 69 | FILE *in = |
|---|
| 70 | popen ("/bin/cat /dev/mtdblock/0|/bin/grep NewMedia|wc -l", "rb"); |
|---|
| 71 | fscanf (in, "%d", &res); |
|---|
| 72 | pclose (in); |
|---|
| 73 | if (res == 0) |
|---|
| 74 | { |
|---|
| 75 | in = |
|---|
| 76 | popen ("/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.02\"|wc -l", "rb"); |
|---|
| 77 | fscanf (in, "%d", &res2); |
|---|
| 78 | pclose (in); |
|---|
| 79 | if (res2 == 0) |
|---|
| 80 | { |
|---|
| 81 | in = |
|---|
| 82 | popen ("/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.04\"|wc -l", |
|---|
| 83 | "rb"); |
|---|
| 84 | fscanf (in, "%d", &res2); |
|---|
| 85 | pclose (in); |
|---|
| 86 | if (res2 == 1 || res2 == 7) //7 is the result for debug info enabled reboot builds |
|---|
| 87 | { |
|---|
| 88 | fprintf (stderr, "updating avila type 2 redboot\n"); |
|---|
| 89 | eval ("tar", "-xaf", "/usr/lib/firmware/redboot.tg7", "-C", |
|---|
| 90 | "/tmp"); |
|---|
| 91 | eval ("mtd", "-r", "-f", "write", "/tmp/avila-rb.bin", |
|---|
| 92 | "RedBoot"); |
|---|
| 93 | return; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | if (res == 1) |
|---|
| 98 | { |
|---|
| 99 | in = |
|---|
| 100 | popen ("/bin/cat /dev/mtdblock/0|/bin/grep \"2\\.03\"|wc -l", "rb"); |
|---|
| 101 | fscanf (in, "%d", &res2); |
|---|
| 102 | pclose (in); |
|---|
| 103 | } |
|---|
| 104 | if (res2 == 1) //redboot update is needed |
|---|
| 105 | { |
|---|
| 106 | in = popen ("/bin/dmesg|/bin/grep \"Memory: 64MB\"|wc -l", "rb"); |
|---|
| 107 | fscanf (in, "%d", &res); |
|---|
| 108 | pclose (in); |
|---|
| 109 | if (res == 1) |
|---|
| 110 | res2 = 64; |
|---|
| 111 | in = popen ("/bin/dmesg|/bin/grep \"Memory: 32MB\"|wc -l", "rb"); |
|---|
| 112 | fscanf (in, "%d", &res); |
|---|
| 113 | pclose (in); |
|---|
| 114 | if (res == 1) |
|---|
| 115 | res2 = 32; |
|---|
| 116 | in = popen ("/bin/dmesg|/bin/grep \"Memory: 128MB\"|wc -l", "rb"); |
|---|
| 117 | fscanf (in, "%d", &res); |
|---|
| 118 | pclose (in); |
|---|
| 119 | if (res == 1) |
|---|
| 120 | res2 = 128; |
|---|
| 121 | in = popen ("/bin/dmesg|/bin/grep \"Memory: 256MB\"|wc -l", "rb"); |
|---|
| 122 | fscanf (in, "%d", &res); |
|---|
| 123 | pclose (in); |
|---|
| 124 | if (res == 1) |
|---|
| 125 | res2 = 256; |
|---|
| 126 | fprintf (stderr, "updating redboot %d MB\n", res2); |
|---|
| 127 | char fname[64]; |
|---|
| 128 | sprintf (fname, "/tmp/rb-%d.bin", res2); |
|---|
| 129 | eval ("tar", "-xaf", "/usr/lib/firmware/redboot.tg7", "-C", "/tmp"); |
|---|
| 130 | eval ("mtd", "-r", "-f", "write", fname, "RedBoot"); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | #endif |
|---|
| 135 | #endif |
|---|
| 136 | int |
|---|
| 137 | start_sysinit (void) |
|---|
| 138 | { |
|---|
| 139 | struct utsname name; |
|---|
| 140 | struct stat tmp_stat; |
|---|
| 141 | time_t tm = 0; |
|---|
| 142 | unlink ("/etc/nvram/.lock"); |
|---|
| 143 | cprintf ("sysinit() proc\n"); |
|---|
| 144 | /* /proc */ |
|---|
| 145 | mount ("proc", "/proc", "proc", MS_MGC_VAL, NULL); |
|---|
| 146 | // system2 ("/etc/convert"); |
|---|
| 147 | mount ("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL); |
|---|
| 148 | cprintf ("sysinit() tmp\n"); |
|---|
| 149 | |
|---|
| 150 | /* /tmp */ |
|---|
| 151 | mount ("ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL); |
|---|
| 152 | mount ("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL); |
|---|
| 153 | /* eval("mount","/etc/www.fs","/www","-t","squashfs","-o","loop"); |
|---|
| 154 | eval("mount","/etc/modules.fs","/lib/modules","-t","squashfs","-o","loop"); |
|---|
| 155 | eval("mount","/etc/usr.fs","/usr","-t","squashfs","-o","loop"); |
|---|
| 156 | */ |
|---|
| 157 | eval ("mkdir", "/tmp/www"); |
|---|
| 158 | eval ("mknod", "/dev/gpio", "c", "127", "0"); |
|---|
| 159 | eval ("mknod", "/dev/nvram", "c", "229", "0"); |
|---|
| 160 | eval ("mknod", "/dev/ppp", "c", "108", "0"); |
|---|
| 161 | eval ("mknod", "/dev/rtc", "c", "254", "0"); |
|---|
| 162 | eval ("mknod", "/dev/crypto", "c", "10", "70"); |
|---|
| 163 | eval ("mount", "-o", "remount,rw", "/"); |
|---|
| 164 | |
|---|
| 165 | unlink ("/tmp/nvram/.lock"); |
|---|
| 166 | eval ("mkdir", "/tmp/nvram"); |
|---|
| 167 | //#ifdef HAVE_REGISTER |
|---|
| 168 | //#else |
|---|
| 169 | // eval ("/bin/tar", "-xzf", "/dev/mtdblock/3", "-C", "/"); |
|---|
| 170 | //#endif |
|---|
| 171 | //mkdir ("/usr/local/nvram", 0777); |
|---|
| 172 | //unlink ("/tmp/nvram/.lock"); |
|---|
| 173 | //eval ("mkdir", "/tmp/nvram"); |
|---|
| 174 | //eval ("cp", "/etc/nvram/nvram.db", "/tmp/nvram"); |
|---|
| 175 | //eval ("cp", "/etc/nvram/offsets.db", "/tmp/nvram"); |
|---|
| 176 | cprintf ("sysinit() var\n"); |
|---|
| 177 | |
|---|
| 178 | /* /var */ |
|---|
| 179 | mkdir ("/tmp/var", 0777); |
|---|
| 180 | mkdir ("/var/lock", 0777); |
|---|
| 181 | mkdir ("/var/log", 0777); |
|---|
| 182 | mkdir ("/var/run", 0777); |
|---|
| 183 | mkdir ("/var/tmp", 0777); |
|---|
| 184 | |
|---|
| 185 | eval ("/sbin/watchdog"); // system watchdog |
|---|
| 186 | |
|---|
| 187 | cprintf ("sysinit() setup console\n"); |
|---|
| 188 | |
|---|
| 189 | /* Setup console */ |
|---|
| 190 | |
|---|
| 191 | cprintf ("sysinit() klogctl\n"); |
|---|
| 192 | klogctl (8, NULL, atoi (nvram_safe_get ("console_loglevel"))); |
|---|
| 193 | cprintf ("sysinit() get router\n"); |
|---|
| 194 | |
|---|
| 195 | /* Modules */ |
|---|
| 196 | uname (&name); |
|---|
| 197 | #ifndef HAVE_TONZE |
|---|
| 198 | #ifndef HAVE_NOP8670 |
|---|
| 199 | checkupdate (); |
|---|
| 200 | #endif |
|---|
| 201 | #endif |
|---|
| 202 | nvram_set ("intel_eth", "0"); |
|---|
| 203 | if (detect ("82541")) // Intel Gigabit |
|---|
| 204 | { |
|---|
| 205 | nvram_set ("intel_eth", "1"); |
|---|
| 206 | eval ("insmod", "e1000"); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | //system("/etc/kendin"); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | #if 1 |
|---|
| 214 | eval ("insmod", "ixp400th"); |
|---|
| 215 | eval ("insmod", "ixp400"); |
|---|
| 216 | system2 ("cat /usr/lib/firmware/IxNpeMicrocode.dat > /dev/IxNpe"); |
|---|
| 217 | eval ("insmod", "ixp400_eth"); |
|---|
| 218 | eval ("ifconfig", "ixp0", "0.0.0.0", "up"); |
|---|
| 219 | eval ("ifconfig", "ixp1", "0.0.0.0", "up"); |
|---|
| 220 | /* if (getRouterBrand () == ROUTER_BOARD_GATEWORX_GW2345) //lets load the spi drivers for this switch |
|---|
| 221 | { |
|---|
| 222 | eval ("insmod", "spi-algo-bit"); |
|---|
| 223 | eval ("insmod", "spi-ixp4xx"); |
|---|
| 224 | eval ("insmod", "ks8995m"); |
|---|
| 225 | sleep (1); |
|---|
| 226 | system ("echo R01=01 > /proc/driver/KS8995M"); // enable switch |
|---|
| 227 | } */ |
|---|
| 228 | eval ("insmod", "ocf"); |
|---|
| 229 | eval ("insmod", "cryptodev"); |
|---|
| 230 | // eval ("insmod", "ixp4xx", "init_crypto=0"); |
|---|
| 231 | #else |
|---|
| 232 | // eval ("mknod", "/dev/IxNpe","c","10","184"); |
|---|
| 233 | system2 ("cat /usr/lib/firmware/NPE-B > /dev/misc/ixp4xx_ucode"); |
|---|
| 234 | system2 ("cat /usr/lib/firmware/NPE-C > /dev/misc/ixp4xx_ucode"); |
|---|
| 235 | #endif |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | eval ("insmod", "ath_hal"); |
|---|
| 243 | eval ("insmod", "ath_pci"); |
|---|
| 244 | |
|---|
| 245 | #ifdef HAVE_MADWIFI_MIMO |
|---|
| 246 | eval ("insmod", "ath_mimo_pci"); |
|---|
| 247 | #endif |
|---|
| 248 | // eval ("insmod", "ath_pci", "rfkill=0", "autocreate=none"); |
|---|
| 249 | |
|---|
| 250 | if (ifexists ("wifi0")) |
|---|
| 251 | eval ("ifconfig", "wifi0", "up"); |
|---|
| 252 | if (ifexists ("wifi1")) |
|---|
| 253 | eval ("ifconfig", "wifi1", "up"); |
|---|
| 254 | if (ifexists ("wifi2")) |
|---|
| 255 | eval ("ifconfig", "wifi2", "up"); |
|---|
| 256 | if (ifexists ("wifi3")) |
|---|
| 257 | eval ("ifconfig", "wifi3", "up"); |
|---|
| 258 | if (ifexists ("wifi4")) |
|---|
| 259 | eval ("ifconfig", "wifi4", "up"); |
|---|
| 260 | if (ifexists ("wifi5")) |
|---|
| 261 | eval ("ifconfig", "wifi5", "up"); |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | //eval ("insmod", "ipv6"); |
|---|
| 265 | |
|---|
| 266 | eval ("insmod", "ad7418"); // temp / voltage sensor |
|---|
| 267 | /* |
|---|
| 268 | Configure mac addresses by reading data from eeprom |
|---|
| 269 | */ |
|---|
| 270 | // char *filename = "/sys/devices/platform/IXP4XX-I2C.0/i2c-0/0-0051/eeprom"; /* bank2=0x100 */ |
|---|
| 271 | #ifdef HAVE_NOP8670 |
|---|
| 272 | char *filename = "/dev/mtdblock/5"; /* bank2=0x100 */ |
|---|
| 273 | FILE *file = fopen (filename, "r"); |
|---|
| 274 | if (file) |
|---|
| 275 | { |
|---|
| 276 | unsigned char buf[16]; |
|---|
| 277 | fseek (file, 0x422, SEEK_SET); |
|---|
| 278 | fread (&buf[0], 6, 1, file); |
|---|
| 279 | char mac[16]; |
|---|
| 280 | sprintf (mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], |
|---|
| 281 | buf[3], buf[4], buf[5]); |
|---|
| 282 | eval ("ifconfig", "ixp0", "hw", "ether", mac); |
|---|
| 283 | fseek (file, 0x43b, SEEK_SET); |
|---|
| 284 | fread (&buf[6], 6, 1, file); |
|---|
| 285 | sprintf (mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8], |
|---|
| 286 | buf[9], buf[10], buf[11]); |
|---|
| 287 | eval ("ifconfig", "ixp1", "hw", "ether", mac); |
|---|
| 288 | fclose (file); |
|---|
| 289 | } |
|---|
| 290 | #else |
|---|
| 291 | char *filename = "/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0051/eeprom"; /* bank2=0x100 */ |
|---|
| 292 | FILE *file = fopen (filename, "r"); |
|---|
| 293 | if (file) |
|---|
| 294 | { |
|---|
| 295 | unsigned char buf[16]; |
|---|
| 296 | fread (&buf[0], 16, 1, file); |
|---|
| 297 | char mac[16]; |
|---|
| 298 | sprintf (mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], |
|---|
| 299 | buf[3], buf[4], buf[5]); |
|---|
| 300 | eval ("ifconfig", "ixp0", "hw", "ether", mac); |
|---|
| 301 | sprintf (mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8], |
|---|
| 302 | buf[9], buf[10], buf[11]); |
|---|
| 303 | eval ("ifconfig", "ixp1", "hw", "ether", mac); |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | fclose (file); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | #endif |
|---|
| 311 | eval ("ifconfig", "ixp0", "0.0.0.0", "up"); |
|---|
| 312 | eval ("ifconfig", "ixp1", "0.0.0.0", "up"); |
|---|
| 313 | if (getRouterBrand () == ROUTER_BOARD_GATEWORX_GW2345) //lets load the spi drivers for this switch |
|---|
| 314 | { |
|---|
| 315 | eval ("insmod", "spi-algo-bit"); |
|---|
| 316 | eval ("insmod", "spi-ixp4xx"); |
|---|
| 317 | eval ("insmod", "ks8995m"); |
|---|
| 318 | sleep (1); |
|---|
| 319 | system ("echo R01=01 > /proc/driver/KS8995M"); // enable switch |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | /* Set a sane date */ |
|---|
| 324 | stime (&tm); |
|---|
| 325 | nvram_set ("wl0_ifname", "ath0"); |
|---|
| 326 | #ifndef HAVE_NOP8670 |
|---|
| 327 | eval ("hwclock", "-s"); |
|---|
| 328 | #endif |
|---|
| 329 | nvram_set ("use_crypto", "0"); |
|---|
| 330 | cprintf ("done\n"); |
|---|
| 331 | eval ("/bin/tar", "-xzf", "/dev/mtdblock/4", "-C", "/"); |
|---|
| 332 | FILE *in = fopen ("/tmp/nvram/nvram.db", "rb"); |
|---|
| 333 | if (in != NULL) |
|---|
| 334 | { |
|---|
| 335 | fclose (in); |
|---|
| 336 | eval ("/usr/sbin/convertnvram"); |
|---|
| 337 | eval ("/usr/sbin/mtd", "erase", "nvram"); |
|---|
| 338 | nvram_commit (); |
|---|
| 339 | } |
|---|
| 340 | return 0; |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | int |
|---|
| 344 | check_cfe_nv (void) |
|---|
| 345 | { |
|---|
| 346 | nvram_set ("portprio_support", "0"); |
|---|
| 347 | return 0; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | int |
|---|
| 351 | check_pmon_nv (void) |
|---|
| 352 | { |
|---|
| 353 | return 0; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | void |
|---|
| 357 | start_overclocking (void) |
|---|
| 358 | { |
|---|
| 359 | } |
|---|
| 360 | void |
|---|
| 361 | enable_dtag_vlan (int enable) |
|---|
| 362 | { |
|---|
| 363 | |
|---|
| 364 | } |
|---|