base-files: don't store label MAC address in uci system config
[oweals/openwrt.git] / package / base-files / files / lib / functions / system.sh
1 # Copyright (C) 2006-2013 OpenWrt.org
2
3 . /usr/share/libubox/jshn.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 get_mac_label_dt() {
18         local basepath="/proc/device-tree"
19         local macdevice="$(cat "$basepath/aliases/label-mac-device" 2>/dev/null)"
20         local macaddr
21
22         [ -n "$macdevice" ] || return
23
24         macaddr=$(get_mac_binary "$basepath/$macdevice/mac-address" 0 2>/dev/null)
25         [ -n "$macaddr" ] || macaddr=$(get_mac_binary "$basepath/$macdevice/local-mac-address" 0 2>/dev/null)
26
27         echo $macaddr
28 }
29
30 get_mac_label_json() {
31         local cfg="/etc/board.json"
32         local macaddr
33
34         [ -s "$cfg" ] || return
35
36         json_init
37         json_load "$(cat $cfg)"
38         if json_is_a system object; then
39                 json_select system
40                         json_get_var macaddr label_macaddr
41                 json_select ..
42         fi
43
44         echo $macaddr
45 }
46
47 get_mac_label() {
48         local macaddr=$(get_mac_label_dt)
49
50         [ -n "$macaddr" ] || macaddr=$(get_mac_label_json)
51
52         echo $macaddr
53 }
54
55 find_mtd_chardev() {
56         local INDEX=$(find_mtd_index "$1")
57         local PREFIX=/dev/mtd
58
59         [ -d /dev/mtd ] && PREFIX=/dev/mtd/
60         echo "${INDEX:+$PREFIX$INDEX}"
61 }
62
63 mtd_get_mac_ascii() {
64         local mtdname="$1"
65         local key="$2"
66         local part
67         local mac_dirty
68
69         part=$(find_mtd_part "$mtdname")
70         if [ -z "$part" ]; then
71                 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
72                 return
73         fi
74
75         mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
76
77         # "canonicalize" mac
78         [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
79 }
80
81 mtd_get_mac_text() {
82         local mtdname=$1
83         local offset=$(($2))
84         local part
85         local mac_dirty
86
87         part=$(find_mtd_part "$mtdname")
88         if [ -z "$part" ]; then
89                 echo "mtd_get_mac_text: partition $mtdname not found!" >&2
90                 return
91         fi
92
93         if [ -z "$offset" ]; then
94                 echo "mtd_get_mac_text: offset missing!" >&2
95                 return
96         fi
97
98         mac_dirty=$(dd if="$part" bs=1 skip="$offset" count=17 2>/dev/null)
99
100         # "canonicalize" mac
101         [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
102 }
103
104 mtd_get_mac_binary() {
105         local mtdname="$1"
106         local offset="$2"
107         local part
108
109         part=$(find_mtd_part "$mtdname")
110         get_mac_binary "$part" "$offset"
111 }
112
113 mtd_get_mac_binary_ubi() {
114         local mtdname="$1"
115         local offset="$2"
116
117         . /lib/upgrade/nand.sh
118
119         local ubidev=$(nand_find_ubi $CI_UBIPART)
120         local part=$(nand_find_volume $ubidev $1)
121
122         get_mac_binary "/dev/$part" "$offset"
123 }
124
125 mtd_get_part_size() {
126         local part_name=$1
127         local first dev size erasesize name
128         while read dev size erasesize name; do
129                 name=${name#'"'}; name=${name%'"'}
130                 if [ "$name" = "$part_name" ]; then
131                         echo $((0x$size))
132                         break
133                 fi
134         done < /proc/mtd
135 }
136
137 macaddr_add() {
138         local mac=$1
139         local val=$2
140         local oui=${mac%:*:*:*}
141         local nic=${mac#*:*:*:}
142
143         nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
144         echo $oui:$nic
145 }
146
147 macaddr_setbit_la() {
148         local mac=$1
149
150         printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
151 }
152
153 macaddr_2bin() {
154         local mac=$1
155
156         echo -ne \\x${mac//:/\\x}
157 }
158
159 macaddr_canonicalize() {
160         local mac="$1"
161         local canon=""
162
163         mac=$(echo -n $mac | tr -d \")
164         [ ${#mac} -gt 17 ] && return
165         [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
166
167         for octet in ${mac//[\.:-]/ }; do
168                 case "${#octet}" in
169                 1)
170                         octet="0${octet}"
171                         ;;
172                 2)
173                         ;;
174                 4)
175                         octet="${octet:0:2} ${octet:2:2}"
176                         ;;
177                 12)
178                         octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
179                         ;;
180                 *)
181                         return
182                         ;;
183                 esac
184                 canon=${canon}${canon:+ }${octet}
185         done
186
187         [ ${#canon} -ne 17 ] && return
188
189         printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
190 }