brcm47xx: parse model from cpuinfo
[openwrt.git] / target / linux / brcm47xx / base-files / etc / init.d / netconfig
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006 OpenWrt.org
3
4 START=05
5
6 start() {
7         [ -e /etc/config/network ] && {
8                 local batch
9
10                 config_cb() {
11                         case "$1" in
12                         switch)
13                                 option_cb() {
14                                         case "$1" in
15                                         vlan[0-9]|vlan1[0-5])
16                                                 local id="${1#vlan}"
17                                                 local ports="${2%\*}"
18                                                 append batch "delete network.eth0.${1}${N}"
19                                                 append batch "set network.eth0_${id}=switch_vlan${N}"
20                                                 append batch "set network.eth0_${id}.device=eth0${N}"
21                                                 append batch "set network.eth0_${id}.vlan=${id}${N}"
22                                                 append batch "set network.eth0_${id}.ports='${ports}'${N}"
23                                         ;;
24                                         esac
25                                 }
26                         ;;
27                         switch_vlan)
28                                 option_cb() { :; }
29                                 batch=""
30                         ;;
31                         esac
32                 }
33
34                 config_load network
35
36                 [ -n "$batch" ] && {
37                         logger -t netconfig "migrating switch config to new format ..."
38                         echo "$batch${N}commit network" | uci batch
39                 }
40
41                 exit 0
42         }
43
44         mkdir -p /etc/config
45
46         local cpuport=5
47         [ -e /sbin/swconfig ] && cpuport=$(swconfig dev switch0 help 2>/dev/null | sed -ne "s|.*cpu @ \([0-9]*\).*|\1|p")
48         local model=$(awk 'BEGIN{FS="[ \t]+:[ \t]"} /machine/ {print $2}' /proc/cpuinfo)
49
50         local network_defs=`(
51                 if grep -E 'mtd0: 000(6|a)0000' /proc/mtd 2>&- >&-; then
52                         # WGT634u
53                         echo boardtype=wgt634u
54                 else
55                         strings "$(find_mtd_part nvram)"
56                 fi
57         ) | awk -v cpuport="$cpuport" -v model="$model" '
58         function macinc(mac, maca, i, result) {
59                 split(mac, maca, ":")
60                 for (i = 1; i <= 6; i++) maca[i] = "0x" maca[i]
61                 if (++maca[6] > 0xff) {
62                         maca[5]++
63                         maca[6] = 0
64                 }
65                 for (i = 1; i <= 6; i++) {
66                         if (i != 1) result = result ":"
67                         result = result sprintf("%02x", maca[i])
68                 }
69                 return result
70         }
71
72         BEGIN {
73                 FS="="
74                 c["lan_ifname"]="eth0.1"
75                 c["wan_ifname"]="eth0.2"
76                 c["vlan1ports"]="1 2 3 4 5t"
77                 c["vlan2ports"]="0 5t"
78                 for (i = 0; i < 6; i++) {
79                         if (mac_check != "") mac_check = mac_check ":"
80                         mac_check = mac_check "[0-9a-fA-F][0-9a-fA-F]"
81                 }
82                 if (cpuport == "8") {
83                         c["vlan1ports"]="1 2 3 4 8t"
84                         c["vlan2ports"]="0 8t"
85                 }
86         }
87
88         ($1 == "boardnum") || ($1 == "boardtype") || ($1 == "boardflags") || ($1 ~ /macaddr/) || \
89          ($1 ~ /^vlan[0-9]{1,2}ports$/) {
90                 nvram[$1] = $2
91         }
92
93         END {
94                 if (((nvram["vlan0ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan1ports"] ~ /^4 8/ && (cpuport == "8"))) || \
95                     ((nvram["vlan1ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan2ports"] ~ /^4 8/ && (cpuport == "8"))) || \
96                     ((nvram["vlan2ports"] ~ /^0 1 2 3 8/ ) && (nvram["vlan1ports"] ~ /^4 8/ && (cpuport == "8")))) {
97                         c["vlan1ports"] = "0 1 2 3 8t"
98                         c["vlan2ports"] = "4 8t"
99                 }
100                 if (((nvram["vlan0ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan1ports"] ~ /^4 5/ && (cpuport == "5"))) || \
101                     ((nvram["vlan1ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan2ports"] ~ /^4 5/ && (cpuport == "5"))) || \
102                     ((nvram["vlan2ports"] ~ /^0 1 2 3 5/ ) && (nvram["vlan1ports"] ~ /^4 5/ && (cpuport == "5")))) {
103                         c["vlan1ports"] = "0 1 2 3 5t"
104                         c["vlan2ports"] = "4 5t"
105                 }
106                 if ((model == "ASUS WL-HDD") || (model == "ASUS WL-300g") || (model == "Linksys WAP54G V1")) {
107                         c["wan_ifname"] = ""
108                         c["lan_ifname"] = "eth1"
109                 }
110                 if (model == "ASUS WL-330gE") {
111                         c["wan_ifname"] = ""
112                         c["lan_ifname"] = "eth0.1"
113                         c["vlan1ports"] = "4 5t"
114                         c["vlan2ports"] = ""
115                 }
116                 if ((model == "ASUS WL-500g") || (model == "Microsoft MN-700")) {
117                         c["wan_ifname"] = "eth1"
118                         c["lan_ifname"] = "eth0"
119                 }
120                 if ((model == "ASUS WL-500g Premium V2") || (model == "Dell TrueMobile 2300 v2") || (model == "Buffalo WHR-G125")) {
121                         c["vlan1ports"] = "0 1 2 3 5t"
122                         c["vlan2ports"] = "4 5t"
123                 }
124                 if (model == "Dell TrueMobile 2300") {
125                         c["lan_ifname"] = "eth0"
126                         c["wan_ifname"] = "eth1"
127                         c["vlan1ports"] = "0 1 2 3 4 5"
128                         c["vlan2ports"] = ""
129                 }
130                 if (nvram["boardtype"] == "bcm94710r4") {
131                         # Toshiba WRC-1000
132                         c["lan_ifname"] = "eth0"
133                         c["wan_ifname"] = "eth1"
134                 }
135                 if ((nvram["boardtype"] == "wgt634u") || (nvram["boardtype"] == "0x0467")) {
136                         c["vlan1ports"] = "0 1 2 3 5t"
137                         c["vlan2ports"] = "4 5t"
138                 }
139                 if ((nvram["boardtype"] == "0x042f") || (nvram["boardtype"] == "0x0472")) {
140                         if (nvram["boardnum"] == "45") {
141                                 # WL-500gP
142                                 c["vlan1ports"] = "1 2 3 4 5t"
143                                 c["vlan2ports"] = "0 5t"
144                         } else {
145                                 # Generic BCM94704
146                                 c["vlan1ports"] = "0 1 2 3 4 5"
147                                 c["vlan2ports"] = ""
148                                 c["lan_ifname"] = "eth0"
149                                 c["wan_ifname"] = "eth1"
150
151                                 # MAC addresses on 4704 tend to be screwed up. Add a workaround here
152                                 if (nvram["et0macaddr"] ~ mac_check) {
153                                         c["lan_macaddr"] = nvram["et0macaddr"]
154                                         c["wan_macaddr"] = macinc(c["lan_macaddr"])
155                                 }
156                         }
157                 }
158                 # Buffalo WBR-B11 and Buffalo WBR-G54
159                 if (nvram["boardtype"] == "bcm94710ap") {
160                         c["vlan1ports"] = "0 1 2 3 4 5"
161                         c["vlan2ports"] = ""
162                         c["lan_ifname"] = "eth0"
163                         c["wan_ifname"] = "eth1"
164                 }
165
166                 # WAP54G
167                 if ((nvram["boardnum"] == "2") || \
168                         (nvram["boardnum"] == "1024")) {
169                         c["lan_ifname"]="eth0"
170                         c["wan_ifname"]=""
171                 }
172
173                 # Sitecom WL-105b
174                 if ((nvram["boardum"] == "2") && \
175                         (nvram["GemtekPmonVer"] == "1")) {
176                         c["lan_ifname"]="eth0"
177                         c["wan_ifname"]=""
178                 }
179
180                 # ASUS WL-700gE
181                 # These are actually same as defaults above. For some reason this script applies
182                 # Generic BCM94704 settings instead so we revert to proper settings here.
183                 # Hopefully someone will fix this properly soon.
184                 if (model == "ASUS WL-700gE") {
185                         c["lan_ifname"]="eth0.1"
186                         c["wan_ifname"]="eth0.2"
187                         c["vlan1ports"]="1 2 3 4 5t"
188                         c["vlan2ports"]="0 5t"
189                 }
190                 if ((model == "Motorola WR850G V2/V3") || (model == "Siemens SE505 V2")) {
191                         c["vlan1ports"]="0 1 2 3 5t"
192                         c["vlan2ports"]="4 5t"
193                 }
194                 if (model == "ASUS WL-500W") {
195                         c["lan_ifname"] = "eth0"
196                         c["wan_ifname"] = "eth1"
197                         c["vlan1ports"] = "0 1 2 3 4 5"
198                         c["vlan2ports"] = ""
199                 }
200                 if (model == "OvisLink WL-1600GL") {
201                         c["lan_ifname"] = "eth0.1"
202                         c["wan_ifname"] = "eth0.2"
203                         c["vlan1ports"] = "0 1 2 3 5t"
204                         c["vlan2ports"] = "4 5t"
205                 }
206                 if (model == "SimpleTech SimpleShare NAS") {
207                         c["lan_ifname"] = "eth0"
208                         c["wan_ifname"] = ""
209                         c["vlan1ports"] = ""
210                         c["vlan2ports"] = ""
211                 }
212                 print "local vlan1ports=\"" c["vlan1ports"] "\";"
213                 print "local vlan2ports=\"" c["vlan2ports"] "\";"
214                 print "local lan_ifname=\"" c["lan_ifname"] "\";"
215                 print "local lan_macaddr=\"" c["lan_macaddr"] "\";"
216                 print "local wan_ifname=\"" c["wan_ifname"] "\";"
217                 print "local wan_macaddr=\"" c["wan_macaddr"] "\";"
218         }'`
219
220         . /lib/functions/uci-defaults.sh
221
222         touch /etc/config/network
223
224         eval "$network_defs"
225
226         [ -n "$vlan1ports" -o -n "$vlan2ports" ] && {
227                 local cfg=`ucidef_add_switch "switch0" 1 1`
228                 [ -n "$cfg" ] && uci rename network.$cfg=eth0
229                 [ -n "$vlan1ports" ] && {
230                         cfg=`ucidef_add_switch_vlan "switch0" 1 "$vlan1ports"`
231                         [ -n "$cfg" ] && uci rename network.$cfg=eth0_1
232                 }
233                 [ -n "$vlan2ports" ] && {
234                         cfg=`ucidef_add_switch_vlan "switch0" 2 "$vlan2ports"`
235                         [ -n "$cfg" ] && uci rename network.$cfg=eth0_2
236                 }
237         }
238
239         ucidef_set_interface_loopback
240
241         ucidef_set_interface_lan "$lan_ifname"
242         [ -n "$lan_macaddr" ] && ucidef_set_interface_macaddr lan "$lan_macaddr"
243
244         [ -n "$wan_ifname" ] && {
245                 ucidef_set_interface_wan "$wan_ifname"
246                 [ -n "$wan_macaddr" ] && ucidef_set_interface_macaddr wan "$wan_macaddr"
247         }
248
249         uci commit network
250 }