• 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
16set -e
17flash_name=out/smallmmc.img
18vnc="-vnc :20  -serial mon:stdio"
19src_dir=out/arm_virt/qemu_small_system_demo/
20bootargs=$(cat <<-END
21bootargs=root=emmc fstype=vfat rootaddr=10M rootsize=20M useraddr=30M usersize=50M
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 for OHOS, and run the image in qemu according
39to the options.
40
41    Options:
42
43    -f, --force                    rebuild image
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, image 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 check_mmc_tools(){
81    type parted > /dev/null
82}
83
84function make_mmc(){
85    echo -ne "\nStart making ${flash_name}..."
86    if [ -f ${flash_name} ]; then
87        existed=true
88    else
89        existed=false
90    fi
91
92    # Create raw "disk" with type 0C (FAT32 LBA) according to los_rootfs.h
93    if [ $existed == false ]; then
94        dd if=/dev/zero of=${flash_name} bs=100M count=1
95    fi
96    sudo losetup /dev/loop590 ${flash_name}
97    if [ $existed == false  ]; then
98        sudo parted -s /dev/loop590 -- mklabel msdos mkpart primary fat32 10MiB 30MiB \
99            mkpart primary fat32 30MiB 80MiB mkpart primary fat32 80MiB -1s
100    fi
101
102    sudo losetup -o 10MiB /dev/loop591 /dev/loop590
103    sudo losetup -o 30MiB /dev/loop592 /dev/loop590 # do not touch partition 3 contents
104    if [ $existed == false  ]; then
105        sudo losetup -o 80MiB /dev/loop593 /dev/loop590
106        sudo mkfs.vfat /dev/loop591 > /dev/null
107        sudo mkfs.vfat /dev/loop592 > /dev/null
108        sudo mkfs.vfat /dev/loop593 > /dev/null
109        sudo losetup -d /dev/loop593
110    fi
111
112    # Copy files.
113    sudo mount /dev/loop591 /mnt
114    sudo cp -r -f ${src_dir}/rootfs/* /mnt
115    sudo umount /mnt
116    sudo mount /dev/loop592 /mnt
117    # redundant binaries would overflow disk
118    rm -rf ${src_dir}/userfs/test/unittest/*/unstripped
119    sudo cp -r -f ${src_dir}/userfs/* /mnt
120    sudo umount /mnt
121
122    # Clean.
123    sudo losetup -d /dev/loop592
124    sudo losetup -d /dev/loop591
125    sudo losetup -d /dev/loop590
126
127    # Write bootargs.
128    echo -ne "${bootargs}"'\0' > ${src_dir}/bootargs
129    dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=512k oflag=seek_bytes
130
131    echo -e "done.\n"
132}
133
134function net_config(){
135    echo "Network config..."
136    set +e
137    sudo modprobe tun tap
138    sudo ip link add br0 type bridge
139    sudo ip address add 10.0.2.2/24 dev br0
140    sudo ip link set dev br0 up
141    set -e
142}
143
144function start_qemu(){
145    net_enable=${1}
146    if [[ "${vnc}" == "-vnc "* ]]; then
147        echo -e "Waiting VNC connection on: 5920 ...(Ctrl-C exit)"
148    fi
149    if [ ${net_enable} = yes ]
150    then
151        net_config 2>/dev/null
152        sudo `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G \
153        -bios ${src_dir}/OHOS_Image.bin -global virtio-mmio.force-legacy=false -netdev bridge,id=net0 \
154        -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
155        -device virtio-gpu-device,xres=960,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \
156        -drive if=none,file=${flash_name},format=raw,id=mmc -device virtio-blk-device,drive=mmc \
157        -device virtio-rng-device
158    else
159        `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G \
160        -bios ${src_dir}/OHOS_Image.bin -global virtio-mmio.force-legacy=false \
161        -device virtio-gpu-device,xres=960,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \
162        -drive if=none,file=${flash_name},format=raw,id=mmc -device virtio-blk-device,drive=mmc \
163        -device virtio-rng-device
164    fi
165}
166
167unsupported_parameters_check
168
169if [ ! -f ${flash_name} ] || [ ${rebuild_image} = yes ]; then
170    check_mmc_tools
171    make_mmc
172elif [ ${add_boot_args} = yes ]; then
173    echo "Update bootargs..."
174    echo -e "${bootargs}"'\0' > ${src_dir}/bootargs
175    dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=512k oflag=seek_bytes
176fi
177start_qemu ${net_enable}
178