| 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 | #include "devices/wireless.c" |
|---|
| 56 | |
|---|
| 57 | #define sys_reboot() eval("sync"); eval("event","3","1","15") |
|---|
| 58 | |
|---|
| 59 | static int getdiscindex(void) // works only for squashfs |
|---|
| 60 | { |
|---|
| 61 | int i; |
|---|
| 62 | |
|---|
| 63 | for (i = 0; i < 10; i++) { |
|---|
| 64 | char dev[64]; |
|---|
| 65 | |
|---|
| 66 | sprintf(dev, "/dev/discs/disc%d/part2", i); |
|---|
| 67 | FILE *in = fopen(dev, "rb"); |
|---|
| 68 | |
|---|
| 69 | if (in == NULL) |
|---|
| 70 | continue; // no second partition or disc does not |
|---|
| 71 | // exist, skipping |
|---|
| 72 | char buf[4]; |
|---|
| 73 | |
|---|
| 74 | fread(buf, 4, 1, in); |
|---|
| 75 | if (buf[0] == 'h' && buf[1] == 's' && buf[2] == 'q' |
|---|
| 76 | && buf[3] == 't') { |
|---|
| 77 | fclose(in); |
|---|
| 78 | // filesystem detected |
|---|
| 79 | return i; |
|---|
| 80 | } |
|---|
| 81 | fclose(in); |
|---|
| 82 | } |
|---|
| 83 | return -1; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void start_sysinit(void) |
|---|
| 87 | { |
|---|
| 88 | time_t tm = 0; |
|---|
| 89 | |
|---|
| 90 | char dev[64]; |
|---|
| 91 | int index = getdiscindex(); |
|---|
| 92 | |
|---|
| 93 | if (index == -1) { |
|---|
| 94 | fprintf(stderr, |
|---|
| 95 | "no valid dd-wrt partition found, calling shell"); |
|---|
| 96 | eval("/bin/sh"); |
|---|
| 97 | exit(0); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | FILE *in = fopen("/usr/local/nvram/nvram.db", "rb"); |
|---|
| 101 | |
|---|
| 102 | if (in != NULL) { |
|---|
| 103 | fclose(in); |
|---|
| 104 | eval("mkdir", "/tmp/nvram"); |
|---|
| 105 | eval("cp", "/etc/nvram/nvram.db", "/tmp/nvram"); |
|---|
| 106 | eval("cp", "/etc/nvram/offsets.db", "/tmp/nvram"); |
|---|
| 107 | eval("/usr/sbin/convertnvram"); |
|---|
| 108 | nvram_commit(); |
|---|
| 109 | eval("rm", "-f", "/etc/nvram/nvram.db"); |
|---|
| 110 | eval("rm", "-f", "/etc/nvram/offsets.db"); |
|---|
| 111 | } |
|---|
| 112 | //recover nvram if available |
|---|
| 113 | in = fopen("/usr/local/nvram/nvram.bin", "rb"); |
|---|
| 114 | if (in == NULL) { |
|---|
| 115 | fprintf(stderr, "recover broken nvram\n"); |
|---|
| 116 | sprintf(dev, "/dev/discs/disc%d/disc", index); |
|---|
| 117 | in = fopen(dev, "rb"); |
|---|
| 118 | fseeko(in, 0, SEEK_END); |
|---|
| 119 | off_t mtdlen = ftello(in); |
|---|
| 120 | fseeko(in, mtdlen-(65536*2), SEEK_SET); |
|---|
| 121 | unsigned char *mem = malloc(65536); |
|---|
| 122 | fread(mem, 65536, 1, in); |
|---|
| 123 | fclose(in); |
|---|
| 124 | if (mem[0] == 0x46 && mem[1] == 0x4c && mem[2] == 0x53 |
|---|
| 125 | && mem[3] == 0x48) { |
|---|
| 126 | fprintf(stderr, "found recovery\n"); |
|---|
| 127 | in = fopen("/usr/local/nvram/nvram.bin", "wb"); |
|---|
| 128 | if (in != NULL) { |
|---|
| 129 | fwrite(mem, 65536, 1, in); |
|---|
| 130 | fclose(in); |
|---|
| 131 | free(mem); |
|---|
| 132 | eval("sync"); |
|---|
| 133 | sleep(5); |
|---|
| 134 | eval("event", "5", "1", "15"); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | free(mem); |
|---|
| 138 | } else { |
|---|
| 139 | fclose(in); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | if (!nvram_match("disable_watchdog", "1")) |
|---|
| 143 | eval("watchdog"); // system watchdog |
|---|
| 144 | #ifdef HAVE_ERC |
|---|
| 145 | if (isregistered_real() && nvram_match("ree_resetme", "1")) { |
|---|
| 146 | fprintf(stderr, "Restoring REE default nvram\n"); |
|---|
| 147 | eval("nvram","restore","/etc/defaults/x86ree.backup"); |
|---|
| 148 | eval("reboot"); |
|---|
| 149 | eval("event", "5", "1", "15"); |
|---|
| 150 | } |
|---|
| 151 | #endif |
|---|
| 152 | |
|---|
| 153 | cprintf("sysinit() setup console\n"); |
|---|
| 154 | |
|---|
| 155 | /* |
|---|
| 156 | * Setup console |
|---|
| 157 | */ |
|---|
| 158 | |
|---|
| 159 | cprintf("sysinit() klogctl\n"); |
|---|
| 160 | klogctl(8, NULL, atoi(nvram_safe_get("console_loglevel"))); |
|---|
| 161 | cprintf("sysinit() get router\n"); |
|---|
| 162 | |
|---|
| 163 | /* |
|---|
| 164 | * eval("insmod","md5"); eval("insmod","aes"); eval("insmod","blowfish"); |
|---|
| 165 | * eval("insmod","deflate"); eval("insmod","des"); |
|---|
| 166 | * eval("insmod","michael_mic"); eval("insmod","cast5"); |
|---|
| 167 | * eval("insmod","crypto_null"); |
|---|
| 168 | */ |
|---|
| 169 | |
|---|
| 170 | detect_ethernet_devices(); |
|---|
| 171 | eval("ifconfig", "eth0", "0.0.0.0", "up"); |
|---|
| 172 | eval("ifconfig", "eth1", "0.0.0.0", "up"); |
|---|
| 173 | eval("ifconfig", "eth2", "0.0.0.0", "up"); |
|---|
| 174 | eval("ifconfig", "eth3", "0.0.0.0", "up"); |
|---|
| 175 | |
|---|
| 176 | struct ifreq ifr; |
|---|
| 177 | int s; |
|---|
| 178 | |
|---|
| 179 | if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))) { |
|---|
| 180 | char eabuf[32]; |
|---|
| 181 | |
|---|
| 182 | strncpy(ifr.ifr_name, "eth0", IFNAMSIZ); |
|---|
| 183 | ioctl(s, SIOCGIFHWADDR, &ifr); |
|---|
| 184 | nvram_set("et0macaddr_safe", |
|---|
| 185 | ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data, |
|---|
| 186 | eabuf)); |
|---|
| 187 | nvram_set("et0macaddr", |
|---|
| 188 | ether_etoa((unsigned char *)ifr.ifr_hwaddr.sa_data, |
|---|
| 189 | eabuf)); |
|---|
| 190 | close(s); |
|---|
| 191 | } |
|---|
| 192 | detect_wireless_devices(); |
|---|
| 193 | |
|---|
| 194 | eval("mknod", "/dev/rtc", "c", "253", "0"); |
|---|
| 195 | #ifdef HAVE_CPUTEMP |
|---|
| 196 | // insmod("nsc_gpio"); |
|---|
| 197 | // insmod("scx200_gpio"); |
|---|
| 198 | // insmod("scx200_i2c"); |
|---|
| 199 | // insmod("scx200_acb"); |
|---|
| 200 | // insmod("lm77"); |
|---|
| 201 | #endif |
|---|
| 202 | |
|---|
| 203 | nvram_set("wl0_ifname", "ath0"); |
|---|
| 204 | eval("mknod", "/dev/crypto", "c", "10", "70"); |
|---|
| 205 | /* |
|---|
| 206 | * Set a sane date |
|---|
| 207 | */ |
|---|
| 208 | stime(&tm); |
|---|
| 209 | cprintf("done\n"); |
|---|
| 210 | return; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | int check_cfe_nv(void) |
|---|
| 214 | { |
|---|
| 215 | nvram_set("portprio_support", "0"); |
|---|
| 216 | return 0; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | int check_pmon_nv(void) |
|---|
| 220 | { |
|---|
| 221 | return 0; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | void start_overclocking(void) |
|---|
| 225 | { |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | void enable_dtag_vlan(int enable) |
|---|
| 229 | { |
|---|
| 230 | |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | #include "tools/recover.c" |
|---|