• 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=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    modprobe -n nbd > /dev/null 2>&1
93    if [ $? != 0 ]; then
94        echo "Failed: need kernel module 'nbd'"
95        exit 1
96    fi
97
98    type qemu-img qemu-nbd > /dev/null 2>&1
99    if [ $? != 0 ]; then
100        echo "Failed: need qemu-utils 'qemu-img' and 'qemu-nbd'"
101        exit 1
102    fi
103
104    type parted > /dev/null 2>&1
105    if [ $? != 0 ]; then
106        echo "Failed: need tool 'parted'"
107        exit 1
108    fi
109}
110
111function make_mmc(){
112    echo -ne "\nStart making out/smallmmc.img..."
113
114    # Create raw "disk" with 1G, 2G, 5G partitions, all type 0C (FAT32 LBA).
115    qemu-img create -f raw out/smallmmc.raw 8G > /dev/null
116    sudo losetup /dev/loop0 out/smallmmc.raw
117    sudo parted -s /dev/loop0 -- mklabel msdos mkpart primary fat32 2048s 1025MiB \
118            mkpart primary fat32 1025MiB 3073MiB mkpart primary fat32 3073MiB -1s
119
120    # Format.
121    sudo losetup -o 1048576 /dev/loop1 /dev/loop0
122    sudo losetup -o 1074790400 /dev/loop2 /dev/loop0
123    sudo losetup -o 3222274048 /dev/loop3 /dev/loop0
124    sudo mkfs.vfat /dev/loop1 > /dev/null
125    sudo mkfs.vfat /dev/loop2 > /dev/null
126    sudo mkfs.vfat /dev/loop3 > /dev/null
127
128    # Clean.
129    sudo losetup -d /dev/loop3
130    sudo losetup -d /dev/loop2
131    sudo losetup -d /dev/loop1
132    sudo losetup -d /dev/loop0
133
134    # Convert to qcow2 format.
135    qemu-img convert -f raw out/smallmmc.raw -O qcow2 out/smallmmc.img
136    rm out/smallmmc.raw
137
138    # Mount.
139    sudo modprobe nbd
140    sudo qemu-nbd --connect=/dev/nbd0 out/smallmmc.img
141    sudo mount /dev/nbd0p1 /mnt   # 1st partition
142
143    # Copy necessary files.
144    sudo mkdir /mnt/data
145    sudo cp out/arm_virt/qemu_small_system_demo/data/line_cj.brk /mnt/data/
146    sudo cp out/arm_virt/qemu_small_system_demo/data/SourceHanSansSC-Regular.otf /mnt/data
147
148    # Unmount.
149    sudo umount /mnt
150    sudo qemu-nbd -d /dev/nbd0 > /dev/null
151    sudo modprobe -r nbd
152    sync out/smallmmc.img   # avoid 'Failed to get "write" lock' error
153
154    echo -e "done.\n"
155}
156
157function net_config(){
158    echo "Network config..."
159    set +e
160    sudo modprobe tun tap
161    sudo ip link add br0 type bridge
162    sudo ip address add 10.0.2.2/24 dev br0
163    sudo ip link set dev br0 up
164    set -e
165}
166
167function start_qemu(){
168    net_enable=${1}
169    if [[ "${vnc}" == "-vnc "* ]]; then
170        echo -e "Waiting VNC connection on: 5920 ...(Ctrl-C exit)"
171    fi
172    if [ ${net_enable} = yes ]
173    then
174        net_config 2>/dev/null
175        sudo `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \
176        if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false -netdev bridge,id=net0 \
177        -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
178        -device virtio-gpu-device,xres=800,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \
179        -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc -device virtio-blk-device,drive=mmc \
180        -device virtio-rng-device
181    else
182        `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \
183        if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false \
184        -device virtio-gpu-device,xres=800,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \
185        -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc -device virtio-blk-device,drive=mmc \
186        -device virtio-rng-device
187    fi
188}
189
190unsupported_parameters_check
191
192if [ ! -f "${flash_name}" ] || [ ${rebuild_image} = yes ]; then
193    make_flash ${flash_name} "${bootargs}" ${src_dir}
194elif [ ${add_boot_args} = yes ]; then
195    echo "Update bootargs..."
196    echo -e "${bootargs}"'\0' > ${src_dir}/bootargs
197    dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=9984k oflag=seek_bytes
198fi
199if [ ! -f "out/smallmmc.img" ]; then
200    check_mmc_tools
201    make_mmc
202fi
203start_qemu ${net_enable}
204