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