| 1 | /* |
|---|
| 2 | * sysinit-whrag108.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 | #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 <sys/ioctl.h> |
|---|
| 42 | |
|---|
| 43 | #include <bcmnvram.h> |
|---|
| 44 | #include <shutils.h> |
|---|
| 45 | #include <utils.h> |
|---|
| 46 | |
|---|
| 47 | #define SIOCGMIIREG 0x8948 /* Read MII PHY register. */ |
|---|
| 48 | #define SIOCSMIIREG 0x8949 /* Write MII PHY register. */ |
|---|
| 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 | |
|---|
| 55 | //highly experimental |
|---|
| 56 | |
|---|
| 57 | void |
|---|
| 58 | setRegister (int socket, short reg, short value) |
|---|
| 59 | { |
|---|
| 60 | // struct mii_ioctl_data data; |
|---|
| 61 | struct ifreq ifr; |
|---|
| 62 | unsigned short *data = (unsigned short *) (&ifr.ifr_data); |
|---|
| 63 | data[0] = 0; |
|---|
| 64 | data[1] = reg; |
|---|
| 65 | data[2] = value; |
|---|
| 66 | (void) strncpy (ifr.ifr_name, "eth0", sizeof ("eth0")); |
|---|
| 67 | // data.reg_num = reg; |
|---|
| 68 | // data.val_in = value; |
|---|
| 69 | ioctl (socket, SIOCSMIIREG, &ifr); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void |
|---|
| 73 | switch_main (int argc, char *argv[]) |
|---|
| 74 | { |
|---|
| 75 | int reg = atoi (argv[1]); |
|---|
| 76 | int val = atoi (argv[2]); |
|---|
| 77 | int s = socket (AF_INET, SOCK_DGRAM, 0); |
|---|
| 78 | if (s < 0) |
|---|
| 79 | { |
|---|
| 80 | return; |
|---|
| 81 | } |
|---|
| 82 | setRegister (s, reg, val); |
|---|
| 83 | close (s); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void |
|---|
| 87 | setupSwitch (void) |
|---|
| 88 | { |
|---|
| 89 | int s = socket (AF_INET, SOCK_DGRAM, 0); |
|---|
| 90 | if (s < 0) |
|---|
| 91 | { |
|---|
| 92 | return; |
|---|
| 93 | } |
|---|
| 94 | // setRegister(s,0x02,0xa0); |
|---|
| 95 | |
|---|
| 96 | //Enable 8021Q (80) and IGMP snooping (40) |
|---|
| 97 | //setRegister(s,0x05,0xa0); |
|---|
| 98 | //vlan1: valid,5,2,1 port fid=1 vid=1 |
|---|
| 99 | // setRegister(s,0x76,0x21); |
|---|
| 100 | // setRegister(s,0x77,0x10); |
|---|
| 101 | // setRegister(s,0x78,0x01); |
|---|
| 102 | //write (04) and trigger address 0 |
|---|
| 103 | // setRegister(s,0x6E,0x04); |
|---|
| 104 | // setRegister(s,0x6F,0x00); |
|---|
| 105 | //vlan2: valid,5,4,3 port fid=2 vid=2 |
|---|
| 106 | // setRegister(s,0x76,0x3E); |
|---|
| 107 | // setRegister(s,0x77,0x20); |
|---|
| 108 | // setRegister(s,0x78,0x02); |
|---|
| 109 | |
|---|
| 110 | //write (04) and trigger address 0 |
|---|
| 111 | // setRegister(s,0x6E,0x04); |
|---|
| 112 | // setRegister(s,0x6F,0x01); |
|---|
| 113 | |
|---|
| 114 | //config port 1,2 to VLAN id 1 |
|---|
| 115 | setRegister (s, 0x14, 0x01); |
|---|
| 116 | //config port 1,2 to filter vid 1 |
|---|
| 117 | setRegister (s, 0x12, 0x46); |
|---|
| 118 | |
|---|
| 119 | //config port 3,4 to VLAN id 2 |
|---|
| 120 | setRegister (s, 0x24, 0x02); |
|---|
| 121 | setRegister (s, 0x34, 0x02); |
|---|
| 122 | setRegister (s, 0x44, 0x02); |
|---|
| 123 | setRegister (s, 0x54, 0x02); |
|---|
| 124 | //config port 3,4 to filter vid 2 |
|---|
| 125 | setRegister (s, 0x22, 0x46); |
|---|
| 126 | setRegister (s, 0x32, 0x46); |
|---|
| 127 | setRegister (s, 0x42, 0x46); |
|---|
| 128 | setRegister (s, 0x52, 0x46); |
|---|
| 129 | |
|---|
| 130 | //for IGMP, disenable special tagging |
|---|
| 131 | // setRegister(s,0x0b,0x01); |
|---|
| 132 | //enable vlan tag insertion por 5 |
|---|
| 133 | // setRegister(s,0x50,0x04); |
|---|
| 134 | // setRegister(s,0x52,0x06); |
|---|
| 135 | //remove it from all others |
|---|
| 136 | setRegister (s, 0x10, 0x02); |
|---|
| 137 | setRegister (s, 0x20, 0x02); |
|---|
| 138 | setRegister (s, 0x30, 0x02); |
|---|
| 139 | setRegister (s, 0x40, 0x02); |
|---|
| 140 | setRegister (s, 0x50, 0x02); |
|---|
| 141 | //switch enable |
|---|
| 142 | setRegister (s, 0x01, 0x01); |
|---|
| 143 | close (s); |
|---|
| 144 | |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | int |
|---|
| 148 | start_sysinit (void) |
|---|
| 149 | { |
|---|
| 150 | char buf[PATH_MAX]; |
|---|
| 151 | struct utsname name; |
|---|
| 152 | struct stat tmp_stat; |
|---|
| 153 | time_t tm = 0; |
|---|
| 154 | unlink ("/etc/nvram/.lock"); |
|---|
| 155 | cprintf ("sysinit() proc\n"); |
|---|
| 156 | /* /proc */ |
|---|
| 157 | mount ("proc", "/proc", "proc", MS_MGC_VAL, NULL); |
|---|
| 158 | mount ("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL); |
|---|
| 159 | cprintf ("sysinit() tmp\n"); |
|---|
| 160 | |
|---|
| 161 | /* /tmp */ |
|---|
| 162 | mount ("ramfs", "/tmp", "ramfs", MS_MGC_VAL, NULL); |
|---|
| 163 | // fix for linux kernel 2.6 |
|---|
| 164 | mount ("devpts", "/dev/pts", "devpts", MS_MGC_VAL, NULL); |
|---|
| 165 | eval ("mkdir", "/tmp/www"); |
|---|
| 166 | eval ("mknod", "/dev/nvram", "c", "229", "0"); |
|---|
| 167 | eval ("mknod", "/dev/ppp", "c", "108", "0"); |
|---|
| 168 | |
|---|
| 169 | unlink ("/tmp/nvram/.lock"); |
|---|
| 170 | eval ("mkdir", "/tmp/nvram"); |
|---|
| 171 | eval ("/bin/tar", "-xzf", "/dev/mtdblock/3", "-C", "/"); |
|---|
| 172 | FILE *in = fopen ("/tmp/nvram/nvram.db", "rb"); |
|---|
| 173 | if (in != NULL) |
|---|
| 174 | { |
|---|
| 175 | fclose (in); |
|---|
| 176 | eval ("/usr/sbin/convertnvram"); |
|---|
| 177 | eval ("/usr/sbin/mtd", "erase", "nvram"); |
|---|
| 178 | nvram_commit (); |
|---|
| 179 | } |
|---|
| 180 | cprintf ("sysinit() var\n"); |
|---|
| 181 | |
|---|
| 182 | /* /var */ |
|---|
| 183 | mkdir ("/tmp/var", 0777); |
|---|
| 184 | mkdir ("/var/lock", 0777); |
|---|
| 185 | mkdir ("/var/log", 0777); |
|---|
| 186 | mkdir ("/var/run", 0777); |
|---|
| 187 | mkdir ("/var/tmp", 0777); |
|---|
| 188 | cprintf ("sysinit() setup console\n"); |
|---|
| 189 | eval ("/sbin/watchdog"); |
|---|
| 190 | /* Setup console */ |
|---|
| 191 | |
|---|
| 192 | cprintf ("sysinit() klogctl\n"); |
|---|
| 193 | klogctl (8, NULL, atoi (nvram_safe_get ("console_loglevel"))); |
|---|
| 194 | cprintf ("sysinit() get router\n"); |
|---|
| 195 | |
|---|
| 196 | int brand = getRouterBrand (); |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | /* Modules */ |
|---|
| 200 | uname (&name); |
|---|
| 201 | |
|---|
| 202 | /* network drivers */ |
|---|
| 203 | eval ("insmod", "ar2313"); |
|---|
| 204 | |
|---|
| 205 | eval ("insmod", "ath_hal"); |
|---|
| 206 | eval ("insmod", "ath_ahb"); |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | system ("echo 2 >/proc/sys/dev/wifi0/ledpin"); |
|---|
| 210 | system ("echo 1 >/proc/sys/dev/wifi0/softled"); |
|---|
| 211 | system ("echo 3 >/proc/sys/dev/wifi1/ledpin"); |
|---|
| 212 | system ("echo 1 >/proc/sys/dev/wifi1/softled"); |
|---|
| 213 | |
|---|
| 214 | eval ("ifconfig", "wifi0", "up"); |
|---|
| 215 | eval ("ifconfig", "wifi1", "up"); |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | eval ("insmod", "ipv6"); |
|---|
| 220 | |
|---|
| 221 | /* Set a sane date */ |
|---|
| 222 | stime (&tm); |
|---|
| 223 | nvram_set ("wl0_ifname", "ath0"); |
|---|
| 224 | |
|---|
| 225 | return 0; |
|---|
| 226 | cprintf ("done\n"); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | int |
|---|
| 230 | check_cfe_nv (void) |
|---|
| 231 | { |
|---|
| 232 | nvram_set ("portprio_support", "0"); |
|---|
| 233 | return 0; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | int |
|---|
| 237 | check_pmon_nv (void) |
|---|
| 238 | { |
|---|
| 239 | return 0; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | void |
|---|
| 243 | start_overclocking (void) |
|---|
| 244 | { |
|---|
| 245 | } |
|---|