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}"
69 mac_dirty
=$
(strings "$part" |
sed -n 's/^'"$key"'=//p')
72 [ -n "$mac_dirty" ] && macaddr_canonicalize
"$mac_dirty"
80 part
=$
(find_mtd_part
"$mtdname")
81 if [ -z "$part" ]; then
82 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
86 get_mac_ascii
"$part" "$key"
89 mtd_get_mac_encrypted_arcadyan
() {
90 local iv
="00000000000000000000000000000000"
91 local key
="2A4B303D7644395C3B2B7053553C5200"
97 part
=$
(find_mtd_part
"$mtdname")
98 if [ -z "$part" ]; then
99 echo "mtd_get_mac_encrypted_arcadyan: partition $mtdname not found!" >&2
103 # Config decryption and getting mac. Trying uencrypt and openssl utils.
104 size
=$
((0x$
(dd if=$part skip
=9 bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/4 "%08x"')))
105 if [[ -f "/usr/bin/uencrypt" ]]; then
106 mac_dirty
=$
(dd if=$part bs
=1 count
=$size skip
=$
((0x100)) 2>/dev
/null | \
107 uencrypt
-d -n -k $key -i $iv |
grep mac | cut
-c 5-)
108 elif [[ -f "/usr/bin/openssl" ]]; then
109 mac_dirty
=$
(dd if=$part bs
=1 count
=$size skip
=$
((0x100)) 2>/dev
/null | \
110 openssl aes-128-cbc
-d -nopad -K $key -iv $iv |
grep mac | cut
-c 5-)
112 echo "mtd_get_mac_encrypted_arcadyan: Neither uencrypt nor openssl was found!" >&2
117 [ -n "$mac_dirty" ] && macaddr_canonicalize
"$mac_dirty"
120 mtd_get_mac_encrypted_deco
() {
123 if ! [ -e "$mtdname" ]; then
124 echo "mtd_get_mac_encrypted_deco: file $mtdname not found!" >&2
128 tplink_key
="3336303032384339"
130 key
=$
(dd if=$mtdname bs
=1 skip
=16 count
=8 2>/dev
/null | \
131 uencrypt
-n -d -k $tplink_key -c des-ecb |
hexdump -v -n 8 -e '1/1 "%02x"')
133 macaddr
=$
(dd if=$mtdname bs
=1 skip
=32 count
=8 2>/dev
/null | \
134 uencrypt
-n -d -k $key -c des-ecb |
hexdump -v -n 6 -e '5/1 "%02x:" 1/1 "%02x"')
139 mtd_get_mac_uci_config_ubi
() {
140 local volumename
="$1"
142 .
/lib
/upgrade
/nand.sh
144 local ubidev
=$
(nand_attach_ubi
$CI_UBIPART)
145 local part
=$
(nand_find_volume
$ubidev $volumename)
147 cat "/dev/$part" |
sed -n 's/^\s*option macaddr\s*'"'"'\?\([0-9A-F:]\+\)'"'"'\?/\1/Ip'
152 local offset
=$
((${2:-0}))
153 local length
="${3:-17}"
156 part
=$
(find_mtd_part
"$mtdname")
157 if [ -z "$part" ]; then
158 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
162 [ $
((offset
+ length
)) -le $
(mtd_get_part_size
"$mtdname") ] ||
return
164 macaddr_canonicalize $
(dd bs
=1 if="$part" skip
="$offset" count
="$length" 2>/dev
/null
)
167 mtd_get_mac_binary
() {
172 part
=$
(find_mtd_part
"$mtdname")
173 get_mac_binary
"$part" "$offset"
176 mtd_get_mac_binary_ubi
() {
180 .
/lib
/upgrade
/nand.sh
182 local ubidev
=$
(nand_find_ubi
$CI_UBIPART)
183 local part
=$
(nand_find_volume
$ubidev $1)
185 get_mac_binary
"/dev/$part" "$offset"
188 mtd_get_part_size
() {
190 local first dev size erasesize name
191 while read dev size erasesize name
; do
192 name
=${name#'"'}; name
=${name%'"'}
193 if [ "$name" = "$part_name" ]; then
200 mmc_get_mac_ascii
() {
205 part
=$
(find_mmc_part
"$part_name")
206 if [ -z "$part" ]; then
207 echo "mmc_get_mac_ascii: partition $part_name not found!" >&2
211 get_mac_ascii
"$part" "$key"
214 mmc_get_mac_binary
() {
219 part
=$
(find_mmc_part
"$part_name")
220 get_mac_binary
"$part" "$offset"
226 local oui
=${mac%:*:*:*}
227 local nic
=${mac#*:*:*:}
229 nic
=$
(printf "%06x" $
((0x
${nic//:/} + val
& 0xffffff)) |
sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
233 macaddr_generate_from_mmc_cid
() {
236 local sd_hash
=$
(sha256sum
/sys
/class
/block
/$mmc_dev/device
/cid
)
237 local mac_base
=$
(macaddr_canonicalize
"$(echo "${sd_hash}" | dd bs=1 count=12 2>/dev/null)")
238 echo "$(macaddr_unsetbit_mc "$
(macaddr_setbit_la
"${mac_base}")")"
245 echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
252 [ $bit -gt 0 -a $bit -le 48 ] ||
return
254 printf "%012x" $
(( 0x
${mac//:/} |
2**(48-bit) )) |
sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
261 [ $bit -gt 0 -a $bit -le 48 ] ||
return
263 printf "%012x" $
(( 0x
${mac//:/} & ~
(2**(48-bit)) )) |
sed -e 's/\(.\{2\}\)/\1:/g' -e 's/:$//'
266 macaddr_setbit_la
() {
270 macaddr_unsetbit_mc
() {
273 printf "%02x:%s" $
((0x
${mac%%:*} & ~
0x01)) ${mac#*:}
277 local randsrc
=$
(get_mac_binary
/dev
/urandom
0)
279 echo "$(macaddr_unsetbit_mc "$
(macaddr_setbit_la
"${randsrc}")")"
285 echo -ne \\x
${mac//:/\\x}
288 macaddr_canonicalize
() {
292 mac
=$
(echo -n $mac |
tr -d \")
293 [ ${#mac} -gt 17 ] && return
294 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
296 for octet
in ${mac//[\.:-]/ }; do
304 octet
="${octet:0:2} ${octet:2:2}"
307 octet
="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
313 canon
=${canon}${canon:+ }${octet}
316 [ ${#canon} -ne 17 ] && return
318 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x
${canon// / 0x} 2>/dev
/null
322 grep -q okay
"/proc/device-tree/$1/status"