1#!/bin/bash
2
3print_usage() {
4  echo "Usage: $(basename $0) --arch [arm|arm64] [options]"
5  echo -e "Starts QEMU system mode emulation for the architecture.\n"
6  echo -e "  --help\t\t\tDisplay this information."
7  echo -e "  --arch {arm|arm64}\t\tSelects architecture QEMU system emulation."
8  echo -e "  --sve\t\t\t\tEnables AArch64 SVE mode."
9  echo -e "  --mte\t\t\t\tEnables AArch64 MTE mode.\n"
10  echo -e "  --rootfs {path}\t\tPath of root file system image."
11  echo -e "  --qemu {path}\t\t\tPath of pre-installed qemu-system-* executable."
12  echo -e "  --kernel {path}\t\tPath of Linux kernel prebuilt image.\n"
13  echo -e "By default this utility will use:"
14  echo -e "  QEMU image built from source in qemu.git directory"
15  echo -e "  Linux kernel image from linux.build/(arm or arm64) directory."
16  echo -e "Custom Linux kernel image or QEMU binary can be provided using commandline."
17  exit "$1"
18}
19
20invalid_arg() {
21  echo "ERROR: Unrecognized argument: $1" >&2
22  print_usage 1
23}
24
25run_qemu() {
26  QEMU_CORES=2
27  QEMU_MEMORY=1024
28
29  $QEMU_BIN \
30  -cpu $QEMU_CPU \
31  -m $QEMU_MEMORY \
32  -smp $QEMU_CORES \
33  -kernel $KERNEL_IMG \
34  -machine $QEMU_MACHINE \
35  -drive file=$ROOTFS_IMG,if=none,format=raw,id=hd0 \
36  -device virtio-blk-device,drive=hd0 \
37  -append "root=/dev/vda rw ip=dhcp mem=1024M raid=noautodetect  \
38  crashkernel=128M rootwait console=ttyAMA0 devtmpfs.mount=0" \
39  -netdev type=tap,id=net0 \
40  -device virtio-net-device,netdev=net0 \
41  -nographic
42}
43
44# Parse options
45while [[ $# -gt 0 ]]; do
46  case "${END_OF_OPT}${1}" in
47    --arch)     ARCH=$2; shift;;
48    --rootfs)   ROOTFS_IMG=$2; shift;;
49    --kernel)   KERNEL_IMG=$2; shift;;
50    --qemu)     QEMU_BIN=$2; shift;;
51    --sve)      SVE=1;;
52    --mte)      MTE=1;;
53    --help)     print_usage 0 ;;
54    *)          invalid_arg "$1" ;;
55  esac
56  shift
57done
58
59if [ "$ARCH" == "arm64" ] && [ "$ARCH" == "arm" ]; then
60  echo "Invalid architecture: $ARCH"
61  print_usage 1
62fi
63
64if [[ ! -f "$ROOTFS_IMG" ]]; then
65  echo "No root file system image image available for emulation."
66  exit
67fi
68
69if [[ ! -f "$KERNEL_IMG" ]]; then
70  KERNEL_IMG_PATH=$(pwd)/linux.build/"$ARCH"/arch/"$ARCH"/boot/
71
72  if [[ ! -d "$KERNEL_IMG_PATH" ]]; then
73    echo "No Linux kernel image available for emulation."
74    exit
75  fi
76
77  if [[ "$ARCH" == "arm" ]]; then
78    KERNEL_IMG=$KERNEL_IMG_PATH/zImage
79  elif [[ "$ARCH" == "arm64" ]]; then
80    KERNEL_IMG=$KERNEL_IMG_PATH/Image
81  fi
82fi
83
84if [[ ! -f "$QEMU_BIN" ]]; then
85  if [[ "$ARCH" == "arm" ]]; then
86    QEMU_BIN=$(pwd)/qemu.git/arm-softmmu/qemu-system-arm
87  elif [[ "$ARCH" == "arm64" ]]; then
88    QEMU_BIN=$(pwd)/qemu.git/aarch64-softmmu/qemu-system-aarch64
89  fi
90
91  if [[ ! -f "$QEMU_BIN" ]]; then
92    echo "QEMU $ARCH system emulation executable not found."
93    exit
94  fi
95fi
96
97if [[ "$ARCH" == "arm" ]]; then
98  QEMU_MACHINE="virt,highmem=off"
99  QEMU_CPU="cortex-a15"
100
101  if [[ $SVE ]]; then
102    echo "warning: --sve is supported by AArch64 targets only"
103  fi
104  if [[ $MTE ]]; then
105    echo "warning: --mte is supported by AArch64 targets only"
106  fi
107elif [[ "$ARCH" == "arm64" ]]; then
108  QEMU_MACHINE=virt
109  QEMU_SVE_MAX_VQ=4
110  QEMU_CPU="cortex-a53"
111
112  if [[ $SVE ]]; then
113    QEMU_CPU="max,sve-max-vq=$QEMU_SVE_MAX_VQ"
114  fi
115  if [[ $MTE ]]; then
116    QEMU_MACHINE="$QEMU_MACHINE,mte=on"
117  fi
118fi
119
120run_qemu
121