1#!/bin/bash
2
3set -ex
4
5test_variant=$1
6
7# Prompt to grep for to check if dom0 booted successfully
8dom0_prompt="^/ #"
9
10serial_log="$(pwd)/smoke.serial"
11
12cd binaries
13# Use the kernel from Debian
14curl --fail --silent --show-error --location --output vmlinuz https://deb.debian.org/debian/dists/bullseye/main/installer-armhf/current/images/netboot/vmlinuz
15# Use a tiny initrd based on busybox from Alpine Linux
16curl --fail --silent --show-error --location --output initrd.tar.gz https://dl-cdn.alpinelinux.org/alpine/v3.15/releases/armhf/alpine-minirootfs-3.15.1-armhf.tar.gz
17
18if [ -z "${test_variant}" ]; then
19    passed="generic test passed"
20    domU_check="
21echo \"${passed}\"
22"
23fi
24
25if [[ "${test_variant}" == "static-mem" ]]; then
26    # Memory range that is statically allocated to domU1
27    domu_base="0x50000000"
28    domu_size="0x20000000"
29    passed="${test_variant} test passed"
30    domU_check="
31mem_range=$(printf \"%08x-%08x\" ${domu_base} $(( ${domu_base} + ${domu_size} - 1 )))
32if grep -q -x \"\${mem_range} : System RAM\" /proc/iomem; then
33    echo \"${passed}\"
34fi
35"
36fi
37
38if [[ "${test_variant}" == "gzip" ]]; then
39    # Compress kernel image with gzip (keep unmodified one for dom0)
40    gzip -k vmlinuz
41    passed="${test_variant} test passed"
42    domU_check="
43echo \"${passed}\"
44"
45fi
46
47if [[ "${test_variant}" == "without-dom0" ]]; then
48    # Clear dom0 prompt
49    dom0_prompt=""
50    passed="${test_variant} test passed"
51    domU_check="
52echo \"${passed}\"
53"
54fi
55
56if [[ "${test_variant}" == "earlyprintk" ]]; then
57    # Clear dom0 prompt
58    dom0_prompt=""
59    # Last early printk message before entering C world
60    passed="\- Ready \-"
61fi
62
63# dom0/domU rootfs
64# We are using the same rootfs for dom0 and domU. The only difference is
65# that for the former, we set explictly rdinit to /bin/sh, whereas for the
66# latter we rely on using custom /init script with test case inside.
67mkdir rootfs
68cd rootfs
69tar xvzf ../initrd.tar.gz
70echo "#!/bin/sh
71
72mount -t proc proc /proc
73mount -t sysfs sysfs /sys
74mount -t devtmpfs devtmpfs /dev
75${domU_check}
76/bin/sh" > init
77chmod +x init
78find . | cpio -H newc -o | gzip > ../initrd.gz
79cd ..
80
81# XXX QEMU looks for "efi-virtio.rom" even if it is unneeded
82curl -fsSLO https://github.com/qemu/qemu/raw/v5.2.0/pc-bios/efi-virtio.rom
83./qemu-system-arm \
84    -machine virt \
85    -machine virtualization=true \
86    -smp 4 \
87    -m 2048 \
88    -serial stdio \
89    -monitor none \
90    -display none \
91    -machine dumpdtb=virt.dtb
92
93# ImageBuilder
94echo 'MEMORY_START="0x40000000"
95MEMORY_END="0xC0000000"
96
97DEVICE_TREE="virt.dtb"
98XEN="xen"
99XEN_CMD="console=dtuart dom0_mem=512M bootscrub=0 console_timestamps=boot"
100
101DOM0_KERNEL="vmlinuz"
102DOM0_RAMDISK="initrd.gz"
103DOM0_CMD="console=hvc0 earlyprintk clk_ignore_unused root=/dev/ram0 rdinit=/bin/sh"
104
105DOMU_KERNEL[0]="vmlinuz"
106DOMU_RAMDISK[0]="initrd.gz"
107DOMU_MEM[0]="512"
108NUM_DOMUS=1
109
110LOAD_CMD="tftpb"
111BOOT_CMD="bootz"
112UBOOT_SOURCE="boot.source"
113UBOOT_SCRIPT="boot.scr"' > config
114
115if [[ "${test_variant}" == "static-mem" ]]; then
116    echo -e "\nDOMU_STATIC_MEM[0]=\"${domu_base} ${domu_size}\"" >> config
117fi
118
119if [[ "${test_variant}" == "gzip" ]]; then
120    sed -i 's/DOMU_KERNEL\[0\]=.*/DOMU_KERNEL\[0\]="vmlinuz.gz"/' config
121fi
122
123if [[ "${test_variant}" == "without-dom0" ]]; then
124    sed -i '/^DOM0/d' config
125fi
126
127rm -rf imagebuilder
128git clone --depth 1 https://gitlab.com/xen-project/imagebuilder.git
129bash imagebuilder/scripts/uboot-script-gen -t tftp -d . -c config
130
131# Run the test
132rm -f ${serial_log}
133set +e
134echo "  virtio scan; dhcp; tftpb 0x40000000 boot.scr; source 0x40000000"| \
135timeout -k 1 240 \
136./qemu-system-arm \
137    -machine virt \
138    -machine virtualization=true \
139    -smp 4 \
140    -m 2048 \
141    -serial stdio \
142    -monitor none \
143    -display none \
144    -no-reboot \
145    -device virtio-net-pci,netdev=n0 \
146    -netdev user,id=n0,tftp=./ \
147    -bios /usr/lib/u-boot/qemu_arm/u-boot.bin |& \
148        tee ${serial_log} | sed 's/\r//'
149
150set -e
151(grep -q "${dom0_prompt}" ${serial_log} && grep -q "${passed}" ${serial_log}) || exit 1
152exit 0
153