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 16#set -e 17flash_name=flash.img 18rebuild=no 19net_enable=no 20vnc="-vnc :20 -serial mon:stdio" 21src_dir=out/arm_virt/qemu_small_system_demo/ 22re_arg=no 23bootargs=$(cat <<-END 24bootargs=root=cfi-flash fstype=jffs2 rootaddr=10M rootsize=22M useraddr=32M usersize=32M 25END 26) 27help_info=$(cat <<-END 28Usage: $0 [OPTION]... 29Make a qemu image($flash_name) for OHOS, and run the image in qemu according 30to the options. 31 32 Options: 33 34 -f, --force rebuild ${flash_name} 35 -n, --net-enable enable net 36 -l, --local-desktop no VNC 37 -b, --bootargs additional boot arguments(-bk1=v1,k2=v2...) 38 -h, --help print help info 39 40 By default, ${flash_name} will not be rebuilt if exists, and net will not 41 be enabled, gpu enabled and waiting for VNC connection at port 5920. 42END 43) 44 45function make_flash(){ 46 echo -e "\nStart making ${flash_name}..." 47 echo -ne "${bootargs}"'\0' > ${src_dir}/bootargs 48 dd if=/dev/zero of=${flash_name} bs=64M count=1 49 dd if=${src_dir}/OHOS_Image.bin of=${flash_name} conv=notrunc seek=0 oflag=seek_bytes 50 dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=9984k oflag=seek_bytes 51 dd if=${src_dir}/rootfs_jffs2.img of=${flash_name} conv=notrunc seek=10M oflag=seek_bytes 52 dd if=${src_dir}/userfs_jffs2.img of=${flash_name} conv=notrunc seek=32M oflag=seek_bytes 53 echo -e "Success making ${flash_name}...\n" 54} 55 56function net_config(){ 57 echo "Network config..." 58 sudo modprobe tun tap 59 sudo ip link add br0 type bridge 60 sudo ip address add 10.0.2.2/24 dev br0 61 sudo ip link set dev br0 up 62} 63 64function start_qemu(){ 65 net_enable=${1} 66 read -t 5 -p "Enter to start qemu[y/n]:" flag 67 start=${flag:-y} 68 if [[ "${vnc}" == "-vnc "* ]]; then 69 echo -e "Waiting VNC connection on: 5920 ...(Ctrl-C exit)" 70 fi 71 if [ ${start} = y ]; then 72 if [ ${net_enable} = yes ] 73 then 74 net_config 2>/dev/null 75 sudo `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \ 76 if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false -netdev bridge,id=net0 \ 77 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \ 78 -device virtio-gpu-device,xres=800,yres=480 -device virtio-mouse-device ${vnc} 79 else 80 `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \ 81 if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false \ 82 -device virtio-gpu-device,xres=800,yres=480 -device virtio-mouse-device ${vnc} 83 fi 84 else 85 echo "Exit qemu-run" 86 fi 87} 88 89############################## main ############################## 90ARGS=`getopt -o fnlb:h -l force,net-enable,local-desktop,bootargs:,help -n "$0" -- "$@"` 91if [ $? != 0 ]; then 92 echo "Try '$0 --help' for more information." 93 exit 1 94fi 95eval set --"${ARGS}" 96 97while true;do 98 case "${1}" in 99 -f|--force) 100 shift; 101 rebuild=yes 102 echo -e "Redo making ${flash_name}..." 103 ;; 104 -n|--net-enable) 105 shift; 106 net_enable=yes 107 echo -e "Qemu net enable..." 108 ;; 109 -l|--local-desktop) 110 shift; 111 vnc="" 112 ;; 113 -b|--bootargs) 114 shift; 115 re_arg=yes 116 bootargs+=" "${1//","/" "} 117 shift 118 ;; 119 -h|--help) 120 shift; 121 echo -e "${help_info}" 122 exit 123 ;; 124 --) 125 shift; 126 break; 127 ;; 128 esac 129done 130 131if [ ! -f "${flash_name}" ] || [ ${rebuild} = yes ]; then 132 make_flash ${flash_name} "${bootargs}" ${src_dir} 133elif [ ${re_arg} = yes ]; then 134 echo "Update bootargs..." 135 echo -e "${bootargs}"'\0' > ${src_dir}/bootargs 136 dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=9984k oflag=seek_bytes 137fi 138start_qemu ${net_enable} 139