1 # Copyright (C) 2006-2013 OpenWrt.org
3 .
/usr
/share
/libubox
/jshn.sh
9 if ! [ -e "$path" ]; then
10 echo "get_mac_binary: file $path not found!" >&2
14 hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev
/null
18 local basepath
="/proc/device-tree"
19 local macdevice
="$(cat "$basepath/aliases
/label-mac-device
" 2>/dev/null)"
22 [ -n "$macdevice" ] ||
return
24 macaddr
=$
(get_mac_binary
"$basepath/$macdevice/mac-address" 0 2>/dev
/null
)
25 [ -n "$macaddr" ] || macaddr
=$
(get_mac_binary
"$basepath/$macdevice/local-mac-address" 0 2>/dev
/null
)
30 get_mac_label_json
() {
31 local cfg
="/etc/board.json"
34 [ -s "$cfg" ] ||
return
37 json_load
"$(cat $cfg)"
38 if json_is_a system object
; then
40 json_get_var macaddr label_macaddr
48 local macaddr
=$
(get_mac_label_dt
)
50 [ -n "$macaddr" ] || macaddr
=$
(get_mac_label_json
)
56 local INDEX
=$
(find_mtd_index
"$1")
59 [ -d /dev
/mtd
] && PREFIX
=/dev
/mtd
/
60 echo "${INDEX:+$PREFIX$INDEX}"
69 part
=$
(find_mtd_part
"$mtdname")
70 if [ -z "$part" ]; then
71 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
75 mac_dirty
=$
(strings "$part" |
sed -n 's/^'"$key"'=//p')
78 [ -n "$mac_dirty" ] && macaddr_canonicalize
"$mac_dirty"
87 part
=$
(find_mtd_part
"$mtdname")
88 if [ -z "$part" ]; then
89 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
93 if [ -z "$offset" ]; then
94 echo "mtd_get_mac_text: offset missing!" >&2
98 mac_dirty
=$
(dd if="$part" bs
=1 skip
="$offset" count
=17 2>/dev
/null
)
101 [ -n "$mac_dirty" ] && macaddr_canonicalize
"$mac_dirty"
104 mtd_get_mac_binary
() {
109 part
=$
(find_mtd_part
"$mtdname")
110 get_mac_binary
"$part" "$offset"
113 mtd_get_mac_binary_ubi
() {
117 .
/lib
/upgrade
/nand.sh
119 local ubidev
=$
(nand_find_ubi
$CI_UBIPART)
120 local part
=$
(nand_find_volume
$ubidev $1)
122 get_mac_binary
"/dev/$part" "$offset"
125 mtd_get_part_size
() {
127 local first dev size erasesize name
128 while read dev size erasesize name
; do
129 name
=${name#'"'}; name
=${name%'"'}
130 if [ "$name" = "$part_name" ]; then
140 local oui
=${mac%:*:*:*}
141 local nic
=${mac#*:*:*:}
143 nic
=$
(printf "%06x" $
((0x
${nic//:/} + val
& 0xffffff)) |
sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
151 echo ${mac:9:2}$sep${mac:12:2}$sep${mac:15:2}
154 macaddr_setbit_la
() {
157 printf "%02x:%s" $
((0x
${mac%%:*} |
0x02)) ${mac#*:}
163 echo -ne \\x
${mac//:/\\x}
166 macaddr_canonicalize
() {
170 mac
=$
(echo -n $mac |
tr -d \")
171 [ ${#mac} -gt 17 ] && return
172 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
174 for octet
in ${mac//[\.:-]/ }; do
182 octet
="${octet:0:2} ${octet:2:2}"
185 octet
="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
191 canon
=${canon}${canon:+ }${octet}
194 [ ${#canon} -ne 17 ] && return
196 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x
${canon// / 0x} 2>/dev
/null