base-files: rework mtd_get_mac_text()
[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:-0}))
146 local length="${3:-17}"
147 local part
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 [ $((offset + length)) -le $(mtd_get_part_size "$mtdname") ] || return
156
157 macaddr_canonicalize $(dd bs=1 if="$part" skip="$offset" count="$length" 2>/dev/null)
158 }
159
160 mtd_get_mac_binary() {
161 local mtdname="$1"
162 local offset="$2"
163 local part
164
165 part=$(find_mtd_part "$mtdname")
166 get_mac_binary "$part" "$offset"
167 }
168
169 mtd_get_mac_binary_ubi() {
170 local mtdname="$1"
171 local offset="$2"
172
173 . /lib/upgrade/nand.sh
174
175 local ubidev=$(nand_find_ubi $CI_UBIPART)
176 local part=$(nand_find_volume $ubidev $1)
177
178 get_mac_binary "/dev/$part" "$offset"
179 }
180
181 mtd_get_part_size() {
182 local part_name=$1
183 local first dev size erasesize name
184 while read dev size erasesize name; do
185 name=${name#'"'}; name=${name%'"'}
186 if [ "$name" = "$part_name" ]; then
187 echo $((0x$size))
188 break
189 fi
190 done < /proc/mtd
191 }
192
193 mmc_get_mac_binary() {
194 local part_name="$1"
195 local offset="$2"
196 local part
197
198 part=$(find_mmc_part "$part_name")
199 get_mac_binary "$part" "$offset"
200 }
201
202 macaddr_add() {
203 local mac=$1
204 local val=$2
205 local oui=${mac%:*:*:*}
206 local nic=${mac#*:*:*:}
207
208 nic=$(printf "%06x" $((0x${nic//:/} + val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
209 echo $oui:$nic
210 }
211
212 macaddr_generate_from_mmc_cid() {
213 local mmc_dev=$1
214
215 local sd_hash=$(sha256sum /sys/class/block/$mmc_dev/device/cid)
216 local mac_base=$(macaddr_canonicalize "$(echo "${sd_hash}" | dd bs=1 count=12 2>/dev/null)")
217 echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${mac_base}")")"
218 }
219
220 macaddr_geteui() {
221 local mac=$1
222 local sep=$2
223
224 echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
225 }
226
227 macaddr_setbit() {
228 local mac=$1
229 local bit=${2:-0}
230
231 [ $bit -gt 0 -a $bit -le 48 ] || return
232
233 printf "%012x" $(( 0x${mac//:/} | 2**(48-bit) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
234 }
235
236 macaddr_unsetbit() {
237 local mac=$1
238 local bit=${2:-0}
239
240 [ $bit -gt 0 -a $bit -le 48 ] || return
241
242 printf "%012x" $(( 0x${mac//:/} & ~(2**(48-bit)) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
243 }
244
245 macaddr_setbit_la() {
246 macaddr_setbit $1 7
247 }
248
249 macaddr_unsetbit_mc() {
250 local mac=$1
251
252 printf "%02x:%s" $((0x${mac%%:*} & ~0x01)) ${mac#*:}
253 }
254
255 macaddr_random() {
256 local randsrc=$(get_mac_binary /dev/urandom 0)
257
258 echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${randsrc}")")"
259 }
260
261 macaddr_2bin() {
262 local mac=$1
263
264 echo -ne \\x${mac//:/\\x}
265 }
266
267 macaddr_canonicalize() {
268 local mac="$1"
269 local canon=""
270
271 mac=$(echo -n $mac | tr -d \")
272 [ ${#mac} -gt 17 ] && return
273 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
274
275 for octet in ${mac//[\.:-]/ }; do
276 case "${#octet}" in
277 1)
278 octet="0${octet}"
279 ;;
280 2)
281 ;;
282 4)
283 octet="${octet:0:2} ${octet:2:2}"
284 ;;
285 12)
286 octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
287 ;;
288 *)
289 return
290 ;;
291 esac
292 canon=${canon}${canon:+ }${octet}
293 done
294
295 [ ${#canon} -ne 17 ] && return
296
297 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
298 }
299
300 dt_is_enabled() {
301 grep -q okay "/proc/device-tree/$1/status"
302 }