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
17elf_file=$1
18rebuild_image=$2
19vnc_enable=$3
20add_boot_args=$4
21boot_args=$5
22net_enable=$6
23gdb_enable=$7
24qemu_test=$8
25test_file=$9
26qemu_help=${10}
27
28vnc="-vnc :20  -serial mon:stdio"
29qemu_option=""
30fsimg="vendor/ohemu/qemu_riscv32_mini_system_demo/fs-storage.img"
31
32#### Submit code to CI test command, do not modify #####
33if [ "$qemu_test" = "test" ]; then
34    qemu_option+="-serial file:$test_file"
35fi
36
37if [ "$elf_file" = "Invalid" ]; then
38    elf_file=out/riscv32_virt/qemu_riscv_mini_system_demo/OHOS_Image
39fi
40
41help_info=$(cat <<-END
42Usage: qemu-run [OPTION]...
43Run a OHOS image in qemu according to the options.
44
45    Options:
46
47    -e,  --exec file_name     kernel exec file name
48    -n,  --net-enable         enable net
49    -l,  --local-desktop      no VNC
50    -g,  --gdb                enable gdb for kernel
51    -t,  --test               test mode, exclusive with -g
52    -h,  --help               print help info
53
54    By default, the kernel exec file is: ${elf_file}.
55END
56)
57
58if [ "$qemu_help" = "yes" ]; then
59    echo "${help_info}"
60    exit 0
61fi
62
63if [ "$vnc_enable" = "no" ]; then
64    vnc=""
65fi
66
67if [ "$gdb_enable" = "yes" ]; then
68    qemu_option+="-s -S"
69fi
70
71function unsupported_parameters_check(){
72    if [ "$rebuild_image" = "yes" ]; then
73        echo "Error: The -f|--force option is not supported !"
74        echo "${help_info}"
75        exit 1
76    fi
77
78    if [ "$add_boot_args" = "yes" ]; then
79        echo "Error: The -b|--bootargs option is not supported !"
80        echo "${help_info}"
81        exit 1
82    fi
83}
84
85function net_config(){
86    echo "Network config..."
87    set +e
88    sudo modprobe tun tap
89    sudo ip link add br0 type bridge
90    sudo ip address add 10.0.2.2/24 dev br0
91    sudo ip link set dev br0 up
92    set -e
93}
94
95function start_qemu(){
96    net_enable=${1}
97    if [ ${net_enable} = yes ]; then
98        net_config 2>/dev/null
99        sudo `which qemu-system-riscv32` -M virt -m 128M -bios none -kernel $elf_file \
100        -global virtio-mmio.force-legacy=false \
101        $qemu_option \
102        -netdev bridge,id=net0 \
103        -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
104        -device virtio-gpu-device,xres=800,yres=480 -device virtio-tablet-device ${vnc} \
105        -drive if=pflash,file=$fsimg,format=raw,index=1 \
106        -append "root=/dev/vda or console=ttyS0"
107    else
108        qemu-system-riscv32 -M virt -m 128M -bios none -kernel $elf_file \
109        -global virtio-mmio.force-legacy=false \
110        $qemu_option \
111        -device virtio-gpu-device,xres=800,yres=480 -device virtio-tablet-device ${vnc} \
112        -drive if=pflash,file=$fsimg,format=raw,index=1 \
113        -append "root=/dev/vda or console=ttyS0"
114    fi
115}
116
117unsupported_parameters_check
118
119start_qemu ${net_enable}
120