1 # Copyright (C) 2006-2013 OpenWrt.org
4 .
/usr
/share
/libubox
/jshn.sh
10 if ! [ -e "$path" ]; then
11 echo "get_mac_binary: file $path not found!" >&2
15 hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev
/null
19 local basepath
="/proc/device-tree"
20 local macdevice
="$(cat "$basepath/aliases
/label-mac-device
" 2>/dev/null)"
23 [ -n "$macdevice" ] ||
return
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
)
31 get_mac_label_json
() {
32 local cfg
="/etc/board.json"
35 [ -s "$cfg" ] ||
return
38 json_load
"$(cat $cfg)"
39 if json_is_a system object
; then
41 json_get_var macaddr label_macaddr
49 local macaddr
=$
(get_mac_label_dt
)
51 [ -n "$macaddr" ] || macaddr
=$
(get_mac_label_json
)
57 local INDEX
=$
(find_mtd_index
"$1")
60 [ -d /dev
/mtd
] && PREFIX
=/dev
/mtd
/
61 echo "${INDEX:+$PREFIX$INDEX}"
70 part
=$
(find_mtd_part
"$mtdname")
71 if [ -z "$part" ]; then
72 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
76 mac_dirty
=$
(strings "$part" |
sed -n 's/^'"$key"'=//p')
79 [ -n "$mac_dirty" ] && macaddr_canonicalize
"$mac_dirty"
82 mtd_get_mac_encrypted_arcadyan
() {
83 local iv
="00000000000000000000000000000000"
84 local key
="2A4B303D7644395C3B2B7053553C5200"
90 part
=$
(find_mtd_part
"$mtdname")
91 if [ -z "$part" ]; then
92 echo "mtd_get_mac_encrypted_arcadyan: partition $mtdname not found!" >&2
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-)
105 echo "mtd_get_mac_encrypted_arcadyan: Neither uencrypt nor openssl was found!" >&2
110 [ -n "$mac_dirty" ] && macaddr_canonicalize
"$mac_dirty"
113 mtd_get_mac_encrypted_deco
() {
116 if ! [ -e "$mtdname" ]; then
117 echo "mtd_get_mac_encrypted_deco: file $mtdname not found!" >&2
121 tplink_key
="3336303032384339"
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"')
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"')
138 part
=$
(find_mtd_part
"$mtdname")
139 if [ -z "$part" ]; then
140 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
144 if [ -z "$offset" ]; then
145 echo "mtd_get_mac_text: offset missing!" >&2
149 mac_dirty
=$
(dd if="$part" bs
=1 skip
="$offset" count
=17 2>/dev
/null
)
152 [ -n "$mac_dirty" ] && macaddr_canonicalize
"$mac_dirty"
155 mtd_get_mac_binary
() {
160 part
=$
(find_mtd_part
"$mtdname")
161 get_mac_binary
"$part" "$offset"
164 mtd_get_mac_binary_ubi
() {
168 .
/lib
/upgrade
/nand.sh
170 local ubidev
=$
(nand_find_ubi
$CI_UBIPART)
171 local part
=$
(nand_find_volume
$ubidev $1)
173 get_mac_binary
"/dev/$part" "$offset"
176 mtd_get_part_size
() {
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
188 mmc_get_mac_binary
() {
193 part
=$
(find_mmc_part
"$part_name")
194 get_mac_binary
"$part" "$offset"
200 local oui
=${mac%:*:*:*}
201 local nic
=${mac#*:*:*:}
203 nic
=$
(printf "%06x" $
((0x
${nic//:/} + val
& 0xffffff)) |
sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
207 macaddr_generate_from_mmc_cid
() {
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}")")"
219 echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
226 [ $bit -gt 0 -a $bit -le 48 ] ||
return
228 printf "%012x" $
(( 0x
${mac//:/} |
2**(48-bit) )) |
sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
235 [ $bit -gt 0 -a $bit -le 48 ] ||
return
237 printf "%012x" $
(( 0x
${mac//:/} & ~
(2**(48-bit)) )) |
sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
240 macaddr_setbit_la
() {
244 macaddr_unsetbit_mc
() {
247 printf "%02x:%s" $
((0x
${mac%%:*} & ~
0x01)) ${mac#*:}
251 local randsrc
=$
(get_mac_binary
/dev
/urandom
0)
253 echo "$(macaddr_unsetbit_mc "$
(macaddr_setbit_la
"${randsrc}")")"
259 echo -ne \\x
${mac//:/\\x}
262 macaddr_canonicalize
() {
266 mac
=$
(echo -n $mac |
tr -d \")
267 [ ${#mac} -gt 17 ] && return
268 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
270 for octet
in ${mac//[\.:-]/ }; do
278 octet
="${octet:0:2} ${octet:2:2}"
281 octet
="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
287 canon
=${canon}${canon:+ }${octet}
290 [ ${#canon} -ne 17 ] && return
292 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x
${canon// / 0x} 2>/dev
/null
296 grep -q okay
"/proc/device-tree/$1/status"