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=flash.img 18vnc="-vnc :20 -serial mon:stdio" 19src_dir=out/arm_virt/qemu_small_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 echo -ne "${bootargs}"'\0' > ${src_dir}/bootargs 83 dd if=/dev/zero of=${flash_name} bs=64M count=1 84 dd if=${src_dir}/OHOS_Image.bin of=${flash_name} conv=notrunc seek=0 oflag=seek_bytes 85 dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=9984k oflag=seek_bytes 86 dd if=${src_dir}/rootfs_jffs2.img of=${flash_name} conv=notrunc seek=10M oflag=seek_bytes 87 dd if=${src_dir}/userfs_jffs2.img of=${flash_name} conv=notrunc seek=37M oflag=seek_bytes 88 echo -e "Succeed making ${flash_name}.\n" 89} 90 91function check_mmc_tools(){ 92 type parted > /dev/null 93} 94 95function make_mmc(){ 96 echo -ne "\nStart making out/smallmmc.img..." 97 98 # Create raw "disk" with 1G, 2G, 5G partitions, all type 0C (FAT32 LBA). 99 qemu-img create -f raw out/smallmmc.raw 8G > /dev/null 100 sudo losetup /dev/loop590 out/smallmmc.raw 101 sudo parted -s /dev/loop590 -- mklabel msdos mkpart primary fat32 2048s 1025MiB \ 102 mkpart primary fat32 1025MiB 3073MiB mkpart primary fat32 3073MiB -1s 103 104 # Format. 105 sudo losetup -o 1048576 /dev/loop591 /dev/loop590 106 sudo losetup -o 1074790400 /dev/loop592 /dev/loop590 107 sudo losetup -o 3222274048 /dev/loop593 /dev/loop590 108 sudo mkfs.vfat /dev/loop591 > /dev/null 109 sudo mkfs.vfat /dev/loop592 > /dev/null 110 sudo mkfs.vfat /dev/loop593 > /dev/null 111 112 # Clean. 113 sudo losetup -d /dev/loop593 114 sudo losetup -d /dev/loop592 115 sudo losetup -d /dev/loop591 116 sudo losetup -d /dev/loop590 117 118 # Convert to qcow2 format. 119 qemu-img convert -f raw out/smallmmc.raw -O qcow2 out/smallmmc.img 120 rm out/smallmmc.raw 121 122 echo -e "done.\n" 123} 124 125function net_config(){ 126 echo "Network config..." 127 set +e 128 sudo modprobe tun tap 129 sudo ip link add br0 type bridge 130 sudo ip address add 10.0.2.2/24 dev br0 131 sudo ip link set dev br0 up 132 set -e 133} 134 135function start_qemu(){ 136 net_enable=${1} 137 if [[ "${vnc}" == "-vnc "* ]]; then 138 echo -e "Waiting VNC connection on: 5920 ...(Ctrl-C exit)" 139 fi 140 if [ ${net_enable} = yes ] 141 then 142 net_config 2>/dev/null 143 sudo `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \ 144 if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false -netdev bridge,id=net0 \ 145 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \ 146 -device virtio-gpu-device,xres=960,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \ 147 -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc -device virtio-blk-device,drive=mmc \ 148 -device virtio-rng-device 149 else 150 `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \ 151 if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false \ 152 -device virtio-gpu-device,xres=960,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \ 153 -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc -device virtio-blk-device,drive=mmc \ 154 -device virtio-rng-device 155 fi 156} 157 158unsupported_parameters_check 159 160if [ ! -f "${flash_name}" ] || [ ${rebuild_image} = yes ]; then 161 make_flash ${flash_name} "${bootargs}" ${src_dir} 162elif [ ${add_boot_args} = yes ]; then 163 echo "Update bootargs..." 164 echo -e "${bootargs}"'\0' > ${src_dir}/bootargs 165 dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=9984k oflag=seek_bytes 166fi 167if [ ! -f "out/smallmmc.img" ]; then 168 check_mmc_tools 169 make_mmc 170fi 171start_qemu ${net_enable} 172