base-files: provide option to specify label MAC address in board.d
[openwrt/openwrt.git] / package / base-files / files / lib / functions / system.sh
1 # Copyright (C) 2006-2013 OpenWrt.org
2
3 get_mac_binary() {
4 local path="$1"
5 local offset="$2"
6
7 if ! [ -e "$path" ]; then
8 echo "get_mac_binary: file $path not found!" >&2
9 return
10 fi
11
12 hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' $path 2>/dev/null
13 }
14
15 get_mac_label() {
16 local basepath="/proc/device-tree"
17 local macdevice="$(cat "$basepath/aliases/label-mac-device" 2>/dev/null)"
18 local macaddr
19
20 [ -n "$macdevice" ] && macaddr=$(get_mac_binary "$basepath/$macdevice/mac-address" 0 2>/dev/null)
21 [ -n "$macaddr" ] || macaddr=$(get_mac_binary "$basepath/$macdevice/local-mac-address" 0 2>/dev/null)
22 [ -n "$macaddr" ] || macaddr=$(uci -q get system.@system[0].label_macaddr)
23 echo $macaddr
24 }
25
26 find_mtd_chardev() {
27 local INDEX=$(find_mtd_index "$1")
28 local PREFIX=/dev/mtd
29
30 [ -d /dev/mtd ] && PREFIX=/dev/mtd/
31 echo "${INDEX:+$PREFIX$INDEX}"
32 }
33
34 mtd_get_mac_ascii() {
35 local mtdname="$1"
36 local key="$2"
37 local part
38 local mac_dirty
39
40 part=$(find_mtd_part "$mtdname")
41 if [ -z "$part" ]; then
42 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
43 return
44 fi
45
46 mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
47
48 # "canonicalize" mac
49 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
50 }
51
52 mtd_get_mac_text() {
53 local mtdname=$1
54 local offset=$(($2))
55 local part
56 local mac_dirty
57
58 part=$(find_mtd_part "$mtdname")
59 if [ -z "$part" ]; then
60 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
61 return
62 fi
63
64 if [ -z "$offset" ]; then
65 echo "mtd_get_mac_text: offset missing!" >&2
66 return
67 fi
68
69 mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
70
71 # "canonicalize" mac
72 [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
73 }
74
75 mtd_get_mac_binary() {
76 local mtdname="$1"
77 local offset="$2"
78 local part
79
80 part=$(find_mtd_part "$mtdname")
81 get_mac_binary "$part" "$offset"
82 }
83
84 mtd_get_mac_binary_ubi() {
85 local mtdname="$1"
86 local offset="$2"
87
88 . /lib/upgrade/nand.sh
89
90 local ubidev=$(nand_find_ubi $CI_UBIPART)
91 local part=$(nand_find_volume $ubidev $1)
92
93 get_mac_binary "/dev/$part" "$offset"
94 }
95
96 mtd_get_part_size() {
97 local part_name=$1
98 local first dev size erasesize name
99 while read dev size erasesize name; do
100 name=${name#'"'}; name=${name%'"'}
101 if [ "$name" = "$part_name" ]; then
102 echo $((0x$size))
103 break
104 fi
105 done < /proc/mtd
106 }
107
108 macaddr_add() {
109 local mac=$1
110 local val=$2
111 local oui=${mac%:*:*:*}
112 local nic=${mac#*:*:*:}
113
114 nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
115 echo $oui:$nic
116 }
117
118 macaddr_setbit_la() {
119 local mac=$1
120
121 printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
122 }
123
124 macaddr_2bin() {
125 local mac=$1
126
127 echo -ne \\x${mac//:/\\x}
128 }
129
130 macaddr_canonicalize() {
131 local mac="$1"
132 local canon=""
133
134 mac=$(echo -n $mac | tr -d \")
135 [ ${#mac} -gt 17 ] && return
136 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
137
138 for octet in ${mac//[\.:-]/ }; do
139 case "${#octet}" in
140 1)
141 octet="0${octet}"
142 ;;
143 2)
144 ;;
145 4)
146 octet="${octet:0:2} ${octet:2:2}"
147 ;;
148 12)
149 octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
150 ;;
151 *)
152 return
153 ;;
154 esac
155 canon=${canon}${canon:+ }${octet}
156 done
157
158 [ ${#canon} -ne 17 ] && return
159
160 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
161 }