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 18product_path=`hb env|grep 'product path:'` 19product_path=${product_path:26} 20kernel_type=`hb env|grep 'kernel:'` 21kernel_type=${kernel_type:20} 22board=`hb env|grep 'board:'` 23board=${board:19} 24product=`hb env|grep 'product:'` 25product=${product:21} 26 27real_cmd="${product_path}/qemu_run.sh" 28if [[ ! -x "${real_cmd}" ]]; then 29 echo -e "Failed: ${real_cmd}" 30 echo -e "Using 'hb set' to choose supported QEMU board\n" 31 exit 32fi 33echo -e "board: ${board}\n" 34 35rebuild_image="no" 36net_enable="no" 37vnc_enable="yes" 38add_boot_args="no" 39boot_args="no" 40gdb_enable="no" 41qemu_test="no" 42test_file="out/$board/$product/test_result.txt" 43elf_file="Invalid" 44qemu_help="no" 45 46################### qemu-run options ############################# 47help_info=$(cat <<-END 48Usage: qemu-run [OPTION]... 49Run a OHOS image in qemu according to the options. 50 51 Options: 52 53 -e, --exec file_name kernel exec file name 54 -f, --force rebuild exec file 55 -l, --local-desktop no VNC 56 -b, --bootargs boot_arguments additional boot arguments(-bk1=v1,k2=v2...) 57 -n, --net-enable enable net 58 -g, --gdb enable gdb for kernel 59 -t, --test test mode, exclusive with -g 60 -h, --help print help info 61END 62) 63 64############################## main ############################## 65ARGS=`getopt -o e:b:flngth -l force,net-enable,local-desktop,bootargs:,exec:,gdb,test,help -n "$0" -- "$@"` 66if [ $? != 0 ]; then 67 echo "Try '$0 --help' for more information." 68 exit 1 69fi 70eval set --"${ARGS}" 71 72while true;do 73 case "${1}" in 74 -e|--exec) 75 elf_file="${2}" 76 shift; 77 shift; 78 ;; 79 -f|--force) 80 rebuild_image=yes 81 shift; 82 ;; 83 -l|--local-desktop) 84 vnc_enable=no 85 shift; 86 ;; 87 -b|--bootargs) 88 add_boot_args=yes 89 boot_args="${2}" 90 shift; 91 shift; 92 ;; 93 -n|--net-enable) 94 net_enable=yes 95 shift; 96 ;; 97 -t|--test) 98 qemu_test="test" 99 shift; 100 ;; 101 -g|--gdb) 102 gdb_enable=yes 103 shift; 104 echo -e "Qemu kernel gdb enable..." 105 ;; 106 -h|--help) 107 shift; 108 qemu_help=yes 109 break; 110 ;; 111 --) 112 shift; 113 break; 114 ;; 115 esac 116done 117 118if [ "$qemu_test" = "test" ] && [ "$gdb_enable" = "yes" ]; then 119 echo "Error: '-g' '-t' options cannot be used together" 120 exit 2 121fi 122 123############### qemu test ######################### 124function test_success() { 125 echo "Test success!!!" 126 exit 0 127} 128 129function test_failed() { 130 cat $test_file 131 echo "Test failed!!!" 132 exit 1 133} 134 135function start_qemu_test() { 136 if [ "$kernel_type" = "liteos_m" ]; then 137 if [ ! -f "$test_file" ]; then 138 test_failed 139 else 140 result=`tail -1 $test_file` 141 if [ "$result" != "--- Test End ---" ]; then 142 test_failed 143 fi 144 result=`tail -2 $test_file` 145 failedresult=${result%,*} 146 failed=${failedresult%:*} 147 if [ "$failed" != "failed count" ]; then 148 test_failed 149 fi 150 failedcount=${failedresult#*:} 151 if [ "$failedcount" = "0" ]; then 152 test_success 153 else 154 test_failed 155 fi 156 fi 157 else 158 echo "The kernel does not support the -t/--test option!"; 159 fi 160} 161 162function kill_specified_process(){ 163 qemu_name=$1 164 while true 165 do 166 pid=`ps -ef | grep $qemu_name | grep -v grep | awk '{print $2}' | head -n 1` 167 if [ "$pid" == "" ]; then 168 break 169 fi 170 kill -15 $pid 171 done 172} 173 174function start_qemu_monitor() { 175 if [ "$kernel_type" = "liteos_m" ]; then 176 kill_specified_process qemu_mini_test_ 177 kill_specified_process qemu-system- 178 ./vendor/ohemu/common/qemu_mini_test_monitor.sh $test_file & 179 fi 180} 181 182######### qemu_run.sh parameters ###### 183# The order of parameters has strict requirements # 184qemu_parameters="\ 185 $elf_file \ 186 $rebuild_image \ 187 $vnc_enable \ 188 $add_boot_args $boot_args \ 189 $net_enable \ 190 $gdb_enable \ 191 $qemu_test $test_file \ 192 $qemu_help" 193 194function start_qemu() { 195 set +e 196 if [ $qemu_help = no ]; then 197 read -t 5 -p "Enter to start qemu[y/n]:" flag 198 set -e 199 start=${flag:-y} 200 fi 201 if [ $qemu_help = yes ] || [ ${start} = y ]; then 202 if [ "$qemu_test" = "test" ]; then 203 start_qemu_monitor 204 fi 205 206 $real_cmd $qemu_parameters 207 208 if [ "$qemu_test" = "test" ]; then 209 start_qemu_test 210 fi 211 else 212 echo "Exit qemu-run" 213 fi 214} 215 216start_qemu 217