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