1 # Copyright (C) 2006-2013 OpenWrt.org
7 if [ -z "$path" ]; then
8 echo "get_mac_binary: file $path not found!" >&2
12 hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev/null
16 local INDEX=$(find_mtd_index "$1")
19 [ -d /dev/mtd ] && PREFIX=/dev/mtd/
20 echo "${INDEX:+$PREFIX$INDEX}"
29 part=$(find_mtd_part "$mtdname")
30 if [ -z "$part" ]; then
31 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
35 mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
38 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
47 part=$(find_mtd_part "$mtdname")
48 if [ -z "$part" ]; then
49 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
53 if [ -z "$offset" ]; then
54 echo "mtd_get_mac_text: offset missing!" >&2
58 mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
61 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
64 mtd_get_mac_binary() {
69 part=$(find_mtd_part "$mtdname")
70 get_mac_binary "$part" "$offset"
73 mtd_get_mac_binary_ubi() {
77 . /lib/upgrade/nand.sh
79 local ubidev=$(nand_find_ubi $CI_UBIPART)
80 local part=$(nand_find_volume $ubidev $1)
82 if [ -z "$part" ]; then
83 echo "mtd_get_mac_binary: ubi volume $mtdname not found!" >&2
87 hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' /dev/$part 2>/dev/null
92 local first dev size erasesize name
93 while read dev size erasesize name; do
94 name=${name#'"'}; name=${name%'"'}
95 if [ "$name" = "$part_name" ]; then
105 local oui=${mac%:*:*:*}
106 local nic=${mac#*:*:*:}
108 nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
112 macaddr_setbit_la() {
115 printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
121 echo -ne \\x${mac//:/\\x}
124 macaddr_canonicalize() {
128 mac=$(echo -n $mac | tr -d \")
129 [ ${#mac} -gt 17 ] && return
130 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
132 for octet in ${mac//[\.:-]/ }; do
140 octet="${octet:0:2} ${octet:2:2}"
143 octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
149 canon=${canon}${canon:+ }${octet}
152 [ ${#canon} -ne 17 ] && return
154 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null