1799b5ee9Sopenharmony_ci#!/bin/bash
2799b5ee9Sopenharmony_ci
3799b5ee9Sopenharmony_ci#Copyright (c) 2020-2021 Huawei Device Co., Ltd.
4799b5ee9Sopenharmony_ci#Licensed under the Apache License, Version 2.0 (the "License");
5799b5ee9Sopenharmony_ci#you may not use this file except in compliance with the License.
6799b5ee9Sopenharmony_ci#You may obtain a copy of the License at
7799b5ee9Sopenharmony_ci#
8799b5ee9Sopenharmony_ci#    http://www.apache.org/licenses/LICENSE-2.0
9799b5ee9Sopenharmony_ci#
10799b5ee9Sopenharmony_ci#Unless required by applicable law or agreed to in writing, software
11799b5ee9Sopenharmony_ci#distributed under the License is distributed on an "AS IS" BASIS,
12799b5ee9Sopenharmony_ci#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13799b5ee9Sopenharmony_ci#See the License for the specific language governing permissions and
14799b5ee9Sopenharmony_ci#limitations under the License.
15799b5ee9Sopenharmony_ci
16799b5ee9Sopenharmony_ciset -e
17799b5ee9Sopenharmony_ci
18799b5ee9Sopenharmony_ciboard=$(cat ohos_config.json | grep -oP '(?<="board": ")[^"]*' | sed -e 's/\\//g')
19799b5ee9Sopenharmony_cikernel_type=$(cat ohos_config.json | grep -oP '(?<="kernel": ")[^"]*' | sed -e 's/\\//g')
20799b5ee9Sopenharmony_ciproduct=$(cat ohos_config.json | grep -oP '(?<="product": ")[^"]*' | sed -e 's/\\//g')
21799b5ee9Sopenharmony_ciproduct_path=$(cat ohos_config.json | grep -oP '(?<="product_path": ")[^"]*' | sed -e 's/\\//g')
22799b5ee9Sopenharmony_ci
23799b5ee9Sopenharmony_cireal_cmd="${product_path}/qemu_run.sh"
24799b5ee9Sopenharmony_ciif [[ ! -x "${real_cmd}" ]]; then
25799b5ee9Sopenharmony_ci    echo -e "Failed: ${real_cmd}"
26799b5ee9Sopenharmony_ci    echo -e "Using 'hb set' to choose supported QEMU board\n"
27799b5ee9Sopenharmony_ci    exit
28799b5ee9Sopenharmony_cifi
29799b5ee9Sopenharmony_ciecho -e "board: ${board}\n"
30799b5ee9Sopenharmony_ci
31799b5ee9Sopenharmony_cirebuild_image="no"
32799b5ee9Sopenharmony_cinet_enable="no"
33799b5ee9Sopenharmony_civnc_enable="yes"
34799b5ee9Sopenharmony_ciadd_boot_args="no"
35799b5ee9Sopenharmony_ciboot_args="no"
36799b5ee9Sopenharmony_cigdb_enable="no"
37799b5ee9Sopenharmony_ciqemu_test="no"
38799b5ee9Sopenharmony_citest_file="out/$board/$product/test_result.txt"
39799b5ee9Sopenharmony_cielf_file="Invalid"
40799b5ee9Sopenharmony_ciqemu_help="no"
41799b5ee9Sopenharmony_ci
42799b5ee9Sopenharmony_ci################### qemu-run options #############################
43799b5ee9Sopenharmony_cihelp_info=$(cat <<-END
44799b5ee9Sopenharmony_ciUsage: qemu-run [OPTION]...
45799b5ee9Sopenharmony_ciRun a OHOS image in qemu according to the options.
46799b5ee9Sopenharmony_ci
47799b5ee9Sopenharmony_ci    Options:
48799b5ee9Sopenharmony_ci
49799b5ee9Sopenharmony_ci    -e,  --exec file_name           kernel exec file name
50799b5ee9Sopenharmony_ci    -f,  --force                    rebuild exec file
51799b5ee9Sopenharmony_ci    -l,  --local-desktop            no VNC
52799b5ee9Sopenharmony_ci    -b,  --bootargs boot_arguments  additional boot arguments(-bk1=v1,k2=v2...)
53799b5ee9Sopenharmony_ci    -n,  --net-enable               enable net
54799b5ee9Sopenharmony_ci    -g,  --gdb                      enable gdb for kernel
55799b5ee9Sopenharmony_ci    -t,  --test                     test mode, exclusive with -g
56799b5ee9Sopenharmony_ci    -h,  --help                     print help info
57799b5ee9Sopenharmony_ciEND
58799b5ee9Sopenharmony_ci)
59799b5ee9Sopenharmony_ci
60799b5ee9Sopenharmony_ci############################## main ##############################
61799b5ee9Sopenharmony_ciARGS=`getopt -o e:b:flngth -l force,net-enable,local-desktop,bootargs:,exec:,gdb,test,help -n "$0" -- "$@"`
62799b5ee9Sopenharmony_ciif [ $? != 0 ]; then
63799b5ee9Sopenharmony_ci    echo "Try '$0 --help' for more information."
64799b5ee9Sopenharmony_ci    exit 1
65799b5ee9Sopenharmony_cifi
66799b5ee9Sopenharmony_cieval set --"${ARGS}"
67799b5ee9Sopenharmony_ci
68799b5ee9Sopenharmony_ciwhile true;do
69799b5ee9Sopenharmony_ci    case "${1}" in
70799b5ee9Sopenharmony_ci        -e|--exec)
71799b5ee9Sopenharmony_ci        elf_file="${2}"
72799b5ee9Sopenharmony_ci        shift;
73799b5ee9Sopenharmony_ci        shift;
74799b5ee9Sopenharmony_ci        ;;
75799b5ee9Sopenharmony_ci        -f|--force)
76799b5ee9Sopenharmony_ci        rebuild_image=yes
77799b5ee9Sopenharmony_ci        shift;
78799b5ee9Sopenharmony_ci        ;;
79799b5ee9Sopenharmony_ci        -l|--local-desktop)
80799b5ee9Sopenharmony_ci        vnc_enable=no
81799b5ee9Sopenharmony_ci        shift;
82799b5ee9Sopenharmony_ci        ;;
83799b5ee9Sopenharmony_ci        -b|--bootargs)
84799b5ee9Sopenharmony_ci        add_boot_args=yes
85799b5ee9Sopenharmony_ci        boot_args="${2}"
86799b5ee9Sopenharmony_ci        shift;
87799b5ee9Sopenharmony_ci        shift;
88799b5ee9Sopenharmony_ci        ;;
89799b5ee9Sopenharmony_ci        -n|--net-enable)
90799b5ee9Sopenharmony_ci        net_enable=yes
91799b5ee9Sopenharmony_ci        shift;
92799b5ee9Sopenharmony_ci        ;;
93799b5ee9Sopenharmony_ci        -t|--test)
94799b5ee9Sopenharmony_ci        qemu_test="test"
95799b5ee9Sopenharmony_ci        shift;
96799b5ee9Sopenharmony_ci        ;;
97799b5ee9Sopenharmony_ci        -g|--gdb)
98799b5ee9Sopenharmony_ci        gdb_enable=yes
99799b5ee9Sopenharmony_ci        shift;
100799b5ee9Sopenharmony_ci        echo -e "Qemu kernel gdb enable..."
101799b5ee9Sopenharmony_ci        ;;
102799b5ee9Sopenharmony_ci        -h|--help)
103799b5ee9Sopenharmony_ci        shift;
104799b5ee9Sopenharmony_ci        qemu_help=yes
105799b5ee9Sopenharmony_ci        break;
106799b5ee9Sopenharmony_ci        ;;
107799b5ee9Sopenharmony_ci        --)
108799b5ee9Sopenharmony_ci        shift;
109799b5ee9Sopenharmony_ci        break;
110799b5ee9Sopenharmony_ci        ;;
111799b5ee9Sopenharmony_ci    esac
112799b5ee9Sopenharmony_cidone
113799b5ee9Sopenharmony_ci
114799b5ee9Sopenharmony_ciif [ "$qemu_test" = "test" ] && [ "$gdb_enable" = "yes" ]; then
115799b5ee9Sopenharmony_ci  echo "Error: '-g' '-t' options cannot be used together"
116799b5ee9Sopenharmony_ci  exit 2
117799b5ee9Sopenharmony_cifi
118799b5ee9Sopenharmony_ci
119799b5ee9Sopenharmony_ci############### qemu test #########################
120799b5ee9Sopenharmony_cifunction test_success() {
121799b5ee9Sopenharmony_ci    echo "Test success!!!"
122799b5ee9Sopenharmony_ci    exit 0
123799b5ee9Sopenharmony_ci}
124799b5ee9Sopenharmony_ci
125799b5ee9Sopenharmony_cifunction test_failed() {
126799b5ee9Sopenharmony_ci    cat $test_file
127799b5ee9Sopenharmony_ci    echo "Test failed!!!"
128799b5ee9Sopenharmony_ci    exit 1
129799b5ee9Sopenharmony_ci}
130799b5ee9Sopenharmony_ci
131799b5ee9Sopenharmony_cifunction start_qemu_test() {
132799b5ee9Sopenharmony_ci    if [ "$kernel_type" = "liteos_m" ]; then
133799b5ee9Sopenharmony_ci        if [ ! -f "$test_file" ]; then
134799b5ee9Sopenharmony_ci            test_failed
135799b5ee9Sopenharmony_ci        else
136799b5ee9Sopenharmony_ci            result=`tail -1 $test_file`
137799b5ee9Sopenharmony_ci            if [ "$result" != "--- Test End ---" ]; then
138799b5ee9Sopenharmony_ci                test_failed
139799b5ee9Sopenharmony_ci            fi
140799b5ee9Sopenharmony_ci            result=`tail -2 $test_file`
141799b5ee9Sopenharmony_ci            failedresult=${result%,*}
142799b5ee9Sopenharmony_ci            failed=${failedresult%:*}
143799b5ee9Sopenharmony_ci            if [ "$failed" != "failed count" ]; then
144799b5ee9Sopenharmony_ci                test_failed
145799b5ee9Sopenharmony_ci            fi
146799b5ee9Sopenharmony_ci            failedcount=${failedresult#*:}
147799b5ee9Sopenharmony_ci            if [ "$failedcount" = "0" ]; then
148799b5ee9Sopenharmony_ci                test_success
149799b5ee9Sopenharmony_ci            else
150799b5ee9Sopenharmony_ci                test_failed
151799b5ee9Sopenharmony_ci            fi
152799b5ee9Sopenharmony_ci       fi
153799b5ee9Sopenharmony_ci    else
154799b5ee9Sopenharmony_ci        echo "The kernel does not support the -t/--test option!";
155799b5ee9Sopenharmony_ci    fi
156799b5ee9Sopenharmony_ci}
157799b5ee9Sopenharmony_ci
158799b5ee9Sopenharmony_cifunction kill_specified_process(){
159799b5ee9Sopenharmony_ci    qemu_name=$1
160799b5ee9Sopenharmony_ci    while true
161799b5ee9Sopenharmony_ci    do
162799b5ee9Sopenharmony_ci        pid=`ps -ef | grep $qemu_name | grep -v grep | awk '{print $2}' | head -n 1`
163799b5ee9Sopenharmony_ci        if [ "$pid" == "" ]; then
164799b5ee9Sopenharmony_ci            break
165799b5ee9Sopenharmony_ci        fi
166799b5ee9Sopenharmony_ci        kill -15 $pid
167799b5ee9Sopenharmony_ci    done
168799b5ee9Sopenharmony_ci}
169799b5ee9Sopenharmony_ci
170799b5ee9Sopenharmony_cifunction start_qemu_monitor() {
171799b5ee9Sopenharmony_ci    if [ "$kernel_type" = "liteos_m" ]; then
172799b5ee9Sopenharmony_ci        kill_specified_process qemu_mini_test_
173799b5ee9Sopenharmony_ci        kill_specified_process qemu-system-
174799b5ee9Sopenharmony_ci        ./vendor/ohemu/common/qemu_mini_test_monitor.sh $test_file &
175799b5ee9Sopenharmony_ci    fi
176799b5ee9Sopenharmony_ci}
177799b5ee9Sopenharmony_ci
178799b5ee9Sopenharmony_ci######### qemu_run.sh parameters ######
179799b5ee9Sopenharmony_ci# The order of parameters has strict requirements #
180799b5ee9Sopenharmony_ciqemu_parameters="\
181799b5ee9Sopenharmony_ci $elf_file \
182799b5ee9Sopenharmony_ci $rebuild_image \
183799b5ee9Sopenharmony_ci $vnc_enable \
184799b5ee9Sopenharmony_ci $add_boot_args $boot_args \
185799b5ee9Sopenharmony_ci $net_enable \
186799b5ee9Sopenharmony_ci $gdb_enable \
187799b5ee9Sopenharmony_ci $qemu_test $test_file \
188799b5ee9Sopenharmony_ci $qemu_help"
189799b5ee9Sopenharmony_ci
190799b5ee9Sopenharmony_cifunction start_qemu() {
191799b5ee9Sopenharmony_ci    set +e
192799b5ee9Sopenharmony_ci    if [ $qemu_help = no ]; then
193799b5ee9Sopenharmony_ci        read -t 5 -p "Enter to start qemu[y/n]:" flag
194799b5ee9Sopenharmony_ci        set -e
195799b5ee9Sopenharmony_ci        start=${flag:-y}
196799b5ee9Sopenharmony_ci    fi
197799b5ee9Sopenharmony_ci    if [ $qemu_help = yes ] || [ ${start} = y ]; then
198799b5ee9Sopenharmony_ci        if [ "$qemu_test" = "test" ]; then
199799b5ee9Sopenharmony_ci            start_qemu_monitor
200799b5ee9Sopenharmony_ci        fi
201799b5ee9Sopenharmony_ci
202799b5ee9Sopenharmony_ci        $real_cmd $qemu_parameters
203799b5ee9Sopenharmony_ci
204799b5ee9Sopenharmony_ci        if [ "$qemu_test" = "test" ]; then
205799b5ee9Sopenharmony_ci            start_qemu_test
206799b5ee9Sopenharmony_ci        fi
207799b5ee9Sopenharmony_ci    else
208799b5ee9Sopenharmony_ci        echo "Exit qemu-run"
209799b5ee9Sopenharmony_ci    fi
210799b5ee9Sopenharmony_ci}
211799b5ee9Sopenharmony_ci
212799b5ee9Sopenharmony_cistart_qemu
213