ramips: add basic support for tp-link er605-v2
[openwrt/openwrt.git] / package / base-files / files / lib / functions / system.sh
1 # Copyright (C) 2006-2013 OpenWrt.org
2
3 . /lib/functions.sh
4 . /usr/share/libubox/jshn.sh
5
6 get_mac_binary() {
7 local path="$1"
8 local offset="$2"
9
10 if ! [ -e "$path" ]; then
11 echo "get_mac_binary: file $path not found!" >&2
12 return
13 fi
14
15 hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev/null
16 }
17
18 get_mac_label_dt() {
19 local basepath="/proc/device-tree"
20 local macdevice="$(cat "$basepath/aliases/label-mac-device" 2>/dev/null)"
21 local macaddr
22
23 [ -n "$macdevice" ] || return
24
25 macaddr=$(get_mac_binary "$basepath/$macdevice/mac-address" 0 2>/dev/null)
26 [ -n "$macaddr" ] || macaddr=$(get_mac_binary "$basepath/$macdevice/local-mac-address" 0 2>/dev/null)
27
28 echo $macaddr
29 }
30
31 get_mac_label_json() {
32 local cfg="/etc/board.json"
33 local macaddr
34
35 [ -s "$cfg" ] || return
36
37 json_init
38 json_load "$(cat $cfg)"
39 if json_is_a system object; then
40 json_select system
41 json_get_var macaddr label_macaddr
42 json_select ..
43 fi
44
45 echo $macaddr
46 }
47
48 get_mac_label() {
49 local macaddr=$(get_mac_label_dt)
50
51 [ -n "$macaddr" ] || macaddr=$(get_mac_label_json)
52
53 echo $macaddr
54 }
55
56 find_mtd_chardev() {
57 local INDEX=$(find_mtd_index "$1")
58 local PREFIX=/dev/mtd
59
60 [ -d /dev/mtd ] && PREFIX=/dev/mtd/
61 echo "${INDEX:+$PREFIX$INDEX}"
62 }
63
64 mtd_get_mac_ascii() {
65 local mtdname="$1"
66 local key="$2"
67 local part
68 local mac_dirty
69
70 part=$(find_mtd_part "$mtdname")
71 if [ -z "$part" ]; then
72 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
73 return
74 fi
75
76 mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
77
78 # "canonicalize" mac
79 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
80 }
81
82 mtd_get_mac_encrypted_arcadyan() {
83 local iv="00000000000000000000000000000000"
84 local key="2A4B303D7644395C3B2B7053553C5200"
85 local mac_dirty
86 local mtdname="$1"
87 local part
88 local size
89
90 part=$(find_mtd_part "$mtdname")
91 if [ -z "$part" ]; then
92 echo "mtd_get_mac_encrypted_arcadyan: partition $mtdname not found!" >&2
93 return
94 fi
95
96 # Config decryption and getting mac. Trying uencrypt and openssl utils.
97 size=$((0x$(dd if=$part skip=9 bs=1 count=4 2>/dev/null | hexdump -v -e '1/4 "%08x"')))
98 if [[ -f "/usr/bin/uencrypt" ]]; then
99 mac_dirty=$(dd if=$part bs=1 count=$size skip=$((0x100)) 2>/dev/null | \
100 uencrypt -d -n -k $key -i $iv | grep mac | cut -c 5-)
101 elif [[ -f "/usr/bin/openssl" ]]; then
102 mac_dirty=$(dd if=$part bs=1 count=$size skip=$((0x100)) 2>/dev/null | \
103 openssl aes-128-cbc -d -nopad -K $key -iv $iv | grep mac | cut -c 5-)
104 else
105 echo "mtd_get_mac_encrypted_arcadyan: Neither uencrypt nor openssl was found!" >&2
106 return
107 fi
108
109 # "canonicalize" mac
110 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
111 }
112
113 mtd_get_mac_encrypted_deco() {
114 local mtdname="$1"
115
116 if ! [ -e "$mtdname" ]; then
117 echo "mtd_get_mac_encrypted_deco: file $mtdname not found!" >&2
118 return
119 fi
120
121 tplink_key="3336303032384339"
122
123 key=$(dd if=$mtdname bs=1 skip=16 count=8 2>/dev/null | \
124 uencrypt -n -d -k $tplink_key -c des-ecb | hexdump -v -n 8 -e '1/1 "%02x"')
125
126 macaddr=$(dd if=$mtdname bs=1 skip=32 count=8 2>/dev/null | \
127 uencrypt -n -d -k $key -c des-ecb | hexdump -v -n 6 -e '5/1 "%02x:" 1/1 "%02x"')
128
129 echo $macaddr
130 }
131
132 mtd_get_mac_uci_config_ubi() {
133 local volumename="$1"
134
135 . /lib/upgrade/nand.sh
136
137 local ubidev=$(nand_attach_ubi $CI_UBIPART)
138 local part=$(nand_find_volume $ubidev $volumename)
139
140 cat "/dev/$part" | sed -n 's/^\s*option macaddr\s*'"'"'\?\([0-9A-F:]\+\)'"'"'\?/\1/Ip'
141 }
142
143 mtd_get_mac_text() {
144 local mtdname=$1
145 local offset=$(($2))
146 local part
147 local mac_dirty
148
149 part=$(find_mtd_part "$mtdname")
150 if [ -z "$part" ]; then
151 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
152 return
153 fi
154
155 if [ -z "$offset" ]; then
156 echo "mtd_get_mac_text: offset missing!" >&2
157 return
158 fi
159
160 mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
161
162 # "canonicalize" mac
163 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
164 }
165
166 mtd_get_mac_binary() {
167 local mtdname="$1"
168 local offset="$2"
169 local part
170
171 part=$(find_mtd_part "$mtdname")
172 get_mac_binary "$part" "$offset"
173 }
174
175 mtd_get_mac_binary_ubi() {
176 local mtdname="$1"
177 local offset="$2"
178
179 . /lib/upgrade/nand.sh
180
181 local ubidev=$(nand_find_ubi $CI_UBIPART)
182 local part=$(nand_find_volume $ubidev $1)
183
184 get_mac_binary "/dev/$part" "$offset"
185 }
186
187 mtd_get_part_size() {
188 local part_name=$1
189 local first dev size erasesize name
190 while read dev size erasesize name; do
191 name=${name#'"'}; name=${name%'"'}
192 if [ "$name" = "$part_name" ]; then
193 echo $((0x$size))
194 break
195 fi
196 done < /proc/mtd
197 }
198
199 mmc_get_mac_binary() {
200 local part_name="$1"
201 local offset="$2"
202 local part
203
204 part=$(find_mmc_part "$part_name")
205 get_mac_binary "$part" "$offset"
206 }
207
208 macaddr_add() {
209 local mac=$1
210 local val=$2
211 local oui=${mac%:*:*:*}
212 local nic=${mac#*:*:*:}
213
214 nic=$(printf "%06x" $((0x${nic//:/} + val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
215 echo $oui:$nic
216 }
217
218 macaddr_generate_from_mmc_cid() {
219 local mmc_dev=$1
220
221 local sd_hash=$(sha256sum /sys/class/block/$mmc_dev/device/cid)
222 local mac_base=$(macaddr_canonicalize "$(echo "${sd_hash}" | dd bs=1 count=12 2>/dev/null)")
223 echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${mac_base}")")"
224 }
225
226 macaddr_geteui() {
227 local mac=$1
228 local sep=$2
229
230 echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
231 }
232
233 macaddr_setbit() {
234 local mac=$1
235 local bit=${2:-0}
236
237 [ $bit -gt 0 -a $bit -le 48 ] || return
238
239 printf "%012x" $(( 0x${mac//:/} | 2**(48-bit) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
240 }
241
242 macaddr_unsetbit() {
243 local mac=$1
244 local bit=${2:-0}
245
246 [ $bit -gt 0 -a $bit -le 48 ] || return
247
248 printf "%012x" $(( 0x${mac//:/} & ~(2**(48-bit)) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
249 }
250
251 macaddr_setbit_la() {
252 macaddr_setbit $1 7
253 }
254
255 macaddr_unsetbit_mc() {
256 local mac=$1
257
258 printf "%02x:%s" $((0x${mac%%:*} & ~0x01)) ${mac#*:}
259 }
260
261 macaddr_random() {
262 local randsrc=$(get_mac_binary /dev/urandom 0)
263
264 echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${randsrc}")")"
265 }
266
267 macaddr_2bin() {
268 local mac=$1
269
270 echo -ne \\x${mac//:/\\x}
271 }
272
273 macaddr_canonicalize() {
274 local mac="$1"
275 local canon=""
276
277 mac=$(echo -n $mac | tr -d \")
278 [ ${#mac} -gt 17 ] && return
279 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
280
281 for octet in ${mac//[\.:-]/ }; do
282 case "${#octet}" in
283 1)
284 octet="0${octet}"
285 ;;
286 2)
287 ;;
288 4)
289 octet="${octet:0:2} ${octet:2:2}"
290 ;;
291 12)
292 octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
293 ;;
294 *)
295 return
296 ;;
297 esac
298 canon=${canon}${canon:+ }${octet}
299 done
300
301 [ ${#canon} -ne 17 ] && return
302
303 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
304 }
305
306 dt_is_enabled() {
307 grep -q okay "/proc/device-tree/$1/status"
308 }