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 17flash_name=ca7_mini.img 18vnc="-vnc :20 -serial mon:stdio" 19src_dir=out/arm_virt/qemu_ca7_mini_system_demo 20bootargs=$(cat <<-END 21bootargs=root=cfi-flash fstype=jffs2 rootaddr=10M rootsize=27M useraddr=37M usersize=27M 22END 23) 24 25elf_file=$1 26rebuild_image=$2 27vnc_enable=$3 28add_boot_args=$4 29boot_args=$5 30net_enable=$6 31gdb_enable=$7 32qemu_test=$8 33test_file=$9 34qemu_help=${10} 35 36help_info=$(cat <<-END 37Usage: qemu-run [OPTION]... 38Make a qemu image($flash_name) for OHOS, and run the image in qemu according 39to the options. 40 41 Options: 42 43 -f, --force rebuild ${flash_name} 44 -n, --net-enable enable net 45 -l, --local-desktop no VNC 46 -b, --bootargs boot_arguments additional boot arguments(-bk1=v1,k2=v2...) 47 -g, --gdb enable gdb for kernel 48 -h, --help print help info 49 50 By default, ${flash_name} will not be rebuilt if exists, and net will not 51 be enabled, gpu enabled and waiting for VNC connection at port 5920. 52END 53) 54 55if [ "$qemu_help" = "yes" ]; then 56 echo "${help_info}" 57 exit 0 58fi 59 60if [ "$vnc_enable" = "no" ]; then 61 vnc="" 62fi 63 64if [ "$add_boot_args" = "yes" ]; then 65 bootargs+=" "${boot_args//","/" "} 66fi 67 68if [ "$gdb_enable" = "yes" ]; then 69 qemu_option+="-s -S" 70fi 71 72function unsupported_parameters_check(){ 73 if [ "$qemu_test" = "test" ]; then 74 echo "Error: The -t|--test option is not supported !" 75 echo "${help_info}" 76 exit 1 77 fi 78} 79 80function make_flash(){ 81 echo -e "\nStart making ${flash_name}..." 82 dd if=/dev/zero of=${flash_name} bs=64M count=1 83 dd if=${src_dir}/OHOS_Image.bin of=${flash_name} conv=notrunc seek=0 oflag=seek_bytes 84 echo -e "Succeed making ${flash_name}.\n" 85} 86 87function net_config(){ 88 echo "Network config..." 89 set +e 90 sudo modprobe tun tap 91 sudo ip link add br0 type bridge 92 sudo ip address add 10.0.2.2/24 dev br0 93 sudo ip link set dev br0 up 94 set -e 95} 96 97function start_qemu(){ 98 net_enable=${1} 99 if [[ "${vnc}" == "-vnc "* ]]; then 100 echo -e "Waiting VNC connection on: 5920 ...(Ctrl-C exit)" 101 fi 102 if [ ${net_enable} = yes ] 103 then 104 net_config 2>/dev/null 105 sudo `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \ 106 if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false -netdev bridge,id=net0 \ 107 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \ 108 -device virtio-gpu-device,xres=960,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \ 109 -device virtio-rng-device 110 else 111 `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \ 112 if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false \ 113 -device virtio-gpu-device,xres=960,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \ 114 -device virtio-rng-device 115 fi 116} 117 118unsupported_parameters_check 119 120if [ ! -f "${flash_name}" ] || [ ${rebuild_image} = yes ]; then 121 make_flash ${flash_name} "${bootargs}" ${src_dir} 122elif [ ${add_boot_args} = yes ]; then 123 echo "Update bootargs..." 124 echo -e "${bootargs}"'\0' > ${src_dir}/bootargs 125 dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=9984k oflag=seek_bytes 126fi 127start_qemu ${net_enable} 128