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