base-files: add mtd_get_mac_encrypted_arcadyan function
[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_text() {
114 local mtdname=$1
115 local offset=$(($2))
116 local part
117 local mac_dirty
118
119 part=$(find_mtd_part "$mtdname")
120 if [ -z "$part" ]; then
121 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
122 return
123 fi
124
125 if [ -z "$offset" ]; then
126 echo "mtd_get_mac_text: offset missing!" >&2
127 return
128 fi
129
130 mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
131
132 # "canonicalize" mac
133 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
134 }
135
136 mtd_get_mac_binary() {
137 local mtdname="$1"
138 local offset="$2"
139 local part
140
141 part=$(find_mtd_part "$mtdname")
142 get_mac_binary "$part" "$offset"
143 }
144
145 mtd_get_mac_binary_ubi() {
146 local mtdname="$1"
147 local offset="$2"
148
149 . /lib/upgrade/nand.sh
150
151 local ubidev=$(nand_find_ubi $CI_UBIPART)
152 local part=$(nand_find_volume $ubidev $1)
153
154 get_mac_binary "/dev/$part" "$offset"
155 }
156
157 mtd_get_part_size() {
158 local part_name=$1
159 local first dev size erasesize name
160 while read dev size erasesize name; do
161 name=${name#'"'}; name=${name%'"'}
162 if [ "$name" = "$part_name" ]; then
163 echo $((0x$size))
164 break
165 fi
166 done < /proc/mtd
167 }
168
169 mmc_get_mac_binary() {
170 local part_name="$1"
171 local offset="$2"
172 local part
173
174 part=$(find_mmc_part "$part_name")
175 get_mac_binary "$part" "$offset"
176 }
177
178 macaddr_add() {
179 local mac=$1
180 local val=$2
181 local oui=${mac%:*:*:*}
182 local nic=${mac#*:*:*:}
183
184 nic=$(printf "%06x" $((0x${nic//:/} + val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
185 echo $oui:$nic
186 }
187
188 macaddr_geteui() {
189 local mac=$1
190 local sep=$2
191
192 echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
193 }
194
195 macaddr_setbit() {
196 local mac=$1
197 local bit=${2:-0}
198
199 [ $bit -gt 0 -a $bit -le 48 ] || return
200
201 printf "%012x" $(( 0x${mac//:/} | 2**(48-bit) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
202 }
203
204 macaddr_unsetbit() {
205 local mac=$1
206 local bit=${2:-0}
207
208 [ $bit -gt 0 -a $bit -le 48 ] || return
209
210 printf "%012x" $(( 0x${mac//:/} & ~(2**(48-bit)) )) | sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
211 }
212
213 macaddr_setbit_la() {
214 macaddr_setbit $1 7
215 }
216
217 macaddr_unsetbit_mc() {
218 local mac=$1
219
220 printf "%02x:%s" $((0x${mac%%:*} & ~0x01)) ${mac#*:}
221 }
222
223 macaddr_random() {
224 local randsrc=$(get_mac_binary /dev/urandom 0)
225
226 echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${randsrc}")")"
227 }
228
229 macaddr_2bin() {
230 local mac=$1
231
232 echo -ne \\x${mac//:/\\x}
233 }
234
235 macaddr_canonicalize() {
236 local mac="$1"
237 local canon=""
238
239 mac=$(echo -n $mac | tr -d \")
240 [ ${#mac} -gt 17 ] && return
241 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
242
243 for octet in ${mac//[\.:-]/ }; do
244 case "${#octet}" in
245 1)
246 octet="0${octet}"
247 ;;
248 2)
249 ;;
250 4)
251 octet="${octet:0:2} ${octet:2:2}"
252 ;;
253 12)
254 octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
255 ;;
256 *)
257 return
258 ;;
259 esac
260 canon=${canon}${canon:+ }${octet}
261 done
262
263 [ ${#canon} -ne 17 ] && return
264
265 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
266 }