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