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_ciqemu_option="-M virt -cpu cortex-a7 -smp 4 -m 1024 -nographic" 17799b5ee9Sopenharmony_ciqemu_setup_network="" 18799b5ee9Sopenharmony_ciqemu_instance_id="" 19799b5ee9Sopenharmony_ciimg_copy_option="-n" 20799b5ee9Sopenharmony_ci 21799b5ee9Sopenharmony_cikernel_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" 22799b5ee9Sopenharmony_ci 23799b5ee9Sopenharmony_cielf_file="out/qemu-arm-linux/packages/phone/images" 24799b5ee9Sopenharmony_ciif [ -f "${PWD}/ramdisk.img" ]; then 25799b5ee9Sopenharmony_ci elf_file="${PWD}" 26799b5ee9Sopenharmony_cifi 27799b5ee9Sopenharmony_ci 28799b5ee9Sopenharmony_cihelp_info=$(cat <<-END 29799b5ee9Sopenharmony_ciUsage: qemu-run [OPTION]... 30799b5ee9Sopenharmony_ciRun a OHOS image in qemu according to the options. 31799b5ee9Sopenharmony_ci -e, --exec image_path images path, including: zImage-dtb, ramdisk.img, system.img, vendor.img, userdata.img 32799b5ee9Sopenharmony_ci -g, --gdb enable gdb for kernel. 33799b5ee9Sopenharmony_ci -n, --network auto setup network for qemu (sudo required). 34799b5ee9Sopenharmony_ci -i, --instance id start qemu images with specified instance id (from 01 to 99). 35799b5ee9Sopenharmony_ci it will also setup network when running in multiple instance mode. 36799b5ee9Sopenharmony_ci -f force override instance image with a new copy. 37799b5ee9Sopenharmony_ci -h, --help print this help info. 38799b5ee9Sopenharmony_ci 39799b5ee9Sopenharmony_ci If no image_path specified, it will find OHOS image in current working directory; then try ${elf_file}. 40799b5ee9Sopenharmony_ci 41799b5ee9Sopenharmony_ci When setting up network, it will create br0 on the host PC with the following information: 42799b5ee9Sopenharmony_ci IP address: 192.168.100.1 43799b5ee9Sopenharmony_ci netmask: 255.255.255.0 44799b5ee9Sopenharmony_ci 45799b5ee9Sopenharmony_ci The default qemu device MAC address is [00:22:33:44:55:66], default serial number is [0023456789]. 46799b5ee9Sopenharmony_ci When running in multiple instances mode, the MAC address and serial number will increase with specified instance ID as follow: 47799b5ee9Sopenharmony_ci MAC address: {instanceID}:22:33:44:55:66 48799b5ee9Sopenharmony_ci Serial number: {instanceID}23456789 49799b5ee9Sopenharmony_ciEND 50799b5ee9Sopenharmony_ci) 51799b5ee9Sopenharmony_ci 52799b5ee9Sopenharmony_cifunction echo_help() 53799b5ee9Sopenharmony_ci{ 54799b5ee9Sopenharmony_ci echo "${help_info}" 55799b5ee9Sopenharmony_ci exit 0 56799b5ee9Sopenharmony_ci} 57799b5ee9Sopenharmony_ci 58799b5ee9Sopenharmony_cifunction qemu_option_add() 59799b5ee9Sopenharmony_ci{ 60799b5ee9Sopenharmony_ci qemu_option+=" " 61799b5ee9Sopenharmony_ci qemu_option+="$1" 62799b5ee9Sopenharmony_ci} 63799b5ee9Sopenharmony_ci 64799b5ee9Sopenharmony_cifunction kernel_bootargs_add() 65799b5ee9Sopenharmony_ci{ 66799b5ee9Sopenharmony_ci kernel_bootargs+=" " 67799b5ee9Sopenharmony_ci kernel_bootargs+="$1" 68799b5ee9Sopenharmony_ci} 69799b5ee9Sopenharmony_ci 70799b5ee9Sopenharmony_cifunction setup_sn() 71799b5ee9Sopenharmony_ci{ 72799b5ee9Sopenharmony_ci if [ x"${qemu_instance_id}" != x ]; then 73799b5ee9Sopenharmony_ci kernel_bootargs_add "sn=${qemu_instance_id}23456789" 74799b5ee9Sopenharmony_ci else 75799b5ee9Sopenharmony_ci kernel_bootargs_add "sn=0023456789" 76799b5ee9Sopenharmony_ci fi 77799b5ee9Sopenharmony_ci} 78799b5ee9Sopenharmony_ci 79799b5ee9Sopenharmony_cifunction parameter_verification() 80799b5ee9Sopenharmony_ci{ 81799b5ee9Sopenharmony_ci if [ $1 -eq 0 ] || [ x"$(echo $2 | cut -c 1)" = x"-" ]; then 82799b5ee9Sopenharmony_ci echo_help 83799b5ee9Sopenharmony_ci fi 84799b5ee9Sopenharmony_ci} 85799b5ee9Sopenharmony_ci 86799b5ee9Sopenharmony_cifunction qemu_network() 87799b5ee9Sopenharmony_ci{ 88799b5ee9Sopenharmony_ci ifconfig br0 > /dev/null 2>&1 89799b5ee9Sopenharmony_ci if [ $? -ne 0 ]; then 90799b5ee9Sopenharmony_ci qemu_install_path=`which qemu-system-arm` 91799b5ee9Sopenharmony_ci qemu_install_path=`dirname ${qemu_install_path}` 92799b5ee9Sopenharmony_ci qemu_install_path=${qemu_install_path}/../etc/qemu 93799b5ee9Sopenharmony_ci [ ! -d ${qemu_install_path} ] && sudo mkdir -p ${qemu_install_path} 94799b5ee9Sopenharmony_ci echo 'allow br0' | sudo tee -a ${qemu_install_path}/bridge.conf 95799b5ee9Sopenharmony_ci [ ! -d /etc/qemu ] && sudo mkdir -p /etc/qemu 96799b5ee9Sopenharmony_ci echo 'allow br0' | sudo tee -a /etc/qemu/bridge.conf 97799b5ee9Sopenharmony_ci sudo modprobe tun tap 98799b5ee9Sopenharmony_ci sudo ip link add br0 type bridge 99799b5ee9Sopenharmony_ci sudo ip address add 192.168.100.1/24 dev br0 100799b5ee9Sopenharmony_ci sudo ip link set dev br0 up 101799b5ee9Sopenharmony_ci fi 102799b5ee9Sopenharmony_ci 103799b5ee9Sopenharmony_ci [ 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" 104799b5ee9Sopenharmony_ci [ 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" 105799b5ee9Sopenharmony_ci qemu_option_add "-no-user-config" 106799b5ee9Sopenharmony_ci} 107799b5ee9Sopenharmony_ci 108799b5ee9Sopenharmony_cifunction copy_img() 109799b5ee9Sopenharmony_ci{ 110799b5ee9Sopenharmony_ci cp ${img_copy_option} $elf_file/userdata.img $elf_file/userdata${qemu_instance_id}.img 111799b5ee9Sopenharmony_ci cp ${img_copy_option} $elf_file/vendor.img $elf_file/vendor${qemu_instance_id}.img 112799b5ee9Sopenharmony_ci cp ${img_copy_option} $elf_file/system.img $elf_file/system${qemu_instance_id}.img 113799b5ee9Sopenharmony_ci cp ${img_copy_option} $elf_file/updater.img $elf_file/updater${qemu_instance_id}.img 114799b5ee9Sopenharmony_ci} 115799b5ee9Sopenharmony_ci 116799b5ee9Sopenharmony_ciwhile [ $# -ne 0 ] 117799b5ee9Sopenharmony_cido 118799b5ee9Sopenharmony_ci case $1 in 119799b5ee9Sopenharmony_ci "-e") 120799b5ee9Sopenharmony_ci shift 121799b5ee9Sopenharmony_ci parameter_verification $# $1 122799b5ee9Sopenharmony_ci elf_file="$1" 123799b5ee9Sopenharmony_ci ;; 124799b5ee9Sopenharmony_ci "-i") 125799b5ee9Sopenharmony_ci shift 126799b5ee9Sopenharmony_ci parameter_verification $# $1 127799b5ee9Sopenharmony_ci qemu_instance_id=$1 128799b5ee9Sopenharmony_ci # Setup qemu network by default when running in multi instance mode 129799b5ee9Sopenharmony_ci qemu_setup_network="y" 130799b5ee9Sopenharmony_ci ;; 131799b5ee9Sopenharmony_ci "-n") 132799b5ee9Sopenharmony_ci qemu_setup_network="y" 133799b5ee9Sopenharmony_ci ;; 134799b5ee9Sopenharmony_ci "-g") 135799b5ee9Sopenharmony_ci qemu_option_add "-s -S" 136799b5ee9Sopenharmony_ci ;; 137799b5ee9Sopenharmony_ci "-f") 138799b5ee9Sopenharmony_ci img_copy_option="" 139799b5ee9Sopenharmony_ci ;; 140799b5ee9Sopenharmony_ci "-h") 141799b5ee9Sopenharmony_ci echo_help 142799b5ee9Sopenharmony_ci ;; 143799b5ee9Sopenharmony_ci esac 144799b5ee9Sopenharmony_ci 145799b5ee9Sopenharmony_ci shift 146799b5ee9Sopenharmony_cidone 147799b5ee9Sopenharmony_ci 148799b5ee9Sopenharmony_ciif [ ! -f $elf_file/ramdisk.img ]; then 149799b5ee9Sopenharmony_ci echo_help 150799b5ee9Sopenharmony_cifi 151799b5ee9Sopenharmony_ci 152799b5ee9Sopenharmony_ci# Setup qemu network if 153799b5ee9Sopenharmony_ci[ x"${qemu_setup_network}" != x ] && qemu_network 154799b5ee9Sopenharmony_ci 155799b5ee9Sopenharmony_ci# Copy original images for multi instance running 156799b5ee9Sopenharmony_ci[ x"${qemu_instance_id}" != x ] && copy_img 157799b5ee9Sopenharmony_ci 158799b5ee9Sopenharmony_cisetup_sn 159799b5ee9Sopenharmony_ci 160799b5ee9Sopenharmony_ciqemu_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" 161799b5ee9Sopenharmony_ciqemu_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" 162799b5ee9Sopenharmony_ciqemu_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" 163799b5ee9Sopenharmony_ciqemu_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" 164799b5ee9Sopenharmony_ciqemu_option_add "-kernel $elf_file/zImage-dtb -initrd $elf_file/ramdisk.img" 165799b5ee9Sopenharmony_ci 166799b5ee9Sopenharmony_ci# Setup network need sudo 167799b5ee9Sopenharmony_ci[ x"${qemu_setup_network}" != x ] && sudo qemu-system-arm ${qemu_option} -append "${kernel_bootargs}" 168799b5ee9Sopenharmony_ci 169799b5ee9Sopenharmony_ci# start without sudo if no need to setup network 170799b5ee9Sopenharmony_ci[ x"${qemu_setup_network}" == x ] && qemu-system-arm ${qemu_option} -append "${kernel_bootargs}" 171