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