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