| 1 | /* |
|---|
| 2 | * sysinit-dir300.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/socket.h> |
|---|
| 38 | #include <sys/sysmacros.h> |
|---|
| 39 | #include <sys/ioctl.h> |
|---|
| 40 | #include <sys/time.h> |
|---|
| 41 | #include <sys/utsname.h> |
|---|
| 42 | #include <sys/wait.h> |
|---|
| 43 | #include <linux/if_ether.h> |
|---|
| 44 | #include <linux/mii.h> |
|---|
| 45 | #include <linux/sockios.h> |
|---|
| 46 | #include <net/if.h> |
|---|
| 47 | |
|---|
| 48 | #include <arpa/inet.h> |
|---|
| 49 | #include <sys/socket.h> |
|---|
| 50 | #include <linux/sockios.h> |
|---|
| 51 | #include <linux/mii.h> |
|---|
| 52 | |
|---|
| 53 | #include <bcmnvram.h> |
|---|
| 54 | #include <shutils.h> |
|---|
| 55 | #include <utils.h> |
|---|
| 56 | #include <cymac.h> |
|---|
| 57 | #include "devices/wireless.c" |
|---|
| 58 | |
|---|
| 59 | extern void vlan_init(int num); |
|---|
| 60 | |
|---|
| 61 | void start_sysinit(void) |
|---|
| 62 | { |
|---|
| 63 | char buf[PATH_MAX]; |
|---|
| 64 | struct stat tmp_stat; |
|---|
| 65 | time_t tm = 0; |
|---|
| 66 | |
|---|
| 67 | if (!nvram_match("disable_watchdog", "1")) |
|---|
| 68 | eval("watchdog"); |
|---|
| 69 | /* |
|---|
| 70 | * Setup console |
|---|
| 71 | */ |
|---|
| 72 | |
|---|
| 73 | cprintf("sysinit() klogctl\n"); |
|---|
| 74 | klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel"))); |
|---|
| 75 | cprintf("sysinit() get router\n"); |
|---|
| 76 | |
|---|
| 77 | #ifndef HAVE_DIR400 |
|---|
| 78 | int mtd = getMTD("board_config"); |
|---|
| 79 | char mtdpath[64]; |
|---|
| 80 | |
|---|
| 81 | sprintf(mtdpath, "/dev/mtdblock/%d", mtd); |
|---|
| 82 | FILE *fp = fopen(mtdpath, "rb"); |
|---|
| 83 | |
|---|
| 84 | if (fp) { |
|---|
| 85 | fseek(fp, 0x1000, SEEK_SET); |
|---|
| 86 | unsigned int test; |
|---|
| 87 | |
|---|
| 88 | fread(&test, 4, 1, fp); |
|---|
| 89 | fprintf(stderr, "test pattern is %X\n", test); |
|---|
| 90 | if (test != 0xffffffff) { |
|---|
| 91 | fprintf(stderr, |
|---|
| 92 | "radio config fixup is required to clean bad stuff out of memory, otherwise the radio config cannot be detected\n"); |
|---|
| 93 | fseek(fp, 0, SEEK_SET); |
|---|
| 94 | char *block = (char *)malloc(65536); |
|---|
| 95 | |
|---|
| 96 | fread(block, 65536, 1, fp); |
|---|
| 97 | fclose(fp); |
|---|
| 98 | int i; |
|---|
| 99 | |
|---|
| 100 | for (i = 0x1000; i < 65536; i++) |
|---|
| 101 | block[i] = 0xff; |
|---|
| 102 | fp = fopen("/tmp/radio", "wb"); |
|---|
| 103 | fwrite(block, 65536, 1, fp); |
|---|
| 104 | eval("mtd", "-f", "write", "/tmp/radio", "board_config"); // writes |
|---|
| 105 | // |
|---|
| 106 | // back |
|---|
| 107 | // new |
|---|
| 108 | // config |
|---|
| 109 | // and |
|---|
| 110 | // reboots |
|---|
| 111 | eval("event", "5", "1", "15"); |
|---|
| 112 | } |
|---|
| 113 | fclose(fp); |
|---|
| 114 | } |
|---|
| 115 | #else |
|---|
| 116 | if (!nvram_match("dir400preconfig", "1")) { |
|---|
| 117 | nvram_set("dir400preconfig", "1"); |
|---|
| 118 | nvram_commit(); |
|---|
| 119 | int mtd = getMTD("fullflash"); |
|---|
| 120 | char mtdpath[64]; |
|---|
| 121 | char mac[18]; |
|---|
| 122 | |
|---|
| 123 | sprintf(mtdpath, "/dev/mtdblock/%d", mtd); |
|---|
| 124 | FILE *fp = fopen(mtdpath, "rb"); |
|---|
| 125 | int s = searchfor(fp, "lan_mac=", 512); |
|---|
| 126 | |
|---|
| 127 | if (s == -1) { |
|---|
| 128 | fprintf(stderr, "no mac found in config\n"); |
|---|
| 129 | fclose(fp); |
|---|
| 130 | } else { |
|---|
| 131 | mac[17] = 0; |
|---|
| 132 | fread(mac, 17, 1, fp); |
|---|
| 133 | fclose(fp); |
|---|
| 134 | mtd = getMTD("board_config"); |
|---|
| 135 | sprintf(mtdpath, "/dev/mtdblock/%d", mtd); |
|---|
| 136 | fp = fopen(mtdpath, "rb"); |
|---|
| 137 | fseek(fp, 0, SEEK_SET); |
|---|
| 138 | char *block = (char *)malloc(65536); |
|---|
| 139 | |
|---|
| 140 | fread(block, 65536, 1, fp); |
|---|
| 141 | fclose(fp); |
|---|
| 142 | unsigned char in_addr[6]; |
|---|
| 143 | int changed = 0; |
|---|
| 144 | |
|---|
| 145 | ether_atoe(mac, &in_addr[0]); |
|---|
| 146 | if (memcmp(block + 96, &in_addr[0], 6)) { |
|---|
| 147 | changed++; |
|---|
| 148 | memcpy(block + 96, &in_addr[0], 6); // wlan mac |
|---|
| 149 | } |
|---|
| 150 | in_addr[5]++; |
|---|
| 151 | if (memcmp(block + 102, &in_addr[0], 6)) { |
|---|
| 152 | changed++; |
|---|
| 153 | memcpy(block + 102, &in_addr[0], 6); // eth0 mac |
|---|
| 154 | } |
|---|
| 155 | in_addr[5]++; |
|---|
| 156 | if (memcmp(block + 108, &in_addr[0], 6)) { |
|---|
| 157 | changed++; |
|---|
| 158 | memcpy(block + 108, &in_addr[0], 6); // eth1 mac |
|---|
| 159 | } |
|---|
| 160 | in_addr[5]++; |
|---|
| 161 | if (memcmp(block + 118, &in_addr[0], 6)) { |
|---|
| 162 | changed++; |
|---|
| 163 | memcpy(block + 118, &in_addr[0], 6); // wlan1 mac |
|---|
| 164 | } |
|---|
| 165 | if (changed) { |
|---|
| 166 | fprintf(stderr, |
|---|
| 167 | "radio config needs to be adjusted, system will reboot after flashing\n"); |
|---|
| 168 | fp = fopen("/tmp/radio", "wb"); |
|---|
| 169 | fwrite(block, 65536, 1, fp); |
|---|
| 170 | fclose(fp); |
|---|
| 171 | eval("mtd", "-f", "write", "/tmp/radio", "board_config"); // writes |
|---|
| 172 | // |
|---|
| 173 | // back |
|---|
| 174 | // new |
|---|
| 175 | // config |
|---|
| 176 | // and |
|---|
| 177 | // reboots |
|---|
| 178 | eval("event", "5", "1", "15"); |
|---|
| 179 | } else { |
|---|
| 180 | fprintf(stderr, |
|---|
| 181 | "no change required, radio config remains unchanged\n"); |
|---|
| 182 | } |
|---|
| 183 | free(block); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | #endif |
|---|
| 187 | |
|---|
| 188 | /* |
|---|
| 189 | * network drivers |
|---|
| 190 | */ |
|---|
| 191 | #ifdef HAVE_HOTPLUG2 |
|---|
| 192 | insmod("ar231x"); |
|---|
| 193 | #else |
|---|
| 194 | insmod("ar2313"); |
|---|
| 195 | #endif |
|---|
| 196 | detect_wireless_devices(); |
|---|
| 197 | // eval ("ifconfig", "wifi0", "up"); |
|---|
| 198 | eval("ifconfig", "eth0", "up"); // wan |
|---|
| 199 | writeproc("/proc/sys/dev/wifi0/ledpin","2"); |
|---|
| 200 | writeproc("/proc/sys/dev/wifi0/softled","1"); |
|---|
| 201 | if (getRouterBrand() == ROUTER_BOARD_FONERA2200) { |
|---|
| 202 | eval("ifconfig", "eth0", "up", "promisc"); // required for vlan config |
|---|
| 203 | eval("/sbin/vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD"); |
|---|
| 204 | eval("/sbin/vconfig", "add", "eth0", "0"); |
|---|
| 205 | eval("/sbin/vconfig", "add", "eth0", "1"); |
|---|
| 206 | struct ifreq ifr; |
|---|
| 207 | int s; |
|---|
| 208 | |
|---|
| 209 | if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) { |
|---|
| 210 | char eabuf[32]; |
|---|
| 211 | |
|---|
| 212 | strncpy(ifr.ifr_name, "eth0", IFNAMSIZ); |
|---|
| 213 | ioctl(s, SIOCGIFHWADDR, &ifr); |
|---|
| 214 | char macaddr[32]; |
|---|
| 215 | |
|---|
| 216 | strcpy(macaddr, |
|---|
| 217 | ether_etoa((unsigned char *)ifr.ifr_hwaddr. |
|---|
| 218 | sa_data, eabuf)); |
|---|
| 219 | nvram_set("et0macaddr", macaddr); |
|---|
| 220 | nvram_set("et0macaddr_safe", macaddr); |
|---|
| 221 | // MAC_ADD( macaddr ); |
|---|
| 222 | ether_atoe(macaddr, |
|---|
| 223 | (unsigned char *)ifr.ifr_hwaddr.sa_data); |
|---|
| 224 | strncpy(ifr.ifr_name, "vlan1", IFNAMSIZ); |
|---|
| 225 | ioctl(s, SIOCSIFHWADDR, &ifr); |
|---|
| 226 | close(s); |
|---|
| 227 | } |
|---|
| 228 | } else { |
|---|
| 229 | // system("swconfig dev eth0 set reset 1"); |
|---|
| 230 | // system("swconfig dev eth0 set enable_vlan 1"); |
|---|
| 231 | #ifdef HAVE_SWCONFIG |
|---|
| 232 | system("swconfig dev eth0 vlan 1 set ports \"0 1 2 3 5t\""); |
|---|
| 233 | system("swconfig dev eth0 vlan 2 set ports \"4 5t\""); |
|---|
| 234 | system("swconfig dev eth0 set apply"); |
|---|
| 235 | eval("vconfig", "set_name_type", "VLAN_PLUS_VID_NO_PAD"); |
|---|
| 236 | eval("vconfig", "add", "eth0", "1"); |
|---|
| 237 | eval("vconfig", "add", "eth0", "2"); |
|---|
| 238 | // set network.eth0_1.ports="0 1 2 3 5t" |
|---|
| 239 | #else |
|---|
| 240 | vlan_init(0xff); // 4 lan + 1 wan |
|---|
| 241 | #endif |
|---|
| 242 | struct ifreq ifr; |
|---|
| 243 | int s; |
|---|
| 244 | |
|---|
| 245 | if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) { |
|---|
| 246 | char eabuf[32]; |
|---|
| 247 | |
|---|
| 248 | strncpy(ifr.ifr_name, "eth0", IFNAMSIZ); |
|---|
| 249 | ioctl(s, SIOCGIFHWADDR, &ifr); |
|---|
| 250 | char macaddr[32]; |
|---|
| 251 | |
|---|
| 252 | strcpy(macaddr, |
|---|
| 253 | ether_etoa((unsigned char *)ifr.ifr_hwaddr. |
|---|
| 254 | sa_data, eabuf)); |
|---|
| 255 | nvram_set("et0macaddr", macaddr); |
|---|
| 256 | nvram_set("et0macaddr_safe", macaddr); |
|---|
| 257 | // MAC_ADD (macaddr); |
|---|
| 258 | ether_atoe(macaddr, |
|---|
| 259 | (unsigned char *)ifr.ifr_hwaddr.sa_data); |
|---|
| 260 | strncpy(ifr.ifr_name, "vlan2", IFNAMSIZ); |
|---|
| 261 | ioctl(s, SIOCSIFHWADDR, &ifr); |
|---|
| 262 | close(s); |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /* |
|---|
| 267 | * Set a sane date |
|---|
| 268 | */ |
|---|
| 269 | stime(&tm); |
|---|
| 270 | nvram_set("wl0_ifname", "ath0"); |
|---|
| 271 | |
|---|
| 272 | return; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | int check_cfe_nv(void) |
|---|
| 276 | { |
|---|
| 277 | nvram_set("portprio_support", "0"); |
|---|
| 278 | return 0; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | int check_pmon_nv(void) |
|---|
| 282 | { |
|---|
| 283 | return 0; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | void start_overclocking(void) |
|---|
| 287 | { |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | void enable_dtag_vlan(int enable) |
|---|
| 291 | { |
|---|
| 292 | |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | void start_fixboard(void) |
|---|
| 296 | { |
|---|
| 297 | int mtd = getMTD("board_config"); |
|---|
| 298 | char mtdpath[64]; |
|---|
| 299 | |
|---|
| 300 | sprintf(mtdpath, "/dev/mtdblock/%d", mtd); |
|---|
| 301 | fprintf(stderr, "board config path = %s\n", mtdpath); |
|---|
| 302 | FILE *fp = fopen(mtdpath, "rb"); |
|---|
| 303 | |
|---|
| 304 | if (fp) { |
|---|
| 305 | fseek(fp, 0x1000, SEEK_SET); |
|---|
| 306 | unsigned int test; |
|---|
| 307 | |
|---|
| 308 | fread(&test, 4, 1, fp); |
|---|
| 309 | fprintf(stderr, "test pattern is %X\n", test); |
|---|
| 310 | if (test != 0xffffffff) { |
|---|
| 311 | fprintf(stderr, |
|---|
| 312 | "radio config fixup is required to clean bad stuff out of memory, otherwise the radio config cannot be detected\n"); |
|---|
| 313 | fseek(fp, 0, SEEK_SET); |
|---|
| 314 | char *block = (char *)malloc(65536); |
|---|
| 315 | |
|---|
| 316 | fread(block, 65536, 1, fp); |
|---|
| 317 | fclose(fp); |
|---|
| 318 | int i; |
|---|
| 319 | |
|---|
| 320 | for (i = 0x1000; i < 65536; i++) |
|---|
| 321 | block[i] = 0xff; |
|---|
| 322 | fp = fopen("/tmp/radio", "wb"); |
|---|
| 323 | fwrite(block, 65536, 1, fp); |
|---|
| 324 | eval("mtd", "-f", "write", "/tmp/radio", "board_config"); // writes |
|---|
| 325 | // |
|---|
| 326 | // back |
|---|
| 327 | // new |
|---|
| 328 | // config |
|---|
| 329 | // and |
|---|
| 330 | // reboots |
|---|
| 331 | eval("event", "5", "1", "15"); |
|---|
| 332 | } |
|---|
| 333 | fclose(fp); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | } |
|---|