summaryrefslogtreecommitdiff
path: root/automated/linux/mbl/mbl_kernel_test_util.sh
blob: 8dd74613dbe9a93b830387ab991a24585464a917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash


with_timestamp=0
with_color=0
keep_going=0
_needed_commands='which grep date cat echo tr'

declare -A _supported_machines=( [warp]=i.MX7 [rpi3]=BCM2709 [bananapi]=H2+ )

_print() {
    local _color_prefix=''
    local _color_sufix=''
    local _timestamp=''
    (($with_color > 0)) && { _color_prefix="\e[1;${2}m" ; _color_sufix="\e[0m" ; }

    if (( ${with_timestamp} > 0 )); then
        _date=$(date "+%Y%m%d_%H%M%S")
        _timestamp="[${_date}]: "
    fi
    [[ ! -z ${3} ]] && opt="-en" || opt="-e"

    echo $opt "${_color_prefix} ${_timestamp} ${1} ${_color_sufix}"
}

print_blue() {
    _print "$1" 34 n
}

print_red() {
    _print "$1" 31
}

print_yellow() {
    _print "$1" 33
}

print_green() {
    _print "$1" 32
}

print_ok() {
    _print "[OK]" 32
}

print_nok() {
    _print "***NOK***" 31
}

print_skip() {
    _print "$1 ***SKIP***" 31
}

print_pass() {
    _print "$1 ...................................... [PASS]" 32
}

print_die() {
    print_red "$1"
    exit 2
}

print_fail() {
    print_red "$1 ...................................... [FAIL]"
}


print_skipped() {
    print_yellow "$1 ...................................... [SKIP]"
}

print_fatal() {
    if (($keep_going > 0)); then
        print_red "$1"
    else
        print_die "$1"
    fi
}

print_yn() {
    print_blue "$1"
    read -p "[y/n]? "
    [[ "$REPLY" == "y" ]] && return 0 || return 1
}

path_check() {
    local ret_final=0

    for p in ${@}; do
	print_blue "Checking path exists ${p}..."
	[[ -d ${p} ]] && ret=0 || ret=-22
	ret=$?; (($ret == 0)) && print_ok || print_nok
	(($ret != 0)) && (($ret_final == 0)) && ret_final=-22
    done

    return $ret_final
}

zgrep_config() {
    local needed_commands='zcat'
    local options=${@}
    local kernel_config="/proc/config.gz"
    local zout=''
    local ret_final=0

    if [[ ! -f ${kernel_config} ]]; then
	 print_red "Kernel Config File ${kernel_config} does not exist..."
	 print_nok
	 return -22
    fi

    check_commands ${needed_commands}
    ret=$?; (($ret != 0)) && return $ret

    for c in ${@}; do
	print_blue "Checking config option ${c}..."
	zout=$(zcat ${kernel_config} | grep "CONFIG_${c}=[y|m]")
	ret=$?; (($ret == 0)) && print_ok || print_nok
	(($ret != 0)) && (($ret_final == 0)) && ret_final=-22
    done

    return $ret_final
}

assign() {
    export "$1"="$2"
}

contains_exactly() {
    local e; for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done; return 1;
}

contains() {
    local e; for e in "${@:2}"; do [[ "$e" == *"$1"* ]] && return 0; done; return 1;
}

check_bash_version() {
    [[ ! -z ${bash_version_major_required} ]] && ((${BASH_VERSINFO[0]} < ${bash_version_major_required})); ret=$?
    (( $ret == 0 )) && print_die "Sorry!! you need at least bash major version ${bash_version_major_required} to run this script"
    [[ ! -z ${bash_version_minor_required} ]] && ((${BASH_VERSINFO[1]} < ${bash_version_minor_required}))
    (( $ret == 0 )) && print_die "Sorry!! you need at least bash major version ${bash_version_minor_required} to run this script"

    return 0
}

check_commands() {
    local needed_commands="${@} ${_needed_commands}"
    missing_counter=0
    for needed_command in $needed_commands; do
        if ! hash "${needed_command}" >/dev/null 2>&1; then
            print_red "\n  Command not found in PATH: $needed_command" >&2
            ((missing_counter++))
        fi
    done

    if ((missing_counter > 0)); then
        print_red "Minimum $missing_counter commands are missing in PATH,\
            \n  please install them in your host machine aborting " >&2
	return 127
    fi
}

check_machine() {
    local hostname=$(cat /etc/hostname)
    local hardware=$(cat /proc/cpuinfo | grep Hardware)

    # try to find machine using the hostname
    for machine in ${!_supported_machines[@]}; do
	[[ ${hostname} == *${machine}* ]] && break || machine=''
    done

    # we could get the machine type from hostname, no need for other check #
    [[ ! -z ${machine} ]] && return

    # if hostname was changed, try using the cpuinfo
    for m in ${!_supported_machines[@]}; do
	[[ ${hardware} == *${_supported_machines[$m]}* ]] && machine=$m && break || machine=''
    done

    [[ -z ${machine} ]] && print_die "Failed to find the running machine"
}

set_subsystem_dev() {
    local subsys_dev_name=subsystem_dev_${subsystem}
    subsys_dev=${!subsys_dev_name}

    [[ -z ${subsys_dev} ]] && subsys_dev_name=subsystem_dev_${subsystem}_${machine}
    subsys_dev=${!subsys_dev_name}

    [[ -z ${subsys_dev} ]] && return 22 || return 0
}

set_subsystem_dev_path() {
    local subsys_path_name=subsystem_path_${subsystem}
    local subsys_path=${!subsys_path_name}

    [[ -z ${subsys_path} ]] && subsys_path_name=subsystem_dev_${subsystem}_${machine}
    subsys_path=${!subsys_path_name}

    find_out=$(grep -l ${subsys_dev} ${subsys_path}/*/name 2>/dev/null)
    subsys_dev_path=${find_out%/*}
    [[ -z ${subsys_dev_path} ]] || return 0

    find_out=$(grep -l ${subsys_dev} ${subsys_path}/*/id 2>/dev/null)
    subsys_dev_path=${find_out%/*}
    [[ -z ${subsys_dev_path} ]] && return 22 || return 0
}