treewide: drop shebang from non-executable target files
[openwrt/openwrt.git] / target / linux / ath79 / generic / base-files / lib / functions / k2t.sh
1 #
2 # Copyright (C) 2018 Weijie Gao <hackpascal@gmail.com>
3 #
4 # Helper function to extract mac addresses from mtd part for Phicomm K2T
5 #
6
7 . /lib/functions.sh
8 . /lib/functions/system.sh
9 . /usr/share/libubox/jshn.sh
10
11 k2t_config_load() {
12 local mtd_blk=$(find_mtd_part config)
13
14 if [ -z "$mtd_blk" ]; then
15 echo "k2t_config_load: no mtd part named config" >&2
16 exit 1
17 fi
18
19 local json_size=$(dd if=$mtd_blk bs=1 count=8 2>/dev/null)
20
21 json_size="0x$json_size"
22 json_size=$((json_size))
23
24 if [ "$?" -ne 0 ]; then
25 echo "k2t_config_load: invalid json data size" >&2
26 exit 2
27 fi
28
29 if [ "$json_size" -eq 0 ]; then
30 echo "k2t_config_load: empty json data" >&2
31 exit 3
32 fi
33
34 local json_data=$(dd if=$mtd_blk bs=1 skip=8 count=$json_size 2>/dev/null)
35
36 json_load "$json_data"
37 }
38
39 k2t_get_mac() {
40 local old_ns
41
42 json_set_namespace "k2t" old_ns
43
44 if k2t_config_load; then
45 json_select "this_dev_info"
46 json_get_var val "$1"
47 json_select ..
48 fi
49
50 json_set_namespace old_ns
51
52 echo $val
53 }
54