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