• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3#Copyright (c) 2020-2021 Huawei Device Co., Ltd.
4#Licensed under the Apache License, Version 2.0 (the "License");
5#you may not use this file except in compliance with the License.
6#You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10#Unless required by applicable law or agreed to in writing, software
11#distributed under the License is distributed on an "AS IS" BASIS,
12#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#See the License for the specific language governing permissions and
14#limitations under the License.
15
16set -e
17
18board=$(cat ohos_config.json | grep -oP '(?<="board": ")[^"]*' | sed -e 's/\\//g')
19kernel_type=$(cat ohos_config.json | grep -oP '(?<="kernel": ")[^"]*' | sed -e 's/\\//g')
20product=$(cat ohos_config.json | grep -oP '(?<="product": ")[^"]*' | sed -e 's/\\//g')
21product_path=$(cat ohos_config.json | grep -oP '(?<="product_path": ")[^"]*' | sed -e 's/\\//g')
22
23real_cmd="${product_path}/qemu_run.sh"
24if [[ ! -x "${real_cmd}" ]]; then
25    echo -e "Failed: ${real_cmd}"
26    echo -e "Using 'hb set' to choose supported QEMU board\n"
27    exit
28fi
29echo -e "board: ${board}\n"
30
31rebuild_image="no"
32net_enable="no"
33vnc_enable="yes"
34add_boot_args="no"
35boot_args="no"
36gdb_enable="no"
37qemu_test="no"
38test_file="out/$board/$product/test_result.txt"
39elf_file="Invalid"
40qemu_help="no"
41
42################### qemu-run options #############################
43help_info=$(cat <<-END
44Usage: qemu-run [OPTION]...
45Run a OHOS image in qemu according to the options.
46
47    Options:
48
49    -e,  --exec file_name           kernel exec file name
50    -f,  --force                    rebuild exec file
51    -l,  --local-desktop            no VNC
52    -b,  --bootargs boot_arguments  additional boot arguments(-bk1=v1,k2=v2...)
53    -n,  --net-enable               enable net
54    -g,  --gdb                      enable gdb for kernel
55    -t,  --test                     test mode, exclusive with -g
56    -h,  --help                     print help info
57END
58)
59
60############################## main ##############################
61ARGS=`getopt -o e:b:flngth -l force,net-enable,local-desktop,bootargs:,exec:,gdb,test,help -n "$0" -- "$@"`
62if [ $? != 0 ]; then
63    echo "Try '$0 --help' for more information."
64    exit 1
65fi
66eval set --"${ARGS}"
67
68while true;do
69    case "${1}" in
70        -e|--exec)
71        elf_file="${2}"
72        shift;
73        shift;
74        ;;
75        -f|--force)
76        rebuild_image=yes
77        shift;
78        ;;
79        -l|--local-desktop)
80        vnc_enable=no
81        shift;
82        ;;
83        -b|--bootargs)
84        add_boot_args=yes
85        boot_args="${2}"
86        shift;
87        shift;
88        ;;
89        -n|--net-enable)
90        net_enable=yes
91        shift;
92        ;;
93        -t|--test)
94        qemu_test="test"
95        shift;
96        ;;
97        -g|--gdb)
98        gdb_enable=yes
99        shift;
100        echo -e "Qemu kernel gdb enable..."
101        ;;
102        -h|--help)
103        shift;
104        qemu_help=yes
105        break;
106        ;;
107        --)
108        shift;
109        break;
110        ;;
111    esac
112done
113
114if [ "$qemu_test" = "test" ] && [ "$gdb_enable" = "yes" ]; then
115  echo "Error: '-g' '-t' options cannot be used together"
116  exit 2
117fi
118
119############### qemu test #########################
120function test_success() {
121    echo "Test success!!!"
122    exit 0
123}
124
125function test_failed() {
126    cat $test_file
127    echo "Test failed!!!"
128    exit 1
129}
130
131function start_qemu_test() {
132    if [ "$kernel_type" = "liteos_m" ]; then
133        if [ ! -f "$test_file" ]; then
134            test_failed
135        else
136            result=`tail -1 $test_file`
137            if [ "$result" != "--- Test End ---" ]; then
138                test_failed
139            fi
140            result=`tail -2 $test_file`
141            failedresult=${result%,*}
142            failed=${failedresult%:*}
143            if [ "$failed" != "failed count" ]; then
144                test_failed
145            fi
146            failedcount=${failedresult#*:}
147            if [ "$failedcount" = "0" ]; then
148                test_success
149            else
150                test_failed
151            fi
152       fi
153    else
154        echo "The kernel does not support the -t/--test option!";
155    fi
156}
157
158function kill_specified_process(){
159    qemu_name=$1
160    while true
161    do
162        pid=`ps -ef | grep $qemu_name | grep -v grep | awk '{print $2}' | head -n 1`
163        if [ "$pid" == "" ]; then
164            break
165        fi
166        kill -15 $pid
167    done
168}
169
170function start_qemu_monitor() {
171    if [ "$kernel_type" = "liteos_m" ]; then
172        kill_specified_process qemu_mini_test_
173        kill_specified_process qemu-system-
174        ./vendor/ohemu/common/qemu_mini_test_monitor.sh $test_file &
175    fi
176}
177
178######### qemu_run.sh parameters ######
179# The order of parameters has strict requirements #
180qemu_parameters="\
181 $elf_file \
182 $rebuild_image \
183 $vnc_enable \
184 $add_boot_args $boot_args \
185 $net_enable \
186 $gdb_enable \
187 $qemu_test $test_file \
188 $qemu_help"
189
190function start_qemu() {
191    set +e
192    if [ $qemu_help = no ]; then
193        read -t 5 -p "Enter to start qemu[y/n]:" flag
194        set -e
195        start=${flag:-y}
196    fi
197    if [ $qemu_help = yes ] || [ ${start} = y ]; then
198        if [ "$qemu_test" = "test" ]; then
199            start_qemu_monitor
200        fi
201
202        $real_cmd $qemu_parameters
203
204        if [ "$qemu_test" = "test" ]; then
205            start_qemu_test
206        fi
207    else
208        echo "Exit qemu-run"
209    fi
210}
211
212start_qemu
213