• 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
16qemu_option="-M virt -cpu cortex-a7 -smp 4 -m 1024 -nographic"
17qemu_setup_network=""
18qemu_instance_id=""
19img_copy_option="-n"
20
21kernel_bootargs="console=ttyAMA0,115200 init=/bin/init hardware=qemu.arm.linux default_boot_device=a003e00.virtio_mmio root=/dev/ram0 rw ohos.required_mount.system=/dev/block/vdb@/usr@ext4@ro,barrier=1@wait,required ohos.required_mount.vendor=/dev/block/vdc@/vendor@ext4@ro,barrier=1@wait,required"
22
23elf_file="out/qemu-arm-linux/packages/phone/images"
24if [ -f "${PWD}/ramdisk.img" ]; then
25  elf_file="${PWD}"
26fi
27
28help_info=$(cat <<-END
29Usage: qemu-run [OPTION]...
30Run a OHOS image in qemu according to the options.
31    -e,  --exec image_path    images path, including: zImage-dtb, ramdisk.img, system.img, vendor.img, userdata.img
32    -g,  --gdb                enable gdb for kernel.
33    -n,  --network            auto setup network for qemu (sudo required).
34    -i,  --instance id        start qemu images with specified instance id (from 01 to 99).
35                              it will also setup network when running in multiple instance mode.
36         -f                   force override instance image with a new copy.
37    -h,  --help               print this help info.
38
39    If no image_path specified, it will find OHOS image in current working directory; then try ${elf_file}.
40
41    When setting up network, it will create br0 on the host PC with the following information:
42        IP address: 192.168.100.1
43        netmask: 255.255.255.0
44
45    The default qemu device MAC address is [00:22:33:44:55:66], default serial number is [0023456789].
46    When running in multiple instances mode, the MAC address and serial number will increase with specified instance ID as follow:
47        MAC address:    {instanceID}:22:33:44:55:66
48        Serial number:  {instanceID}23456789
49END
50)
51
52function echo_help()
53{
54    echo "${help_info}"
55    exit 0
56}
57
58function qemu_option_add()
59{
60    qemu_option+=" "
61    qemu_option+="$1"
62}
63
64function kernel_bootargs_add()
65{
66    kernel_bootargs+=" "
67    kernel_bootargs+="$1"
68}
69
70function setup_sn()
71{
72    if [ x"${qemu_instance_id}" != x ]; then
73        kernel_bootargs_add "sn=${qemu_instance_id}23456789"
74    else
75        kernel_bootargs_add "sn=0023456789"
76    fi
77}
78
79function parameter_verification()
80{
81    if [ $1 -eq 0 ] || [ x"$(echo $2 | cut -c 1)" = x"-" ]; then
82        echo_help
83    fi
84}
85
86function qemu_network()
87{
88    ifconfig br0 > /dev/null 2>&1
89    if [ $? -ne 0 ]; then
90        qemu_install_path=`which qemu-system-arm`
91        qemu_install_path=`dirname ${qemu_install_path}`
92        qemu_install_path=${qemu_install_path}/../etc/qemu
93        [ ! -d ${qemu_install_path} ] && sudo mkdir -p ${qemu_install_path}
94        echo 'allow br0' | sudo tee -a ${qemu_install_path}/bridge.conf
95        [ ! -d /etc/qemu ] && sudo mkdir -p /etc/qemu
96        echo 'allow br0' | sudo tee -a /etc/qemu/bridge.conf
97        sudo modprobe tun tap
98        sudo ip link add br0 type bridge
99        sudo ip address add 192.168.100.1/24 dev br0
100        sudo ip link set dev br0 up
101    fi
102
103    [ x"${qemu_instance_id}" != x ] && qemu_option_add "-netdev bridge,id=net0 -device virtio-net-device,netdev=net0,mac=${qemu_instance_id}:22:33:44:55:66"
104    [ x"${qemu_instance_id}" == x ] && qemu_option_add "-netdev bridge,id=net0 -device virtio-net-device,netdev=net0,mac=00:22:33:44:55:66"
105    qemu_option_add "-no-user-config"
106}
107
108function copy_img()
109{
110    cp ${img_copy_option} $elf_file/userdata.img $elf_file/userdata${qemu_instance_id}.img
111    cp ${img_copy_option} $elf_file/vendor.img $elf_file/vendor${qemu_instance_id}.img
112    cp ${img_copy_option} $elf_file/system.img $elf_file/system${qemu_instance_id}.img
113    cp ${img_copy_option} $elf_file/updater.img $elf_file/updater${qemu_instance_id}.img
114}
115
116while [ $# -ne 0 ]
117do
118    case $1 in
119        "-e")
120            shift
121            parameter_verification $# $1
122            elf_file="$1"
123            ;;
124        "-i")
125            shift
126            parameter_verification $# $1
127            qemu_instance_id=$1
128            # Setup qemu network by default when running in multi instance mode
129            qemu_setup_network="y"
130            ;;
131        "-n")
132            qemu_setup_network="y"
133            ;;
134        "-g")
135            qemu_option_add "-s -S"
136            ;;
137        "-f")
138            img_copy_option=""
139            ;;
140        "-h")
141            echo_help
142            ;;
143    esac
144
145    shift
146done
147
148if [ ! -f $elf_file/ramdisk.img ]; then
149  echo_help
150fi
151
152# Setup qemu network if
153[ x"${qemu_setup_network}" != x ] && qemu_network
154
155# Copy original images for multi instance running
156[ x"${qemu_instance_id}" != x ] && copy_img
157
158setup_sn
159
160qemu_option_add "-drive if=none,file=$elf_file/userdata${qemu_instance_id}.img,format=raw,id=userdata,index=3 -device virtio-blk-device,drive=userdata"
161qemu_option_add "-drive if=none,file=$elf_file/vendor${qemu_instance_id}.img,format=raw,id=vendor,index=2 -device virtio-blk-device,drive=vendor"
162qemu_option_add "-drive if=none,file=$elf_file/system${qemu_instance_id}.img,format=raw,id=system,index=1 -device virtio-blk-device,drive=system"
163qemu_option_add "-drive if=none,file=$elf_file/updater${qemu_instance_id}.img,format=raw,id=updater,index=0 -device virtio-blk-device,drive=updater"
164qemu_option_add "-kernel $elf_file/zImage-dtb -initrd $elf_file/ramdisk.img"
165
166# Setup network need sudo
167[ x"${qemu_setup_network}" != x ] && sudo qemu-system-arm ${qemu_option} -append "${kernel_bootargs}"
168
169# start without sudo if no need to setup network
170[ x"${qemu_setup_network}" == x ] && qemu-system-arm ${qemu_option} -append "${kernel_bootargs}"
171