base-files: Really check path in get_mac_binary
[openwrt/openwrt.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 if [ -z "$part" ]; then
83 echo "mtd_get_mac_binary: ubi volume $mtdname not found!" >&2
84 return
85 fi
86
87 hexdump -v -n 6 -s $offset -e '5/1 "%02x:" 1/1 "%02x"' /dev/$part 2>/dev/null
88 }
89
90 mtd_get_part_size() {
91 local part_name=$1
92 local first dev size erasesize name
93 while read dev size erasesize name; do
94 name=${name#'"'}; name=${name%'"'}
95 if [ "$name" = "$part_name" ]; then
96 echo $((0x$size))
97 break
98 fi
99 done < /proc/mtd
100 }
101
102 macaddr_add() {
103 local mac=$1
104 local val=$2
105 local oui=${mac%:*:*:*}
106 local nic=${mac#*:*:*:}
107
108 nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
109 echo $oui:$nic
110 }
111
112 macaddr_setbit_la() {
113 local mac=$1
114
115 printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
116 }
117
118 macaddr_2bin() {
119 local mac=$1
120
121 echo -ne \\x${mac//:/\\x}
122 }
123
124 macaddr_canonicalize() {
125 local mac="$1"
126 local canon=""
127
128 mac=$(echo -n $mac | tr -d \")
129 [ ${#mac} -gt 17 ] && return
130 [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
131
132 for octet in ${mac//[\.:-]/ }; do
133 case "${#octet}" in
134 1)
135 octet="0${octet}"
136 ;;
137 2)
138 ;;
139 4)
140 octet="${octet:0:2} ${octet:2:2}"
141 ;;
142 12)
143 octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
144 ;;
145 *)
146 return
147 ;;
148 esac
149 canon=${canon}${canon:+ }${octet}
150 done
151
152 [ ${#canon} -ne 17 ] && return
153
154 printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
155 }